Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / platform / Crypto.cpp
blobef49ad8032ae36fa5ac238b3b7fdb3f273feec9b
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.
5 #include "config.h"
6 #include "platform/Crypto.h"
8 #include "public/platform/Platform.h"
9 #include "public/platform/WebCrypto.h"
10 #include "public/platform/WebCryptoAlgorithm.h"
12 namespace blink {
14 static WebCryptoAlgorithmId toWebCryptoAlgorithmId(HashAlgorithm algorithm)
16 switch (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;
27 ASSERT_NOT_REACHED();
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;
36 unsigned resultSize;
38 ASSERT(crypto);
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))
42 return false;
44 digestResult.append(static_cast<uint8_t*>(result), resultSize);
45 return true;
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))
59 return;
61 ASSERT(result);
63 digestResult.append(static_cast<uint8_t*>(result), resultSize);
66 } // namespace blink