Roll src/third_party/WebKit 29324ab:10b2b4a (svn 202547:202548)
[chromium-blink-merge.git] / net / base / hash_value.h
bloba9e0b63a8318b38cec720fe3bb88101644f86d91
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_
8 #include <string.h>
10 #include <string>
11 #include <vector>
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"
18 namespace net {
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];
32 enum HashValueTag {
33 HASH_VALUE_SHA1,
34 HASH_VALUE_SHA256,
37 class NET_EXPORT HashValue {
38 public:
39 explicit HashValue(HashValueTag tag) : tag(tag) {}
40 HashValue() : tag(HASH_VALUE_SHA1) {}
42 // Check for equality of hash values
43 // This function may have VARIABLE timing which leaks information
44 // about its inputs. For example it may exit early once a
45 // nonequal character is discovered. Thus, for security reasons
46 // this function MUST NOT be used with secret values (such as
47 // password hashes, MAC tags, etc.)
48 bool Equals(const HashValue& other) const;
50 // Serializes/Deserializes hashes in the form of
51 // <hash-name>"/"<base64-hash-value>
52 // (eg: "sha1/...")
53 // This format may be persisted to permanent storage, so
54 // care should be taken before changing the serialization.
56 // This format is used for:
57 // - net_internals display/setting public-key pins
58 // - logging public-key pins
59 // - serializing public-key pins
61 // Deserializes a HashValue from a string. On error, returns
62 // false and MAY change the contents of HashValue to contain invalid data.
63 bool FromString(const base::StringPiece input);
65 // Serializes the HashValue to a string. If an invalid HashValue
66 // is supplied (eg: an unknown hash tag), returns "unknown"/<base64>
67 std::string ToString() const;
69 size_t size() const;
70 unsigned char* data();
71 const unsigned char* data() const;
73 HashValueTag tag;
75 private:
76 union {
77 SHA1HashValue sha1;
78 SHA256HashValue sha256;
79 } fingerprint;
82 typedef std::vector<HashValue> HashValueVector;
85 class SHA1HashValueLessThan {
86 public:
87 bool operator()(const SHA1HashValue& lhs,
88 const SHA1HashValue& rhs) const {
89 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0;
93 class SHA256HashValueLessThan {
94 public:
95 bool operator()(const SHA256HashValue& lhs,
96 const SHA256HashValue& rhs) const {
97 return memcmp(lhs.data, rhs.data, sizeof(lhs.data)) < 0;
101 class HashValuesEqual {
102 public:
103 explicit HashValuesEqual(const HashValue& fingerprint) :
104 fingerprint_(fingerprint) {}
106 bool operator()(const HashValue& other) const {
107 return fingerprint_.Equals(other);
110 const HashValue& fingerprint_;
114 // IsSHA1HashInSortedArray returns true iff |hash| is in |array|, a sorted
115 // array of SHA1 hashes.
116 bool IsSHA1HashInSortedArray(const SHA1HashValue& hash,
117 const uint8_t* array,
118 size_t array_byte_len);
120 } // namespace net
122 #endif // NET_BASE_HASH_VALUE_H_