userver: userver/crypto/hash.hpp Source File
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/crypto/hash.hpp
4/// @brief @copybrief crypto::hash
5/// @ingroup userver_universal
6
7#include <string_view>
8
9USERVER_NAMESPACE_BEGIN
10
11/// Cryptographic hashing
12namespace crypto::hash {
13
14enum class OutputEncoding { kBinary, kBase16, kHex = kBase16, kBase64 };
15
16enum class Pad { kWith, kWithout };
17
18#ifndef USERVER_NO_CRYPTOPP_BLAKE2
19/// @brief Calculates Blake2-128, output format depends from encoding param
20/// @param encoding result could be returned as binary string or encoded
21/// @throws CryptoException internal library exception
22std::string Blake2b128(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
23#endif
24
25/// @brief Calculates SHA-1, output format depends from encoding param
26/// @param encoding result could be returned as binary string or encoded
27/// @throws CryptoException internal library exception
28std::string Sha1(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
29
30/// @brief Calculates SHA-224, output format depends from encoding param
31/// @param encoding result could be returned as binary string or encoded
32/// @throws CryptoException internal library exception
33std::string Sha224(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
34
35/// @brief Calculates SHA-256, output format depends from encoding param
36/// @param encoding result could be returned as binary string or encoded
37/// @throws CryptoException internal library exception
38std::string Sha256(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
39
40/// @brief Calculates SHA-384, output format depends from encoding param
41/// @param encoding result could be returned as binary string or encoded
42/// @throws CryptoException internal library exception
43std::string Sha384(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
44
45/// @brief Calculates SHA-512, output format depends from encoding param
46/// @param encoding result could be returned as binary string or encoded
47/// @throws CryptoException internal library exception
48std::string Sha512(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
49
50/// @brief Calculates HMAC (using SHA-1 hash), encodes result with `encoding`
51/// algorithm
52/// @param key HMAC key
53/// @param message data to encode
54/// @param encoding result could be returned as binary string or encoded
55/// @throws CryptoException internal library exception
56std::string HmacSha1(std::string_view key, std::string_view message, OutputEncoding encoding = OutputEncoding::kHex);
57
58/// @brief Calculates HMAC (using SHA-256 hash), encodes result with `encoding`
59/// algorithm
60/// @param key HMAC key
61/// @param message data to encode
62/// @param encoding result could be returned as binary string or encoded
63/// @throws CryptoException internal library exception
64std::string HmacSha256(std::string_view key, std::string_view message, OutputEncoding encoding = OutputEncoding::kHex);
65
66/// @brief Calculates HMAC (using SHA-384 hash), encodes result with `encoding`
67/// algorithm
68/// @param key HMAC key
69/// @param message data to encode
70/// @param encoding result could be returned as binary string or encoded
71/// @throws CryptoException internal library exception
72std::string HmacSha384(std::string_view key, std::string_view message, OutputEncoding encoding = OutputEncoding::kHex);
73
74/// @brief Calculates HMAC (using SHA-512 hash), encodes result with `encoding`
75/// algorithm
76/// @param key HMAC key
77/// @param message data to encode
78/// @param encoding result could be returned as binary string or encoded
79/// @throws CryptoException internal library exception
80std::string HmacSha512(std::string_view key, std::string_view message, OutputEncoding encoding = OutputEncoding::kHex);
81
82/// Broken cryptographic hashes, must not be used except for compatibility
83namespace weak {
84
85/// @brief Calculates MD5, output format depends from encoding param
86/// @param encoding result could be returned as binary string or encoded
87/// @throws CryptoException internal library exception
88std::string Md5(std::string_view data, OutputEncoding encoding = OutputEncoding::kHex);
89
90} // namespace weak
91} // namespace crypto::hash
92
93USERVER_NAMESPACE_END