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 <libaddressinput/util/basictypes.h> 23 24#include <ctime> 25#include <string> 26 27namespace i18n { 28namespace addressinput { 29 30// Wraps data with a checksum and a timestamp. Sample usage: 31// std::string data = ... 32// ValidatingUtil::Wrap(time(NULL), &data); 33// Process(data); 34// 35// std::string unwrapped = wrapped; 36// if (ValidatingUtil::UnwrapTimestamp(&unwrapped, time(NULL)) && 37// ValidatingUtil::UnwrapChecksum(&unwrapped)) { 38// Process(unwrapped); 39// } 40class ValidatingUtil { 41 public: 42 // Adds checksum and given |timestamp| to |data|. 43 static void Wrap(time_t timestamp, std::string* data); 44 45 // Strips out the timestamp from |data|. Returns |true| if the timestamp is 46 // present, formatted correctly, valid, and recent with respect to |now|. 47 static bool UnwrapTimestamp(std::string* data, time_t now); 48 49 // Strips out the checksum from |data|. Returns |true| if the checksum is 50 // present, formatted correctly, and valid for this data. 51 static bool UnwrapChecksum(std::string* data); 52 53 private: 54 DISALLOW_COPY_AND_ASSIGN(ValidatingUtil); 55}; 56 57} // namespace addressinput 58} // namespace i18n 59 60#endif // I18N_ADDRESSINPUT_VALIDATING_UTIL_H_ 61