1 // Copyright 2014 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 // TODO(jww) The original Blink-style header guard for this file conflicts with 6 // the header guard in Source/modules/crypto/Crypto.h, so this is a 7 // Chromium-style header guard instead. There is now a bug 8 // (https://crbug.com/360121) to track a proposal to change all header guards 9 // to a similar style. Thus, whenever that is resolved, this header guard 10 // should be changed to whatever style is agreed upon. 11 #ifndef SOURCE_PLATFORM_CRYPTO_H_ 12 #define SOURCE_PLATFORM_CRYPTO_H_ 13 14 #include "platform/PlatformExport.h" 15 #include "public/platform/WebCrypto.h" 16 #include "wtf/HashSet.h" 17 #include "wtf/StringHasher.h" 18 #include "wtf/Vector.h" 19 20 namespace WebCore { 21 22 static const size_t kMaxDigestSize = 64; 23 typedef Vector<uint8_t, kMaxDigestSize> DigestValue; 24 25 const size_t sha1HashSize = 20; 26 enum HashAlgorithm { 27 HashAlgorithmSha1, 28 HashAlgorithmSha256, 29 HashAlgorithmSha384, 30 HashAlgorithmSha512 31 }; 32 33 PLATFORM_EXPORT bool computeDigest(HashAlgorithm, const char* digestable, size_t length, DigestValue& digestResult); 34 PLATFORM_EXPORT PassOwnPtr<blink::WebCryptoDigestor> createDigestor(HashAlgorithm); 35 PLATFORM_EXPORT void finishDigestor(blink::WebCryptoDigestor*, DigestValue& digestResult); 36 37 } // namespace WebCore 38 39 namespace WTF { 40 41 struct DigestValueHash { 42 static unsigned hash(const WebCore::DigestValue& v) 43 { 44 return StringHasher::computeHash(v.data(), v.size()); 45 } 46 static bool equal(const WebCore::DigestValue& a, const WebCore::DigestValue& b) 47 { 48 return a == b; 49 }; 50 static const bool safeToCompareToEmptyOrDeleted = true; 51 }; 52 template <> 53 struct DefaultHash<WebCore::DigestValue> { 54 typedef DigestValueHash Hash; 55 }; 56 57 template <> 58 struct DefaultHash<WebCore::HashAlgorithm> { 59 typedef IntHash<WebCore::HashAlgorithm> Hash; 60 }; 61 template <> 62 struct HashTraits<WebCore::HashAlgorithm> : UnsignedWithZeroKeyHashTraits<WebCore::HashAlgorithm> { 63 }; 64 65 } // namespace WTF 66 #endif // SOURCE_PLATFORM_CRYPTO_H_ 67