blob: f80dd7aa53d7d1bc43e5155e4c7ac34b9aec0dee [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.
55size_t ComputeDigest(MessageDigest* digest, const void* input, size_t in_len,
56 void* output, size_t out_len);
57// Like the previous function, but creates a digest implementation based on
58// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
59// digest with the given name.
60size_t ComputeDigest(const std::string& alg, const void* input, size_t in_len,
61 void* output, size_t out_len);
62// Computes the hash of |input| using the |digest| hash implementation, and
63// returns it as a hex-encoded string.
64std::string ComputeDigest(MessageDigest* digest, const std::string& input);
65// Like the previous function, but creates a digest implementation based on
66// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
67// there is no digest with the given name.
68std::string ComputeDigest(const std::string& alg, const std::string& input);
69// Like the previous function, but returns an explicit result code.
70bool ComputeDigest(const std::string& alg, const std::string& input,
71 std::string* output);
72
73// Shorthand way to compute a hex-encoded hash using MD5.
74inline std::string MD5(const std::string& input) {
75 return ComputeDigest(DIGEST_MD5, input);
76}
77
78// Functions to compute RFC 2104 HMACs.
79
80// Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash
81// implementation and |key_len| bytes of |key| to key the HMAC, and outputs
82// the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the
83// number of bytes written to |output| if successful, or 0 if |out_len| was too
84// small.
85size_t ComputeHmac(MessageDigest* digest, const void* key, size_t key_len,
86 const void* input, size_t in_len,
87 void* output, 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.
91size_t ComputeHmac(const std::string& alg, const void* key, size_t key_len,
92 const void* input, size_t in_len,
93 void* output, size_t out_len);
94// Computes the HMAC of |input| using the |digest| hash implementation and |key|
95// to key the HMAC, and returns it as a hex-encoded string.
96std::string ComputeHmac(MessageDigest* digest, const std::string& key,
97 const std::string& input);
98// Like the previous function, but creates a digest implementation based on
99// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
100// there is no digest with the given name.
101std::string ComputeHmac(const std::string& alg, const std::string& key,
102 const std::string& input);
103// Like the previous function, but returns an explicit result code.
104bool ComputeHmac(const std::string& alg, const std::string& key,
105 const std::string& input, std::string* output);
106
107} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000108
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200109#endif // RTC_BASE_MESSAGEDIGEST_H_