OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #include "content/renderer/webcrypto_impl.h" |
| 6 |
| 7 #include <sechash.h> |
| 8 |
| 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" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 bool WebCryptoImpl::digestInternal( |
| 17 const WebKit::WebCryptoAlgorithm& algorithm, |
| 18 const unsigned char* data, |
| 19 size_t data_size, |
| 20 WebKit::WebArrayBuffer* buffer) { |
| 21 HASH_HashType hash_type = HASH_AlgNULL; |
| 22 |
| 23 switch (algorithm.id()) { |
| 24 case WebKit::WebCryptoAlgorithmIdSha1: |
| 25 hash_type = HASH_AlgSHA1; |
| 26 break; |
| 27 case WebKit::WebCryptoAlgorithmIdSha224: |
| 28 hash_type = HASH_AlgSHA224; |
| 29 break; |
| 30 case WebKit::WebCryptoAlgorithmIdSha256: |
| 31 hash_type = HASH_AlgSHA256; |
| 32 break; |
| 33 case WebKit::WebCryptoAlgorithmIdSha384: |
| 34 hash_type = HASH_AlgSHA384; |
| 35 break; |
| 36 case WebKit::WebCryptoAlgorithmIdSha512: |
| 37 hash_type = HASH_AlgSHA512; |
| 38 break; |
| 39 default: |
| 40 // Not a digest algorithm. |
| 41 return false; |
| 42 } |
| 43 |
| 44 crypto::EnsureNSSInit(); |
| 45 |
| 46 HASHContext* context = HASH_Create(hash_type); |
| 47 if (!context) { |
| 48 return false; |
| 49 } |
| 50 |
| 51 HASH_Begin(context); |
| 52 |
| 53 HASH_Update(context, data, data_size); |
| 54 |
| 55 size_t hash_result_length = HASH_ResultLenContext(context); |
| 56 DCHECK_LE(hash_result_length, static_cast<size_t>(HASH_LENGTH_MAX)); |
| 57 |
| 58 *buffer = WebKit::WebArrayBuffer::create(hash_result_length, 1); |
| 59 |
| 60 unsigned char* digest = reinterpret_cast<unsigned char*>(buffer->data()); |
| 61 |
| 62 uint32 result_length = 0; |
| 63 HASH_End(context, digest, &result_length, hash_result_length); |
| 64 |
| 65 HASH_Destroy(context); |
| 66 |
| 67 return result_length == hash_result_length; |
| 68 } |
| 69 |
| 70 } // namespace content |
OLD | NEW |