Remove unused Device Motion IPC messages.
[chromium-blink-merge.git] / content / renderer / webcrypto_impl_nss.cc
blob71bd11034423ac5bff7b6c7ea41105c96728578a
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"
7 #include <sechash.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"
14 namespace content {
16 void WebCryptoImpl::Init() {
17 crypto::EnsureNSSInit();
20 bool WebCryptoImpl::DigestInternal(
21 const WebKit::WebCryptoAlgorithm& algorithm,
22 const unsigned char* data,
23 unsigned data_size,
24 WebKit::WebArrayBuffer* buffer) {
25 HASH_HashType hash_type = HASH_AlgNULL;
27 switch (algorithm.id()) {
28 case WebKit::WebCryptoAlgorithmIdSha1:
29 hash_type = HASH_AlgSHA1;
30 break;
31 case WebKit::WebCryptoAlgorithmIdSha224:
32 hash_type = HASH_AlgSHA224;
33 break;
34 case WebKit::WebCryptoAlgorithmIdSha256:
35 hash_type = HASH_AlgSHA256;
36 break;
37 case WebKit::WebCryptoAlgorithmIdSha384:
38 hash_type = HASH_AlgSHA384;
39 break;
40 case WebKit::WebCryptoAlgorithmIdSha512:
41 hash_type = HASH_AlgSHA512;
42 break;
43 default:
44 // Not a digest algorithm.
45 return false;
48 HASHContext* context = HASH_Create(hash_type);
49 if (!context) {
50 return false;
53 HASH_Begin(context);
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