Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Unified Diff: net/tools/domain_security_preload_generator/spki_hash.cc

Issue 2551153003: Add static domain security state generator tool. (Closed)
Patch Set: fix base64 issue and accidental replace. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/tools/domain_security_preload_generator/spki_hash.cc
diff --git a/net/tools/domain_security_preload_generator/spki_hash.cc b/net/tools/domain_security_preload_generator/spki_hash.cc
new file mode 100644
index 0000000000000000000000000000000000000000..aee74de0135fb20ecb7444cbbbd0c8ba6ae2d858
--- /dev/null
+++ b/net/tools/domain_security_preload_generator/spki_hash.cc
@@ -0,0 +1,61 @@
+// Copyright 2016 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 "net/tools/domain_security_preload_generator/spki_hash.h"
+
+#include <string>
+
+#include "base/base64.h"
+#include "base/logging.h"
+#include "base/strings/string_util.h"
+#include "third_party/boringssl/src/include/openssl/evp.h"
+
+namespace net {
+
+SPKIHash::SPKIHash() {}
+
+SPKIHash::~SPKIHash() {}
+
+bool SPKIHash::FromString(const std::string& hash_string) {
+ std::string base64_string;
+
+ if (!base::StartsWith(hash_string, "sha256/",
+ base::CompareCase::INSENSITIVE_ASCII)) {
+ return false;
+ }
+ base64_string = hash_string.substr(7);
+
+ std::string decoded;
+ if (!base::Base64Decode(base64_string, &decoded)) {
+ return false;
+ }
+
+ if (decoded.size() != size()) {
+ return false;
+ }
+
+ memcpy(data_, decoded.data(), decoded.size());
+ return true;
+}
+
+bool SPKIHash::CalculateFromBytes(const uint8_t* input, size_t input_length) {
+ bssl::ScopedEVP_MD_CTX context;
+ if (!EVP_DigestInit(context.get(), EVP_sha256())) {
agl 2016/12/06 18:51:36 include "third_party/boringssl/src/include/openssl
martijnc 2016/12/07 22:37:54 Done.
+ return false;
+ }
+
+ if (!EVP_DigestUpdate(context.get(), input, input_length)) {
+ return false;
+ }
+
+ uint32_t digest_len;
+ if (!EVP_DigestFinal(context.get(), data_, &digest_len)) {
+ return false;
+ }
+
+ DCHECK(digest_len == size());
+ return true;
+}
+
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698