1 // Copyright 2013 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 "content/renderer/webcrypto_impl.h"
9 #include "base/logging.h"
10 #include "crypto/nss_util.h"
11 #include "third_party/WebKit/public/platform/WebArrayBuffer.h"
12 #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
16 void WebCryptoImpl::Init() {
17 crypto::EnsureNSSInit();
20 bool WebCryptoImpl::DigestInternal(
21 const WebKit::WebCryptoAlgorithm
& algorithm
,
22 const unsigned char* data
,
24 WebKit::WebArrayBuffer
* buffer
) {
25 HASH_HashType hash_type
= HASH_AlgNULL
;
27 switch (algorithm
.id()) {
28 case WebKit::WebCryptoAlgorithmIdSha1
:
29 hash_type
= HASH_AlgSHA1
;
31 case WebKit::WebCryptoAlgorithmIdSha224
:
32 hash_type
= HASH_AlgSHA224
;
34 case WebKit::WebCryptoAlgorithmIdSha256
:
35 hash_type
= HASH_AlgSHA256
;
37 case WebKit::WebCryptoAlgorithmIdSha384
:
38 hash_type
= HASH_AlgSHA384
;
40 case WebKit::WebCryptoAlgorithmIdSha512
:
41 hash_type
= HASH_AlgSHA512
;
44 // Not a digest algorithm.
48 HASHContext
* context
= HASH_Create(hash_type
);
55 HASH_Update(context
, data
, data_size
);
57 size_t hash_result_length
= HASH_ResultLenContext(context
);
58 DCHECK_LE(hash_result_length
, static_cast<size_t>(HASH_LENGTH_MAX
));
60 *buffer
= WebKit::WebArrayBuffer::create(hash_result_length
, 1);
62 unsigned char* digest
= reinterpret_cast<unsigned char*>(buffer
->data());
64 uint32 result_length
= 0;
65 HASH_End(context
, digest
, &result_length
, hash_result_length
);
67 HASH_Destroy(context
);
69 return result_length
== hash_result_length
;
72 } // namespace content