hashing.cc 853 B

12345678910111213141516171819202122232425262728
  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. #include "components/variations/hashing.h"
  5. #include <string.h>
  6. #include "base/hash/sha1.h"
  7. #include "base/sys_byteorder.h"
  8. namespace variations {
  9. uint32_t HashName(base::StringPiece name) {
  10. // SHA-1 is designed to produce a uniformly random spread in its output space,
  11. // even for nearly-identical inputs.
  12. unsigned char sha1_hash[base::kSHA1Length];
  13. base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(name.data()),
  14. name.size(), sha1_hash);
  15. uint32_t bits;
  16. static_assert(sizeof(bits) < sizeof(sha1_hash), "more data required");
  17. memcpy(&bits, sha1_hash, sizeof(bits));
  18. return base::ByteSwapToLE32(bits);
  19. }
  20. } // namespace variations