OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/secure_hash.h" | 5 #include "crypto/secure_hash.h" |
6 | 6 |
7 #include <openssl/mem.h> | 7 #include <openssl/mem.h> |
8 #include <openssl/sha.h> | 8 #include <openssl/sha.h> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 void Update(const void* input, size_t len) override { | 33 void Update(const void* input, size_t len) override { |
34 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); | 34 SHA256_Update(&ctx_, static_cast<const unsigned char*>(input), len); |
35 } | 35 } |
36 | 36 |
37 void Finish(void* output, size_t len) override { | 37 void Finish(void* output, size_t len) override { |
38 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( | 38 ScopedOpenSSLSafeSizeBuffer<SHA256_DIGEST_LENGTH> result( |
39 static_cast<unsigned char*>(output), len); | 39 static_cast<unsigned char*>(output), len); |
40 SHA256_Final(result.safe_buffer(), &ctx_); | 40 SHA256_Final(result.safe_buffer(), &ctx_); |
41 } | 41 } |
42 | 42 |
43 bool Serialize(Pickle* pickle) override; | 43 bool Serialize(base::Pickle* pickle) override; |
44 bool Deserialize(PickleIterator* data_iterator) override; | 44 bool Deserialize(base::PickleIterator* data_iterator) override; |
45 | 45 |
46 private: | 46 private: |
47 SHA256_CTX ctx_; | 47 SHA256_CTX ctx_; |
48 }; | 48 }; |
49 | 49 |
50 bool SecureHashSHA256OpenSSL::Serialize(Pickle* pickle) { | 50 bool SecureHashSHA256OpenSSL::Serialize(base::Pickle* pickle) { |
51 if (!pickle) | 51 if (!pickle) |
52 return false; | 52 return false; |
53 | 53 |
54 if (!pickle->WriteInt(kSecureHashVersion) || | 54 if (!pickle->WriteInt(kSecureHashVersion) || |
55 !pickle->WriteString(kSHA256Descriptor) || | 55 !pickle->WriteString(kSHA256Descriptor) || |
56 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) { | 56 !pickle->WriteBytes(&ctx_, sizeof(ctx_))) { |
57 return false; | 57 return false; |
58 } | 58 } |
59 | 59 |
60 return true; | 60 return true; |
61 } | 61 } |
62 | 62 |
63 bool SecureHashSHA256OpenSSL::Deserialize(PickleIterator* data_iterator) { | 63 bool SecureHashSHA256OpenSSL::Deserialize(base::PickleIterator* data_iterator) { |
64 if (!data_iterator) | 64 if (!data_iterator) |
65 return false; | 65 return false; |
66 | 66 |
67 int version; | 67 int version; |
68 if (!data_iterator->ReadInt(&version)) | 68 if (!data_iterator->ReadInt(&version)) |
69 return false; | 69 return false; |
70 | 70 |
71 if (version > kSecureHashVersion) | 71 if (version > kSecureHashVersion) |
72 return false; // We don't know how to deal with this. | 72 return false; // We don't know how to deal with this. |
73 | 73 |
(...skipping 19 matching lines...) Expand all Loading... |
93 switch (algorithm) { | 93 switch (algorithm) { |
94 case SHA256: | 94 case SHA256: |
95 return new SecureHashSHA256OpenSSL(); | 95 return new SecureHashSHA256OpenSSL(); |
96 default: | 96 default: |
97 NOTIMPLEMENTED(); | 97 NOTIMPLEMENTED(); |
98 return NULL; | 98 return NULL; |
99 } | 99 } |
100 } | 100 } |
101 | 101 |
102 } // namespace crypto | 102 } // namespace crypto |
OLD | NEW |