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 WEBRTC_BASE_MESSAGEDIGEST_H_ 12 #define WEBRTC_BASE_MESSAGEDIGEST_H_ 13 14 #include <string> 15 16 namespace rtc { 17 18 // Definitions for the digest algorithms. 19 extern const char DIGEST_MD5[]; 20 extern const char DIGEST_SHA_1[]; 21 extern const char DIGEST_SHA_224[]; 22 extern const char DIGEST_SHA_256[]; 23 extern const char DIGEST_SHA_384[]; 24 extern const char DIGEST_SHA_512[]; 25 26 // A general class for computing hashes. 27 class 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. 41 class 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). 47 bool 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. 55 size_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. 60 size_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. 64 std::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. 68 std::string ComputeDigest(const std::string& alg, const std::string& input); 69 // Like the previous function, but returns an explicit result code. 70 bool 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. 74 inline 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. 85 size_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. 91 size_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. 96 std::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. 101 std::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. 104 bool ComputeHmac(const std::string& alg, const std::string& key, 105 const std::string& input, std::string* output); 106 107 } // namespace rtc 108 109 #endif // WEBRTC_BASE_MESSAGEDIGEST_H_ 110