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_impl.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 void WebCryptoImpl::digest( | |
17 const WebKit::WebCryptoAlgorithm& algorithm, | |
18 const unsigned char* data, | |
19 size_t data_size, | |
20 WebKit::WebCryptoResult result) { | |
21 HASH_HashType hash_type = HASH_AlgNULL; | |
22 | |
23 switch (algorithm.id()) { | |
24 case WebKit::WebCryptoAlgorithmIdSha1: | |
25 hash_type = HASH_AlgSHA1; | |
26 break; | |
27 case WebKit::WebCryptoAlgorithmIdSha224: | |
28 hash_type = HASH_AlgSHA224; | |
29 break; | |
30 case WebKit::WebCryptoAlgorithmIdSha256: | |
31 hash_type = HASH_AlgSHA256; | |
32 break; | |
33 case WebKit::WebCryptoAlgorithmIdSha384: | |
34 hash_type = HASH_AlgSHA384; | |
35 break; | |
36 case WebKit::WebCryptoAlgorithmIdSha512: | |
37 hash_type = HASH_AlgSHA512; | |
38 break; | |
39 default: | |
40 // Not a digest algorithm. | |
41 result.completeWithError(); | |
42 return; | |
43 } | |
44 | |
45 crypto::EnsureNSSInit(); | |
46 | |
47 HASHContext* context = HASH_Create(hash_type); | |
48 if (!context) { | |
49 result.completeWithError(); | |
50 return; | |
51 } | |
52 | |
53 HASH_Begin(context); | |
54 | |
55 HASH_Update(context, data, data_size); | |
56 | |
57 size_t hash_result_length = HASH_ResultLenContext(context); | |
58 DCHECK(hash_result_length <= HASH_LENGTH_MAX); | |
jamesr
2013/08/23 01:27:40
DCHECK_LE()
Bryan Eyler
2013/08/23 17:45:31
Done.
| |
59 | |
60 WebKit::WebArrayBuffer buffer( | |
61 WebKit::WebArrayBuffer::create(hash_result_length, 1)); | |
62 | |
63 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data()); | |
64 | |
65 uint32 result_length = 0; | |
66 HASH_End(context, digest, &result_length, hash_result_length); | |
67 if (result_length != hash_result_length) { | |
68 result.completeWithError(); | |
69 } else { | |
70 result.completeWithBuffer(buffer); | |
71 } | |
72 | |
73 HASH_Destroy(context); | |
74 } | |
75 | |
76 } // namespace content | |
OLD | NEW |