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 // ValidatingUtil wraps data with checksum and timestamp. Format:
17 // timestamp=<timestamp>
18 // checksum=<checksum>
21 // The timestamp is the time_t that was returned from time() function. The
22 // timestamp does not need to be portable because it is written and read only by
23 // ValidatingUtil. The value is somewhat human-readable: it is the number of
24 // seconds since the epoch.
26 // The checksum is the 32-character hexadecimal MD5 checksum of <data>. It is
27 // meant to protect from random file changes on disk.
29 #include "validating_util.h"
41 namespace addressinput {
45 const char kTimestampPrefix[] = "timestamp=";
46 const size_t kTimestampPrefixLength = sizeof kTimestampPrefix - 1;
48 const char kChecksumPrefix[] = "checksum=";
49 const size_t kChecksumPrefixLength = sizeof kChecksumPrefix - 1;
51 const char kSeparator = '\n';
53 // Places the header value into |header_value| parameter and erases the header
54 // from |data|. Returns |true| if the header format is valid.
55 bool UnwrapHeader(const char* header_prefix,
56 size_t header_prefix_length,
58 std::string* header_value) {
59 assert(header_prefix != NULL);
61 assert(header_value != NULL);
64 0, header_prefix_length, header_prefix, header_prefix_length) != 0) {
68 std::string::size_type separator_position =
69 data->find(kSeparator, header_prefix_length);
70 if (separator_position == std::string::npos) {
75 *data, header_prefix_length, separator_position - header_prefix_length);
76 data->erase(0, separator_position + 1);
84 void ValidatingUtil::Wrap(time_t timestamp, std::string* data) {
86 char timestamp_string[2 + 3 * sizeof timestamp];
88 std::sprintf(timestamp_string, "%ld", static_cast<long>(timestamp));
90 assert(size < sizeof timestamp_string);
94 header.append(kTimestampPrefix, kTimestampPrefixLength);
95 header.append(timestamp_string);
96 header.push_back(kSeparator);
98 header.append(kChecksumPrefix, kChecksumPrefixLength);
99 header.append(MD5String(*data));
100 header.push_back(kSeparator);
102 data->reserve(header.size() + data->size());
103 data->insert(0, header);
107 bool ValidatingUtil::UnwrapTimestamp(std::string* data, time_t now) {
108 assert(data != NULL);
113 std::string timestamp_string;
115 kTimestampPrefix, kTimestampPrefixLength, data, ×tamp_string)) {
119 time_t timestamp = atol(timestamp_string.c_str());
124 // One month contains:
126 // 24 hours per day *
127 // 60 minutes per hour *
128 // 60 seconds per minute.
129 static const double kOneMonthInSeconds = 30.0 * 24.0 * 60.0 * 60.0;
130 double age_in_seconds = difftime(now, timestamp);
131 return !(age_in_seconds < 0.0) && age_in_seconds < kOneMonthInSeconds;
135 bool ValidatingUtil::UnwrapChecksum(std::string* data) {
136 assert(data != NULL);
137 std::string checksum;
138 if (!UnwrapHeader(kChecksumPrefix, kChecksumPrefixLength, data, &checksum)) {
141 return checksum == MD5String(*data);
144 } // namespace addressinput