blob: e6e713c4c6f62d48936973d71437f5c210cf8f66 [file] [log] [blame]
[email protected]bca34942012-09-05 18:23:251// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]50ae9f12013-08-29 18:03:225#include "components/variations/metrics_util.h"
[email protected]bca34942012-09-05 18:23:256
avi51ba3e692015-12-26 17:30:507#include <string.h>
8
[email protected]bca34942012-09-05 18:23:259#include "base/sha1.h"
10#include "base/sys_byteorder.h"
11
12namespace metrics {
13
thestig345fc9c52017-03-23 22:35:2414uint32_t HashName(base::StringPiece name) {
[email protected]bca34942012-09-05 18:23:2515 // SHA-1 is designed to produce a uniformly random spread in its output space,
16 // even for nearly-identical inputs.
17 unsigned char sha1_hash[base::kSHA1Length];
thestig345fc9c52017-03-23 22:35:2418 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.data()),
19 name.size(), sha1_hash);
[email protected]bca34942012-09-05 18:23:2520
avi5dd91f82015-12-25 22:30:4621 uint32_t bits;
mostynb470748ce2014-12-22 21:14:4622 static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required");
[email protected]bca34942012-09-05 18:23:2523 memcpy(&bits, sha1_hash, sizeof(bits));
24
25 return base::ByteSwapToLE32(bits);
26}
27
28} // namespace metrics