Index: chrome/browser/profile_resetter/jtl_foundation.cc |
diff --git a/chrome/browser/profile_resetter/jtl_foundation.cc b/chrome/browser/profile_resetter/jtl_foundation.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0739040cc5e0d2abd1e50ac0082bafb75ef445d6 |
--- /dev/null |
+++ b/chrome/browser/profile_resetter/jtl_foundation.cc |
@@ -0,0 +1,48 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/profile_resetter/jtl_foundation.h" |
+ |
+#include "base/logging.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+ |
+namespace jtl_foundation { |
+ |
+Hasher::Hasher(const std::string& seed) : hmac_(crypto::HMAC::SHA256) { |
+ if (!hmac_.Init(seed)) |
+ NOTREACHED(); |
+} |
+ |
+Hasher::~Hasher() {} |
+ |
+std::string Hasher::GetHash(const std::string& input) const { |
+ if (cached_hashes_.find(input) == cached_hashes_.end()) { |
+ // Calculate value. |
+ unsigned char digest[kHashSizeInBytes]; |
+ if (!hmac_.Sign(input, digest, arraysize(digest))) { |
+ NOTREACHED(); |
+ return std::string(); |
+ } |
+ // Instead of using the full SHA256, we only use the hex encoding of the |
+ // first 16 bytes. |
+ cached_hashes_[input] = base::HexEncode(digest, kHashSizeInBytes / 2); |
+ DCHECK_EQ(kHashSizeInBytes, cached_hashes_[input].size()); |
+ } |
+ return cached_hashes_[input]; |
+} |
+ |
+// static |
+bool Hasher::IsHash(const std::string& maybe_hash) { |
+ if (maybe_hash.size() != kHashSizeInBytes) |
+ return false; |
+ for (std::string::const_iterator it = maybe_hash.begin(); |
+ it != maybe_hash.end(); ++it) { |
+ if (!IsHexDigit(*it)) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+} // namespace jtl_foundation |