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 #ifndef NET_BASE_HASH_VALUE_H_
6 #define NET_BASE_HASH_VALUE_H_
13 #include "base/basictypes.h"
14 #include "base/strings/string_piece.h"
15 #include "build/build_config.h"
16 #include "net/base/net_export.h"
20 struct NET_EXPORT SHA1HashValue
{
21 bool Equals(const SHA1HashValue
& other
) const;
23 unsigned char data
[20];
26 struct NET_EXPORT SHA256HashValue
{
27 bool Equals(const SHA256HashValue
& other
) const;
29 unsigned char data
[32];
36 // This must always be last.
40 class NET_EXPORT HashValue
{
42 explicit HashValue(HashValueTag tag
) : tag(tag
) {}
43 HashValue() : tag(HASH_VALUE_SHA1
) {}
45 // Check for equality of hash values
46 // This function may have VARIABLE timing which leaks information
47 // about its inputs. For example it may exit early once a
48 // nonequal character is discovered. Thus, for security reasons
49 // this function MUST NOT be used with secret values (such as
50 // password hashes, MAC tags, etc.)
51 bool Equals(const HashValue
& other
) const;
53 // Serializes/Deserializes hashes in the form of
54 // <hash-name>"/"<base64-hash-value>
56 // This format may be persisted to permanent storage, so
57 // care should be taken before changing the serialization.
59 // This format is used for:
60 // - net_internals display/setting public-key pins
61 // - logging public-key pins
62 // - serializing public-key pins
64 // Deserializes a HashValue from a string. On error, returns
65 // false and MAY change the contents of HashValue to contain invalid data.
66 bool FromString(const base::StringPiece input
);
68 // Serializes the HashValue to a string. If an invalid HashValue
69 // is supplied (eg: an unknown hash tag), returns "unknown"/<base64>
70 std::string
ToString() const;
73 unsigned char* data();
74 const unsigned char* data() const;
81 SHA256HashValue sha256
;
85 typedef std::vector
<HashValue
> HashValueVector
;
88 class SHA1HashValueLessThan
{
90 bool operator()(const SHA1HashValue
& lhs
,
91 const SHA1HashValue
& rhs
) const {
92 return memcmp(lhs
.data
, rhs
.data
, sizeof(lhs
.data
)) < 0;
96 class SHA256HashValueLessThan
{
98 bool operator()(const SHA256HashValue
& lhs
,
99 const SHA256HashValue
& rhs
) const {
100 return memcmp(lhs
.data
, rhs
.data
, sizeof(lhs
.data
)) < 0;
104 class HashValuesEqual
{
106 explicit HashValuesEqual(const HashValue
& fingerprint
) :
107 fingerprint_(fingerprint
) {}
109 bool operator()(const HashValue
& other
) const {
110 return fingerprint_
.Equals(other
);
113 const HashValue
& fingerprint_
;
117 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted
118 // array of SHA1 hashes.
119 bool IsSHA1HashInSortedArray(const SHA1HashValue
& hash
,
121 size_t array_byte_len
);
125 #endif // NET_BASE_HASH_VALUE_H_