1 // Copyright (C) 2013 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // An object to wrap data with a checksum and a timestamp. These fields are used 16 // to verify that the data is not stale or corrupted. Staleness threshold is 1 17 // month. 18 19 #ifndef I18N_ADDRESSINPUT_VALIDATING_UTIL_H_ 20 #define I18N_ADDRESSINPUT_VALIDATING_UTIL_H_ 21 22 #include <ctime> 23 #include <string> 24 25 namespace i18n { 26 namespace addressinput { 27 28 // Wraps data with a checksum and a timestamp. Sample usage: 29 // std::string data = ... 30 // ValidatingUtil::Wrap(time(NULL), &data); 31 // Process(data); 32 // 33 // std::string unwrapped = wrapped; 34 // if (ValidatingUtil::UnwrapTimestamp(&unwrapped, time(NULL)) && 35 // ValidatingUtil::UnwrapChecksum(&unwrapped)) { 36 // Process(unwrapped); 37 // } 38 class ValidatingUtil { 39 public: 40 // Adds checksum and given |timestamp| to |data|. 41 static void Wrap(time_t timestamp, std::string* data); 42 43 // Strips out the timestamp from |data|. Returns |true| if the timestamp is 44 // present, formatted correctly, valid, and recent with respect to |now|. 45 static bool UnwrapTimestamp(std::string* data, time_t now); 46 47 // Strips out the checksum from |data|. Returns |true| if the checksum is 48 // present, formatted correctly, and valid for this data. 49 static bool UnwrapChecksum(std::string* data); 50 }; 51 52 } // namespace addressinput 53 } // namespace i18n 54 55 #endif // I18N_ADDRESSINPUT_VALIDATING_UTIL_H_ 56