[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 1 | // 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 Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 5 | #include "components/variations/hashing.h" |
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 6 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 7 | #include <string.h> |
8 | |||||
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 9 | #include "base/sha1.h" |
10 | #include "base/sys_byteorder.h" | ||||
11 | |||||
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 12 | namespace variations { |
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 13 | |
thestig | 345fc9c5 | 2017-03-23 22:35:24 | [diff] [blame] | 14 | uint32_t HashName(base::StringPiece name) { |
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 15 | // 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]; | ||||
thestig | 345fc9c5 | 2017-03-23 22:35:24 | [diff] [blame] | 18 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.data()), |
19 | name.size(), sha1_hash); | ||||
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 20 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 21 | uint32_t bits; |
mostynb | 470748ce | 2014-12-22 21:14:46 | [diff] [blame] | 22 | static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required"); |
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 23 | memcpy(&bits, sha1_hash, sizeof(bits)); |
24 | |||||
25 | return base::ByteSwapToLE32(bits); | ||||
26 | } | ||||
27 | |||||
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 28 | } // namespace variations |