blob: 208c560c33de08822d7da9a4c12fdf206b26b321 [file] [log] [blame]
Bertrand SIMONNET52e5b992015-08-10 15:18:00 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Alex Vakulenko788d3b62014-12-11 09:48:46 -080016
Bertrand SIMONNET4b915ae2015-07-28 15:38:14 -070017#include "uploader/metrics_hashes.h"
Alex Vakulenko788d3b62014-12-11 09:48:46 -080018
19#include "base/logging.h"
20#include "base/md5.h"
21#include "base/sys_byteorder.h"
22
23namespace metrics {
24
25namespace {
26
27// Converts the 8-byte prefix of an MD5 hash into a uint64 value.
28inline uint64_t HashToUInt64(const std::string& hash) {
29 uint64_t value;
30 DCHECK_GE(hash.size(), sizeof(value));
31 memcpy(&value, hash.data(), sizeof(value));
32 return base::HostToNet64(value);
33}
34
35} // namespace
36
37uint64_t HashMetricName(const std::string& name) {
38 // Create an MD5 hash of the given |name|, represented as a byte buffer
39 // encoded as an std::string.
40 base::MD5Context context;
41 base::MD5Init(&context);
42 base::MD5Update(&context, name);
43
44 base::MD5Digest digest;
45 base::MD5Final(&digest, &context);
46
47 std::string hash_str(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
48 return HashToUInt64(hash_str);
49}
50
51} // namespace metrics