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
11 #ifndef RTC_BASE_MESSAGE_DIGEST_H_
12 #define RTC_BASE_MESSAGE_DIGEST_H_
13
14 #include <stddef.h>
15
16 #include <string>
17
18 namespace rtc {
19
20 // Definitions for the digest algorithms.
21 extern const char DIGEST_MD5[];
22 extern const char DIGEST_SHA_1[];
23 extern const char DIGEST_SHA_224[];
24 extern const char DIGEST_SHA_256[];
25 extern const char DIGEST_SHA_384[];
26 extern const char DIGEST_SHA_512[];
27
28 // A general class for computing hashes.
29 class MessageDigest {
30 public:
31 enum { kMaxSize = 64 }; // Maximum known size (SHA-512)
~MessageDigest()32 virtual ~MessageDigest() {}
33 // Returns the digest output size (e.g. 16 bytes for MD5).
34 virtual size_t Size() const = 0;
35 // Updates the digest with |len| bytes from |buf|.
36 virtual void Update(const void* buf, size_t len) = 0;
37 // Outputs the digest value to |buf| with length |len|.
38 // Returns the number of bytes written, i.e., Size().
39 virtual size_t Finish(void* buf, size_t len) = 0;
40 };
41
42 // A factory class for creating digest objects.
43 class MessageDigestFactory {
44 public:
45 static MessageDigest* Create(const std::string& alg);
46 };
47
48 // A whitelist of approved digest algorithms from RFC 4572 (FIPS 180).
49 bool IsFips180DigestAlgorithm(const std::string& alg);
50
51 // Functions to create hashes.
52
53 // Computes the hash of |in_len| bytes of |input|, using the |digest| hash
54 // implementation, and outputs the hash to the buffer |output|, which is
55 // |out_len| bytes long. Returns the number of bytes written to |output| if
56 // successful, or 0 if |out_len| was too small.
57 size_t ComputeDigest(MessageDigest* digest,
58 const void* input,
59 size_t in_len,
60 void* output,
61 size_t out_len);
62 // Like the previous function, but creates a digest implementation based on
63 // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
64 // digest with the given name.
65 size_t ComputeDigest(const std::string& alg,
66 const void* input,
67 size_t in_len,
68 void* output,
69 size_t out_len);
70 // Computes the hash of |input| using the |digest| hash implementation, and
71 // returns it as a hex-encoded string.
72 std::string ComputeDigest(MessageDigest* digest, const std::string& input);
73 // Like the previous function, but creates a digest implementation based on
74 // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
75 // there is no digest with the given name.
76 std::string ComputeDigest(const std::string& alg, const std::string& input);
77 // Like the previous function, but returns an explicit result code.
78 bool ComputeDigest(const std::string& alg,
79 const std::string& input,
80 std::string* output);
81
82 // Shorthand way to compute a hex-encoded hash using MD5.
MD5(const std::string & input)83 inline std::string MD5(const std::string& input) {
84 return ComputeDigest(DIGEST_MD5, input);
85 }
86
87 // Functions to compute RFC 2104 HMACs.
88
89 // Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash
90 // implementation and |key_len| bytes of |key| to key the HMAC, and outputs
91 // the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the
92 // number of bytes written to |output| if successful, or 0 if |out_len| was too
93 // small.
94 size_t ComputeHmac(MessageDigest* digest,
95 const void* key,
96 size_t key_len,
97 const void* input,
98 size_t in_len,
99 void* output,
100 size_t out_len);
101 // Like the previous function, but creates a digest implementation based on
102 // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
103 // digest with the given name.
104 size_t ComputeHmac(const std::string& alg,
105 const void* key,
106 size_t key_len,
107 const void* input,
108 size_t in_len,
109 void* output,
110 size_t out_len);
111 // Computes the HMAC of |input| using the |digest| hash implementation and |key|
112 // to key the HMAC, and returns it as a hex-encoded string.
113 std::string ComputeHmac(MessageDigest* digest,
114 const std::string& key,
115 const std::string& input);
116 // Like the previous function, but creates a digest implementation based on
117 // the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
118 // there is no digest with the given name.
119 std::string ComputeHmac(const std::string& alg,
120 const std::string& key,
121 const std::string& input);
122 // Like the previous function, but returns an explicit result code.
123 bool ComputeHmac(const std::string& alg,
124 const std::string& key,
125 const std::string& input,
126 std::string* output);
127
128 } // namespace rtc
129
130 #endif // RTC_BASE_MESSAGE_DIGEST_H_
131