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.
5 #include "net/base/hash_value.h"
7 #include "base/base64.h"
8 #include "base/logging.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
17 // CompareSHA1Hashes is a helper function for using bsearch() with an array of
19 int CompareSHA1Hashes(const void* a
, const void* b
) {
20 return memcmp(a
, b
, base::kSHA1Length
);
26 bool SHA1HashValue::Equals(const SHA1HashValue
& other
) const {
27 return memcmp(data
, other
.data
, sizeof(data
)) == 0;
30 bool SHA256HashValue::Equals(const SHA256HashValue
& other
) const {
31 return memcmp(data
, other
.data
, sizeof(data
)) == 0;
34 bool HashValue::Equals(const HashValue
& other
) const {
39 return fingerprint
.sha1
.Equals(other
.fingerprint
.sha1
);
40 case HASH_VALUE_SHA256
:
41 return fingerprint
.sha256
.Equals(other
.fingerprint
.sha256
);
43 NOTREACHED() << "Unknown HashValueTag " << tag
;
48 bool HashValue::FromString(const base::StringPiece value
) {
49 base::StringPiece base64_str
;
50 if (value
.starts_with("sha1/")) {
51 tag
= HASH_VALUE_SHA1
;
52 base64_str
= value
.substr(5);
53 } else if (value
.starts_with("sha256/")) {
54 tag
= HASH_VALUE_SHA256
;
55 base64_str
= value
.substr(7);
61 if (!base::Base64Decode(base64_str
, &decoded
) || decoded
.size() != size())
64 memcpy(data(), decoded
.data(), size());
68 std::string
HashValue::ToString() const {
69 std::string base64_str
;
70 base::Base64Encode(base::StringPiece(reinterpret_cast<const char*>(data()),
71 size()), &base64_str
);
74 return std::string("sha1/") + base64_str
;
75 case HASH_VALUE_SHA256
:
76 return std::string("sha256/") + base64_str
;
78 NOTREACHED() << "Unknown HashValueTag " << tag
;
79 return std::string("unknown/" + base64_str
);
83 size_t HashValue::size() const {
86 return sizeof(fingerprint
.sha1
.data
);
87 case HASH_VALUE_SHA256
:
88 return sizeof(fingerprint
.sha256
.data
);
90 NOTREACHED() << "Unknown HashValueTag " << tag
;
91 // While an invalid tag should not happen, return a non-zero length
92 // to avoid compiler warnings when the result of size() is
93 // used with functions like memset.
94 return sizeof(fingerprint
.sha1
.data
);
98 unsigned char* HashValue::data() {
99 return const_cast<unsigned char*>(const_cast<const HashValue
*>(this)->data());
102 const unsigned char* HashValue::data() const {
104 case HASH_VALUE_SHA1
:
105 return fingerprint
.sha1
.data
;
106 case HASH_VALUE_SHA256
:
107 return fingerprint
.sha256
.data
;
109 NOTREACHED() << "Unknown HashValueTag " << tag
;
114 bool IsSHA1HashInSortedArray(const SHA1HashValue
& hash
,
116 size_t array_byte_len
) {
117 DCHECK_EQ(0u, array_byte_len
% base::kSHA1Length
);
118 const size_t arraylen
= array_byte_len
/ base::kSHA1Length
;
119 return NULL
!= bsearch(hash
.data
, array
, arraylen
, base::kSHA1Length
,