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