blob: 3383201f83cfc37dd5b64082337a122f5b4ce9cf [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
Alexei Svitkine9de32cb2018-02-06 20:21:215#include "components/variations/hashing.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
Alexei Svitkine9de32cb2018-02-06 20:21:2112namespace variations {
[email protected]bca34942012-09-05 18:23:2513
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
Alexei Svitkine9de32cb2018-02-06 20:21:2128} // namespace variations