| // Copyright 2014 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 "platform/Crypto.h" | 
 |  | 
 | #include "public/platform/Platform.h" | 
 | #include "public/platform/WebCrypto.h" | 
 | #include "public/platform/WebCryptoAlgorithm.h" | 
 | #include <memory> | 
 |  | 
 | namespace blink { | 
 |  | 
 | static WebCryptoAlgorithmId ToWebCryptoAlgorithmId(HashAlgorithm algorithm) { | 
 |   switch (algorithm) { | 
 |     case kHashAlgorithmSha1: | 
 |       return kWebCryptoAlgorithmIdSha1; | 
 |     case kHashAlgorithmSha256: | 
 |       return kWebCryptoAlgorithmIdSha256; | 
 |     case kHashAlgorithmSha384: | 
 |       return kWebCryptoAlgorithmIdSha384; | 
 |     case kHashAlgorithmSha512: | 
 |       return kWebCryptoAlgorithmIdSha512; | 
 |   }; | 
 |  | 
 |   ASSERT_NOT_REACHED(); | 
 |   return kWebCryptoAlgorithmIdSha256; | 
 | } | 
 |  | 
 | bool ComputeDigest(HashAlgorithm algorithm, | 
 |                    const char* digestable, | 
 |                    size_t length, | 
 |                    DigestValue& digest_result) { | 
 |   WebCryptoAlgorithmId algorithm_id = ToWebCryptoAlgorithmId(algorithm); | 
 |   WebCrypto* crypto = Platform::Current()->Crypto(); | 
 |   unsigned char* result; | 
 |   unsigned result_size; | 
 |  | 
 |   ASSERT(crypto); | 
 |  | 
 |   std::unique_ptr<WebCryptoDigestor> digestor = | 
 |       crypto->CreateDigestor(algorithm_id); | 
 |   DCHECK(digestor); | 
 |   if (!digestor->Consume(reinterpret_cast<const unsigned char*>(digestable), | 
 |                          length) || | 
 |       !digestor->Finish(result, result_size)) | 
 |     return false; | 
 |  | 
 |   digest_result.Append(static_cast<uint8_t*>(result), result_size); | 
 |   return true; | 
 | } | 
 |  | 
 | std::unique_ptr<WebCryptoDigestor> CreateDigestor(HashAlgorithm algorithm) { | 
 |   return Platform::Current()->Crypto()->CreateDigestor( | 
 |       ToWebCryptoAlgorithmId(algorithm)); | 
 | } | 
 |  | 
 | void FinishDigestor(WebCryptoDigestor* digestor, DigestValue& digest_result) { | 
 |   unsigned char* result = 0; | 
 |   unsigned result_size = 0; | 
 |  | 
 |   if (!digestor->Finish(result, result_size)) | 
 |     return; | 
 |  | 
 |   ASSERT(result); | 
 |  | 
 |   digest_result.Append(static_cast<uint8_t*>(result), result_size); | 
 | } | 
 |  | 
 | }  // namespace blink |