blob: 691330e23c11772a05b23267e02277e18574fed2 [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
Steve Anton10542f22019-01-11 09:11:00 -080011#ifndef RTC_BASE_MESSAGE_DIGEST_H_
12#define RTC_BASE_MESSAGE_DIGEST_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stddef.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <string>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
19
20// Definitions for the digest algorithms.
21extern const char DIGEST_MD5[];
22extern const char DIGEST_SHA_1[];
23extern const char DIGEST_SHA_224[];
24extern const char DIGEST_SHA_256[];
25extern const char DIGEST_SHA_384[];
26extern const char DIGEST_SHA_512[];
27
28// A general class for computing hashes.
29class MessageDigest {
30 public:
31 enum { kMaxSize = 64 }; // Maximum known size (SHA-512)
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.
43class MessageDigestFactory {
44 public:
45 static MessageDigest* Create(const std::string& alg);
46};
47
Harald Alvestrandcffaf0a2021-01-05 15:55:20 +000048// A check that an algorithm is in a list of approved digest algorithms
49// from RFC 4572 (FIPS 180).
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020050bool IsFips180DigestAlgorithm(const std::string& alg);
51
52// Functions to create hashes.
53
54// Computes the hash of |in_len| bytes of |input|, using the |digest| hash
55// implementation, and outputs the hash to the buffer |output|, which is
56// |out_len| bytes long. Returns the number of bytes written to |output| if
57// successful, or 0 if |out_len| was too small.
Yves Gerey665174f2018-06-19 15:03:05 +020058size_t ComputeDigest(MessageDigest* digest,
59 const void* input,
60 size_t in_len,
61 void* output,
62 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063// Like the previous function, but creates a digest implementation based on
64// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
65// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +020066size_t ComputeDigest(const std::string& alg,
67 const void* input,
68 size_t in_len,
69 void* output,
70 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020071// Computes the hash of |input| using the |digest| hash implementation, and
72// returns it as a hex-encoded string.
73std::string ComputeDigest(MessageDigest* digest, const std::string& input);
74// Like the previous function, but creates a digest implementation based on
75// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
76// there is no digest with the given name.
77std::string ComputeDigest(const std::string& alg, const std::string& input);
78// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +020079bool ComputeDigest(const std::string& alg,
80 const std::string& input,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020081 std::string* output);
82
83// Shorthand way to compute a hex-encoded hash using MD5.
84inline std::string MD5(const std::string& input) {
85 return ComputeDigest(DIGEST_MD5, input);
86}
87
88// Functions to compute RFC 2104 HMACs.
89
90// Computes the HMAC of |in_len| bytes of |input|, using the |digest| hash
91// implementation and |key_len| bytes of |key| to key the HMAC, and outputs
92// the HMAC to the buffer |output|, which is |out_len| bytes long. Returns the
93// number of bytes written to |output| if successful, or 0 if |out_len| was too
94// small.
Yves Gerey665174f2018-06-19 15:03:05 +020095size_t ComputeHmac(MessageDigest* digest,
96 const void* key,
97 size_t key_len,
98 const void* input,
99 size_t in_len,
100 void* output,
101 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200102// Like the previous function, but creates a digest implementation based on
103// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns 0 if there is no
104// digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200105size_t ComputeHmac(const std::string& alg,
106 const void* key,
107 size_t key_len,
108 const void* input,
109 size_t in_len,
110 void* output,
111 size_t out_len);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112// Computes the HMAC of |input| using the |digest| hash implementation and |key|
113// to key the HMAC, and returns it as a hex-encoded string.
Yves Gerey665174f2018-06-19 15:03:05 +0200114std::string ComputeHmac(MessageDigest* digest,
115 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200116 const std::string& input);
117// Like the previous function, but creates a digest implementation based on
118// the desired digest name |alg|, e.g. DIGEST_SHA_1. Returns empty string if
119// there is no digest with the given name.
Yves Gerey665174f2018-06-19 15:03:05 +0200120std::string ComputeHmac(const std::string& alg,
121 const std::string& key,
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200122 const std::string& input);
123// Like the previous function, but returns an explicit result code.
Yves Gerey665174f2018-06-19 15:03:05 +0200124bool ComputeHmac(const std::string& alg,
125 const std::string& key,
126 const std::string& input,
127 std::string* output);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128
129} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130
Steve Anton10542f22019-01-11 09:11:00 -0800131#endif // RTC_BASE_MESSAGE_DIGEST_H_