crypto.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_CRYPTO_H_
  5. #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_CRYPTO_H_
  6. #include "base/containers/span.h"
  7. #include "third_party/blink/renderer/platform/platform_export.h"
  8. #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
  9. #include "third_party/blink/renderer/platform/wtf/hash_functions.h"
  10. #include "third_party/blink/renderer/platform/wtf/text/string_hasher.h"
  11. #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  12. #include "third_party/blink/renderer/platform/wtf/vector.h"
  13. #include "third_party/boringssl/src/include/openssl/digest.h"
  14. namespace blink {
  15. static const size_t kMaxDigestSize = 64;
  16. typedef Vector<uint8_t, kMaxDigestSize> DigestValue;
  17. enum HashAlgorithm {
  18. kHashAlgorithmSha1,
  19. kHashAlgorithmSha256,
  20. kHashAlgorithmSha384,
  21. kHashAlgorithmSha512
  22. };
  23. PLATFORM_EXPORT bool ComputeDigest(HashAlgorithm,
  24. const char* digestable,
  25. size_t length,
  26. DigestValue& digest_result);
  27. class PLATFORM_EXPORT Digestor {
  28. public:
  29. explicit Digestor(HashAlgorithm);
  30. ~Digestor();
  31. bool has_failed() const { return has_failed_; }
  32. // Return false on failure. These do nothing once the |has_failed_| flag is
  33. // set. This object cannot be reused; do not update it after Finish.
  34. bool Update(base::span<const uint8_t>);
  35. bool UpdateUtf8(const String&,
  36. WTF::UTF8ConversionMode = WTF::kLenientUTF8Conversion);
  37. bool Finish(DigestValue&);
  38. private:
  39. bssl::ScopedEVP_MD_CTX digest_context_;
  40. bool has_failed_ = false;
  41. };
  42. } // namespace blink
  43. namespace WTF {
  44. struct DigestValueHash {
  45. STATIC_ONLY(DigestValueHash);
  46. static unsigned GetHash(const blink::DigestValue& v) {
  47. return StringHasher::ComputeHash(v.data(), v.size());
  48. }
  49. static bool Equal(const blink::DigestValue& a, const blink::DigestValue& b) {
  50. return a == b;
  51. }
  52. static const bool safe_to_compare_to_empty_or_deleted = true;
  53. };
  54. template <>
  55. struct DefaultHash<blink::DigestValue> {
  56. STATIC_ONLY(DefaultHash);
  57. typedef DigestValueHash Hash;
  58. };
  59. template <>
  60. struct DefaultHash<blink::HashAlgorithm> {
  61. STATIC_ONLY(DefaultHash);
  62. typedef IntHash<blink::HashAlgorithm> Hash;
  63. };
  64. template <>
  65. struct HashTraits<blink::HashAlgorithm>
  66. : UnsignedWithZeroKeyHashTraits<blink::HashAlgorithm> {
  67. STATIC_ONLY(HashTraits);
  68. };
  69. } // namespace WTF
  70. #endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_CRYPTO_H_