blob: 61aa3a69dcb9ef8816003722a1d042ec0247b373 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/webcrypto_impl.h"
#include <sechash.h>
#include "base/logging.h"
#include "crypto/nss_util.h"
#include "third_party/WebKit/public/platform/WebArrayBuffer.h"
#include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h"
namespace content {
bool WebCryptoImpl::digestInternal(
const WebKit::WebCryptoAlgorithm& algorithm,
const unsigned char* data,
size_t data_size,
WebKit::WebArrayBuffer* buffer) {
HASH_HashType hash_type = HASH_AlgNULL;
switch (algorithm.id()) {
case WebKit::WebCryptoAlgorithmIdSha1:
hash_type = HASH_AlgSHA1;
break;
case WebKit::WebCryptoAlgorithmIdSha224:
hash_type = HASH_AlgSHA224;
break;
case WebKit::WebCryptoAlgorithmIdSha256:
hash_type = HASH_AlgSHA256;
break;
case WebKit::WebCryptoAlgorithmIdSha384:
hash_type = HASH_AlgSHA384;
break;
case WebKit::WebCryptoAlgorithmIdSha512:
hash_type = HASH_AlgSHA512;
break;
default:
// Not a digest algorithm.
return false;
}
crypto::EnsureNSSInit();
HASHContext* context = HASH_Create(hash_type);
if (!context) {
return false;
}
HASH_Begin(context);
HASH_Update(context, data, data_size);
size_t hash_result_length = HASH_ResultLenContext(context);
DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX));
*buffer = WebKit::WebArrayBuffer::create(hash_result_length, 1);
unsigned char* digest = reinterpret_cast<unsigned char*>(buffer->data());
uint32 result_length = 0;
HASH_End(context, digest, &result_length, hash_result_length);
HASH_Destroy(context);
return result_length == hash_result_length;
}
} // namespace content
|