[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 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 5 | #include "components/variations/metrics_util.h" |
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 6 | |
| 7 | #include "base/sha1.h" |
| 8 | #include "base/sys_byteorder.h" |
| 9 | |
| 10 | namespace metrics { |
| 11 | |
| 12 | uint32 HashName(const std::string& name) { |
| 13 | // SHA-1 is designed to produce a uniformly random spread in its output space, |
| 14 | // even for nearly-identical inputs. |
| 15 | unsigned char sha1_hash[base::kSHA1Length]; |
| 16 | base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.c_str()), |
| 17 | name.size(), |
| 18 | sha1_hash); |
| 19 | |
| 20 | uint32 bits; |
mostynb | 470748ce | 2014-12-22 21:14:46 | [diff] [blame^] | 21 | static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required"); |
[email protected] | bca3494 | 2012-09-05 18:23:25 | [diff] [blame] | 22 | memcpy(&bits, sha1_hash, sizeof(bits)); |
| 23 | |
| 24 | return base::ByteSwapToLE32(bits); |
| 25 | } |
| 26 | |
| 27 | } // namespace metrics |