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
3 // found in the LICENSE file.
6 #include "platform/Crypto.h"
8 #include "public/platform/Platform.h"
9 #include "public/platform/WebCrypto.h"
10 #include "public/platform/WebCryptoAlgorithm.h"
14 static WebCryptoAlgorithmId
toWebCryptoAlgorithmId(HashAlgorithm algorithm
)
17 case HashAlgorithmSha1
:
18 return WebCryptoAlgorithmIdSha1
;
19 case HashAlgorithmSha256
:
20 return WebCryptoAlgorithmIdSha256
;
21 case HashAlgorithmSha384
:
22 return WebCryptoAlgorithmIdSha384
;
23 case HashAlgorithmSha512
:
24 return WebCryptoAlgorithmIdSha512
;
28 return WebCryptoAlgorithmIdSha256
;
31 bool computeDigest(HashAlgorithm algorithm
, const char* digestable
, size_t length
, DigestValue
& digestResult
)
33 WebCryptoAlgorithmId algorithmId
= toWebCryptoAlgorithmId(algorithm
);
34 WebCrypto
* crypto
= Platform::current()->crypto();
35 unsigned char* result
;
40 OwnPtr
<WebCryptoDigestor
> digestor
= adoptPtr(crypto
->createDigestor(algorithmId
));
41 if (!digestor
.get() || !digestor
->consume(reinterpret_cast<const unsigned char*>(digestable
), length
) || !digestor
->finish(result
, resultSize
))
44 digestResult
.append(static_cast<uint8_t*>(result
), resultSize
);
48 PassOwnPtr
<WebCryptoDigestor
> createDigestor(HashAlgorithm algorithm
)
50 return adoptPtr(Platform::current()->crypto()->createDigestor(toWebCryptoAlgorithmId(algorithm
)));
53 void finishDigestor(WebCryptoDigestor
* digestor
, DigestValue
& digestResult
)
55 unsigned char* result
= 0;
56 unsigned resultSize
= 0;
58 if (!digestor
->finish(result
, resultSize
))
63 digestResult
.append(static_cast<uint8_t*>(result
), resultSize
);