OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "platform/Crypto.h" | 5 #include "platform/Crypto.h" |
6 | 6 |
7 #include "public/platform/Platform.h" | 7 #include "public/platform/Platform.h" |
8 #include "public/platform/WebCrypto.h" | 8 #include "public/platform/WebCrypto.h" |
9 #include "public/platform/WebCryptoAlgorithm.h" | 9 #include "public/platform/WebCryptoAlgorithm.h" |
10 #include "wtf/PtrUtil.h" | |
11 #include <memory> | |
12 | 10 |
13 namespace blink { | 11 namespace blink { |
14 | 12 |
15 static WebCryptoAlgorithmId toWebCryptoAlgorithmId(HashAlgorithm algorithm) | 13 static WebCryptoAlgorithmId toWebCryptoAlgorithmId(HashAlgorithm algorithm) |
16 { | 14 { |
17 switch (algorithm) { | 15 switch (algorithm) { |
18 case HashAlgorithmSha1: | 16 case HashAlgorithmSha1: |
19 return WebCryptoAlgorithmIdSha1; | 17 return WebCryptoAlgorithmIdSha1; |
20 case HashAlgorithmSha256: | 18 case HashAlgorithmSha256: |
21 return WebCryptoAlgorithmIdSha256; | 19 return WebCryptoAlgorithmIdSha256; |
22 case HashAlgorithmSha384: | 20 case HashAlgorithmSha384: |
23 return WebCryptoAlgorithmIdSha384; | 21 return WebCryptoAlgorithmIdSha384; |
24 case HashAlgorithmSha512: | 22 case HashAlgorithmSha512: |
25 return WebCryptoAlgorithmIdSha512; | 23 return WebCryptoAlgorithmIdSha512; |
26 }; | 24 }; |
27 | 25 |
28 ASSERT_NOT_REACHED(); | 26 ASSERT_NOT_REACHED(); |
29 return WebCryptoAlgorithmIdSha256; | 27 return WebCryptoAlgorithmIdSha256; |
30 } | 28 } |
31 | 29 |
32 bool computeDigest(HashAlgorithm algorithm, const char* digestable, size_t lengt
h, DigestValue& digestResult) | 30 bool computeDigest(HashAlgorithm algorithm, const char* digestable, size_t lengt
h, DigestValue& digestResult) |
33 { | 31 { |
34 WebCryptoAlgorithmId algorithmId = toWebCryptoAlgorithmId(algorithm); | 32 WebCryptoAlgorithmId algorithmId = toWebCryptoAlgorithmId(algorithm); |
35 WebCrypto* crypto = Platform::current()->crypto(); | 33 WebCrypto* crypto = Platform::current()->crypto(); |
36 unsigned char* result; | 34 unsigned char* result; |
37 unsigned resultSize; | 35 unsigned resultSize; |
38 | 36 |
39 ASSERT(crypto); | 37 ASSERT(crypto); |
40 | 38 |
41 std::unique_ptr<WebCryptoDigestor> digestor = wrapUnique(crypto->createDiges
tor(algorithmId)); | 39 OwnPtr<WebCryptoDigestor> digestor = adoptPtr(crypto->createDigestor(algorit
hmId)); |
42 if (!digestor.get() || !digestor->consume(reinterpret_cast<const unsigned ch
ar*>(digestable), length) || !digestor->finish(result, resultSize)) | 40 if (!digestor.get() || !digestor->consume(reinterpret_cast<const unsigned ch
ar*>(digestable), length) || !digestor->finish(result, resultSize)) |
43 return false; | 41 return false; |
44 | 42 |
45 digestResult.append(static_cast<uint8_t*>(result), resultSize); | 43 digestResult.append(static_cast<uint8_t*>(result), resultSize); |
46 return true; | 44 return true; |
47 } | 45 } |
48 | 46 |
49 std::unique_ptr<WebCryptoDigestor> createDigestor(HashAlgorithm algorithm) | 47 PassOwnPtr<WebCryptoDigestor> createDigestor(HashAlgorithm algorithm) |
50 { | 48 { |
51 return wrapUnique(Platform::current()->crypto()->createDigestor(toWebCryptoA
lgorithmId(algorithm))); | 49 return adoptPtr(Platform::current()->crypto()->createDigestor(toWebCryptoAlg
orithmId(algorithm))); |
52 } | 50 } |
53 | 51 |
54 void finishDigestor(WebCryptoDigestor* digestor, DigestValue& digestResult) | 52 void finishDigestor(WebCryptoDigestor* digestor, DigestValue& digestResult) |
55 { | 53 { |
56 unsigned char* result = 0; | 54 unsigned char* result = 0; |
57 unsigned resultSize = 0; | 55 unsigned resultSize = 0; |
58 | 56 |
59 if (!digestor->finish(result, resultSize)) | 57 if (!digestor->finish(result, resultSize)) |
60 return; | 58 return; |
61 | 59 |
62 ASSERT(result); | 60 ASSERT(result); |
63 | 61 |
64 digestResult.append(static_cast<uint8_t*>(result), resultSize); | 62 digestResult.append(static_cast<uint8_t*>(result), resultSize); |
65 } | 63 } |
66 | 64 |
67 } // namespace blink | 65 } // namespace blink |
OLD | NEW |