OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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. | |
4 | |
5 #include "content/renderer/webcrypto_sha_digest.h" | |
6 | |
7 #include <sechash.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "crypto/nss_util.h" | |
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h" | |
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" | |
13 | |
14 namespace content { | |
15 | |
16 WebCryptoSHADigest::WebCryptoSHADigest( | |
17 WebKit::WebCryptoOperationResult& result) | |
eroman
2013/08/02 01:42:58
Can you make this parameter const?
Bryan Eyler
2013/08/02 21:08:08
Done.
| |
18 : result_(result), | |
19 context_(NULL), | |
20 hash_result_length_(0) { | |
21 } | |
22 | |
23 WebCryptoSHADigest::~WebCryptoSHADigest() { | |
24 if (context_) { | |
25 HASH_Destroy(context_); | |
26 } | |
27 } | |
28 | |
29 bool WebCryptoSHADigest::Initialize( | |
30 const WebKit::WebCryptoAlgorithm& algorithm) { | |
31 crypto::EnsureNSSInit(); | |
32 | |
33 HASH_HashType hash_type = HASH_AlgNULL; | |
34 | |
35 switch (algorithm.id()) { | |
36 case WebKit::WebCryptoAlgorithmIdSha1: | |
37 hash_type = HASH_AlgSHA1; | |
38 hash_result_length_ = SHA1_LENGTH; | |
39 break; | |
40 case WebKit::WebCryptoAlgorithmIdSha224: | |
41 hash_type = HASH_AlgSHA224; | |
42 hash_result_length_ = SHA224_LENGTH; | |
43 break; | |
44 case WebKit::WebCryptoAlgorithmIdSha256: | |
45 hash_type = HASH_AlgSHA256; | |
46 hash_result_length_ = SHA256_LENGTH; | |
47 break; | |
48 case WebKit::WebCryptoAlgorithmIdSha384: | |
49 hash_type = HASH_AlgSHA384; | |
50 hash_result_length_ = SHA384_LENGTH; | |
51 break; | |
52 case WebKit::WebCryptoAlgorithmIdSha512: | |
53 hash_type = HASH_AlgSHA512; | |
54 hash_result_length_ = SHA512_LENGTH; | |
55 break; | |
56 default: | |
57 NOTREACHED(); | |
58 } | |
59 | |
60 context_ = HASH_Create(hash_type); | |
61 if (!context_) { | |
62 LOG(ERROR) << "Could not create digest context for hash algorithm: " | |
63 << hash_type; | |
64 return false; | |
65 } | |
66 | |
67 HASH_Begin(context_); | |
68 | |
69 return true; | |
70 } | |
71 | |
72 void WebCryptoSHADigest::process(const unsigned char* bytes, size_t size) { | |
73 DCHECK(context_); | |
74 HASH_Update(context_, bytes, size); | |
75 } | |
76 | |
77 void WebCryptoSHADigest::abort() { | |
78 delete this; | |
79 } | |
80 | |
81 void WebCryptoSHADigest::finish() { | |
82 DCHECK(context_); | |
83 | |
84 WebKit::WebArrayBuffer buffer( | |
85 WebKit::WebArrayBuffer::create(hash_result_length_, 1)); | |
86 | |
87 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data()); | |
88 DCHECK(digest); | |
89 | |
90 unsigned int result_length = 0; | |
91 HASH_End(context_, digest, &result_length, hash_result_length_); | |
92 if (result_length != hash_result_length_) { | |
93 LOG(ERROR) << "Result length invalid; expected " << hash_result_length_ | |
94 << ", got " << result_length; | |
95 result_.completeWithError(); | |
96 delete this; | |
97 return; | |
98 } | |
99 | |
100 result_.completeWithArrayBuffer(buffer); | |
101 | |
102 delete this; | |
103 } | |
104 | |
105 } // namespace content | |
OLD | NEW |