Chromium Code Reviews| Index: content/renderer/webcrypto_impl.cc |
| diff --git a/content/renderer/webcrypto_impl.cc b/content/renderer/webcrypto_impl.cc |
| index 80c551240eeb141a4c5b305f9485e5df2c241b6e..c27647b8a6929bc6865abd3651fcf00f8ea2020f 100644 |
| --- a/content/renderer/webcrypto_impl.cc |
| +++ b/content/renderer/webcrypto_impl.cc |
| @@ -4,24 +4,85 @@ |
| #include "content/renderer/webcrypto_impl.h" |
| +#if defined(USE_NSS) |
| +#include <sechash.h> |
| +#endif |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
|
eroman
2013/08/22 02:25:05
unused; remove
Bryan Eyler
2013/08/22 18:51:03
Done.
|
| +#include "crypto/nss_util.h" |
| +#include "third_party/WebKit/public/platform/WebArrayBuffer.h" |
| #include "third_party/WebKit/public/platform/WebCryptoAlgorithm.h" |
| namespace content { |
| -WebKit::WebCryptoOperation* WebCryptoImpl::digest( |
| - const WebKit::WebCryptoAlgorithm& algorithm) { |
| +void WebCryptoImpl::digest( |
| + const WebKit::WebCryptoAlgorithm& algorithm, |
| + const unsigned char* data, |
| + size_t data_size, |
| + WebKit::WebCryptoResult result) { |
| +// TODO(bryaneyler): Also include these in OpenSSL build. Issue 267888. |
| +#if defined(USE_NSS) |
|
eroman
2013/08/22 02:25:05
Because this is quickly going to turn into a lot o
Bryan Eyler
2013/08/22 18:51:03
Done.
|
| + 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: |
| - // TODO(eroman): Implement. |
| - return NULL; |
| + hash_type = HASH_AlgSHA512; |
| + break; |
| default: |
| // Not a digest algorithm. |
| - return NULL; |
| + result.completeWithError(); |
| + return; |
| } |
| + |
| + crypto::EnsureNSSInit(); |
| + |
| + HASHContext* context = HASH_Create(hash_type); |
| + if (!context) { |
| + LOG(ERROR) << "Could not create digest context for hash algorithm: " |
|
eroman
2013/08/22 02:25:05
I don't think there is value to having these LOG(E
Bryan Eyler
2013/08/22 18:51:03
Done.
|
| + << hash_type; |
| + result.completeWithError(); |
| + return; |
| + } |
| + |
| + HASH_Begin(context); |
| + |
| + HASH_Update(context, data, data_size); |
| + |
| + unsigned int hash_result_length = HASH_ResultLenContext(context); |
|
jamesr
2013/08/22 02:31:01
i think size_t is a better type, but you don't wan
Bryan Eyler
2013/08/22 18:51:03
Changed to size_t. FWIW, I was trying to match th
|
| + WebKit::WebArrayBuffer buffer( |
| + WebKit::WebArrayBuffer::create(hash_result_length, 1)); |
| + |
| + unsigned char* digest = reinterpret_cast<unsigned char*>(buffer.data()); |
| + DCHECK(digest); |
|
eroman
2013/08/22 02:25:05
I think the only situation when buffer.data() can
jamesr
2013/08/22 02:31:01
I'm not sure this DCHECK makes sense. WebArrayBuff
Bryan Eyler
2013/08/22 18:51:03
DCHECK changed to sanity check hash_result_length.
|
| + |
| + unsigned int result_length = 0; |
|
jamesr
2013/08/22 02:31:01
please use a better type
Bryan Eyler
2013/08/22 18:51:03
I've made this uint32 for lack of a better type.
|
| + HASH_End(context, digest, &result_length, hash_result_length); |
| + if (result_length != hash_result_length) { |
| + LOG(ERROR) << "Result length invalid; expected " << hash_result_length |
|
eroman
2013/08/22 02:25:05
LOG(ERROR) have very little usefulness in my exper
Bryan Eyler
2013/08/22 18:51:03
Done.
|
| + << ", got " << result_length; |
| + result.completeWithError(); |
| + return; |
| + } |
| + |
| + result.completeWithBuffer(buffer); |
| + |
| +#else |
| + // No way to process. |
| + result.completeWithError(); |
| +#endif |
| } |
| } // namespace content |