message_digest.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_MESSAGE_DIGEST_H_
  11. #define RTC_BASE_MESSAGE_DIGEST_H_
  12. #include <stddef.h>
  13. #include <string>
  14. namespace rtc {
  15. // Definitions for the digest algorithms.
  16. extern const char DIGEST_MD5[];
  17. extern const char DIGEST_SHA_1[];
  18. extern const char DIGEST_SHA_224[];
  19. extern const char DIGEST_SHA_256[];
  20. extern const char DIGEST_SHA_384[];
  21. extern const char DIGEST_SHA_512[];
  22. // A general class for computing hashes.
  23. class MessageDigest {
  24. public:
  25. enum { kMaxSize = 64 }; // Maximum known size (SHA-512)
  26. virtual ~MessageDigest() {}
  27. // Returns the digest output size (e.g. 16 bytes for MD5).
  28. virtual size_t Size() const = 0;
  29. // Updates the digest with |len| bytes from |buf|.
  30. virtual void Update(const void* buf, size_t len) = 0;
  31. // Outputs the digest value to |buf| with length |len|.
  32. // Returns the number of bytes written, i.e., Size().
  33. virtual size_t Finish(void* buf, size_t len) = 0;
  34. };
  35. // A factory class for creating digest objects.
  36. class MessageDigestFactory {
  37. public:
  38. static MessageDigest* Create(const std::string& alg);
  39. };
  40. // A whitelist of approved digest algorithms from RFC 4572 (FIPS 180).
  41. bool IsFips180DigestAlgorithm(const std::string& alg);
  42. // Functions to create hashes.
  43. // Computes the hash of |in_len| bytes of |input|, using the |digest| hash
  44. // implementation, and outputs the hash to the buffer |output|, which is
  45. // |out_len| bytes long. Returns the number of bytes written to |output| if
  46. // successful, or 0 if |out_len| was too small.
  47. size_t ComputeDigest(MessageDigest* digest,
  48. const void* input,
  49. size_t in_len,
  50. void* output,
  51. size_t out_len);
  52. // Like the previous function, but creates a digest implementation based on
  53. // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
  54. // digest with the given name.
  55. size_t ComputeDigest(const std::string& alg,
  56. const void* input,
  57. size_t in_len,
  58. void* output,
  59. size_t out_len);
  60. // Computes the hash of |input| using the |digest| hash implementation, and
  61. // returns it as a hex-encoded string.
  62. std::string ComputeDigest(MessageDigest* digest, const std::string& input);
  63. // Like the previous function, but creates a digest implementation based on
  64. // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
  65. // there is no digest with the given name.
  66. std::string ComputeDigest(const std::string& alg, const std::string& input);
  67. // Like the previous function, but returns an explicit result code.
  68. bool ComputeDigest(const std::string& alg,
  69. const std::string& input,
  70. std::string* output);
  71. // Shorthand way to compute a hex-encoded hash using MD5.
  72. inline std::string MD5(const std::string& input) {
  73. return ComputeDigest(DIGEST_MD5, input);
  74. }
  75. // Functions to compute RFC 2104 HMACs.
  76. // Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash
  77. // implementation and |key_len| bytes of |key| to key the HMAC, and outputs
  78. // the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the
  79. // number of bytes written to |output| if successful, or 0 if |out_len| was too
  80. // small.
  81. size_t ComputeHmac(MessageDigest* digest,
  82. const void* key,
  83. size_t key_len,
  84. const void* input,
  85. size_t in_len,
  86. void* output,
  87. size_t out_len);
  88. // Like the previous function, but creates a digest implementation based on
  89. // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
  90. // digest with the given name.
  91. size_t ComputeHmac(const std::string& alg,
  92. const void* key,
  93. size_t key_len,
  94. const void* input,
  95. size_t in_len,
  96. void* output,
  97. size_t out_len);
  98. // Computes the HMAC of |input| using the |digest| hash implementation and |key|
  99. // to key the HMAC, and returns it as a hex-encoded string.
  100. std::string ComputeHmac(MessageDigest* digest,
  101. const std::string& key,
  102. const std::string& input);
  103. // Like the previous function, but creates a digest implementation based on
  104. // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
  105. // there is no digest with the given name.
  106. std::string ComputeHmac(const std::string& alg,
  107. const std::string& key,
  108. const std::string& input);
  109. // Like the previous function, but returns an explicit result code.
  110. bool ComputeHmac(const std::string& alg,
  111. const std::string& key,
  112. const std::string& input,
  113. std::string* output);
  114. } // namespace rtc
  115. #endif // RTC_BASE_MESSAGE_DIGEST_H_