1 // Copyright (C) 2013 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
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
19 #ifndef I18N_ADDRESSINPUT_VALIDATING_UTIL_H_
20 #define I18N_ADDRESSINPUT_VALIDATING_UTIL_H_
26 namespace addressinput {
28 // Wraps data with a checksum and a timestamp. Sample usage:
29 // std::string data = ...
30 // ValidatingUtil::Wrap(time(NULL), &data);
33 // std::string unwrapped = wrapped;
34 // if (ValidatingUtil::UnwrapTimestamp(&unwrapped, time(NULL)) &&
35 // ValidatingUtil::UnwrapChecksum(&unwrapped)) {
36 // Process(unwrapped);
38 class ValidatingUtil {
40 // Adds checksum and given |timestamp| to |data|.
41 static void Wrap(time_t timestamp, std::string* data);
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);
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);
52 } // namespace addressinput
55 #endif // I18N_ADDRESSINPUT_VALIDATING_UTIL_H_