blob: fc820885436f8ce71d45f6cb2153a7ef6173b398 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_MESSAGEDIGEST_H_
12#define RTC_BASE_MESSAGEDIGEST_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016namespace rtc {
17
18// Definitions for the digest algorithms.
19extern const char DIGEST_MD5[];
20extern const char DIGEST_SHA_1[];
21extern const char DIGEST_SHA_224[];
22extern const char DIGEST_SHA_256[];
23extern const char DIGEST_SHA_384[];
24extern const char DIGEST_SHA_512[];
25
26// A general class for computing hashes.
27class MessageDigest {
28 public:
29 enum { kMaxSize = 64 }; // Maximum known size (SHA-512)
30 virtual ~MessageDigest() {}
31 // Returns the digest output size (e.g. 16 bytes for MD5).
32 virtual size_t Size() const = 0;
33 // Updates the digest with |len| bytes from |buf|.
34 virtual void Update(const void* buf, size_t len) = 0;
35 // Outputs the digest value to |buf| with length |len|.
36 // Returns the number of bytes written, i.e., Size().
37 virtual size_t Finish(void* buf, size_t len) = 0;
38};
39
40// A factory class for creating digest objects.
41class MessageDigestFactory {
42 public:
43 static MessageDigest* Create(const std::string& alg);
44};
45
46// A whitelist of approved digest algorithms from RFC 4572 (FIPS 180).
47bool IsFips180DigestAlgorithm(const std::string& alg);
48
49// Functions to create hashes.
50
51// Computes the hash of |in_len| bytes of |input|, using the |digest| hash
52// implementation, and outputs the hash to the buffer |output|, which is
53// |out_len| bytes long. Returns the number of bytes written to |output| if
54// successful, or 0 if |out_len| was too small.
Yves Gerey665174f2018-06-19 15:03:05 +020055size_t ComputeDigest(MessageDigest* digest,
56 const void* input,
57 size_t in_len,
58 void* output,
59 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060// Like the previous function, but creates a digest implementation based on
61// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
62// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +020063size_t ComputeDigest(const std::string& alg,
64 const void* input,
65 size_t in_len,
66 void* output,
67 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020068// Computes the hash of |input| using the |digest| hash implementation, and
69// returns it as a hex-encoded string.
70std::string ComputeDigest(MessageDigest* digest, const std::string& input);
71// Like the previous function, but creates a digest implementation based on
72// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
73// there is no digest with the given name.
74std::string ComputeDigest(const std::string& alg, const std::string& input);
75// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +020076bool ComputeDigest(const std::string& alg,
77 const std::string& input,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 std::string* output);
79
80// Shorthand way to compute a hex-encoded hash using MD5.
81inline std::string MD5(const std::string& input) {
82 return ComputeDigest(DIGEST_MD5, input);
83}
84
85// Functions to compute RFC 2104 HMACs.
86
87// Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash
88// implementation and |key_len| bytes of |key| to key the HMAC, and outputs
89// the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the
90// number of bytes written to |output| if successful, or 0 if |out_len| was too
91// small.
Yves Gerey665174f2018-06-19 15:03:05 +020092size_t ComputeHmac(MessageDigest* digest,
93 const void* key,
94 size_t key_len,
95 const void* input,
96 size_t in_len,
97 void* output,
98 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020099// Like the previous function, but creates a digest implementation based on
100// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
101// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200102size_t ComputeHmac(const std::string& alg,
103 const void* key,
104 size_t key_len,
105 const void* input,
106 size_t in_len,
107 void* output,
108 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109// Computes the HMAC of |input| using the |digest| hash implementation and |key|
110// to key the HMAC, and returns it as a hex-encoded string.
Yves Gerey665174f2018-06-19 15:03:05 +0200111std::string ComputeHmac(MessageDigest* digest,
112 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 const std::string& input);
114// Like the previous function, but creates a digest implementation based on
115// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
116// there is no digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200117std::string ComputeHmac(const std::string& alg,
118 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 const std::string& input);
120// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +0200121bool ComputeHmac(const std::string& alg,
122 const std::string& key,
123 const std::string& input,
124 std::string* output);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200125
126} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200128#endif // RTC_BASE_MESSAGEDIGEST_H_