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/WebArrayBuffer.h"
10 #include "public/platform/WebCrypto.h"
11 #include "public/platform/WebCryptoAlgorithm.h"
15 static WebCryptoAlgorithmId toWebCryptoAlgorithmId(HashAlgorithm algorithm)
18 case HashAlgorithmSha1:
19 return WebCryptoAlgorithmIdSha1;
20 case HashAlgorithmSha256:
21 return WebCryptoAlgorithmIdSha256;
22 case HashAlgorithmSha384:
23 return WebCryptoAlgorithmIdSha384;
24 case HashAlgorithmSha512:
25 return WebCryptoAlgorithmIdSha512;
29 return WebCryptoAlgorithmIdSha256;
32 bool computeDigest(HashAlgorithm algorithm, const char* digestable, size_t length, DigestValue& digestResult)
34 WebCryptoAlgorithmId algorithmId = toWebCryptoAlgorithmId(algorithm);
35 WebCrypto* crypto = Platform::current()->crypto();
36 unsigned char* result;
41 OwnPtr<WebCryptoDigestor> digestor = adoptPtr(crypto->createDigestor(algorithmId));
42 if (!digestor.get() || !digestor->consume(reinterpret_cast<const unsigned char*>(digestable), length) || !digestor->finish(result, resultSize))
45 digestResult.append(static_cast<uint8_t*>(result), resultSize);
49 PassOwnPtr<WebCryptoDigestor> createDigestor(HashAlgorithm algorithm)
51 return adoptPtr(Platform::current()->crypto()->createDigestor(toWebCryptoAlgorithmId(algorithm)));
54 void finishDigestor(WebCryptoDigestor* digestor, DigestValue& digestResult)
56 unsigned char* result = 0;
57 unsigned resultSize = 0;
59 if (!digestor->finish(result, resultSize))
64 digestResult.append(static_cast<uint8_t*>(result), resultSize);