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,
23 OutputEncoding encoding = OutputEncoding::kHex);
24#endif
25
26/// @brief Calculates SHA-1, output format depends from encoding param
27/// @param encoding result could be returned as binary string or encoded
28/// @throws CryptoException internal library exception
29std::string Sha1(std::string_view data,
30 OutputEncoding encoding = OutputEncoding::kHex);
31
32/// @brief Calculates SHA-224, output format depends from encoding param
33/// @param encoding result could be returned as binary string or encoded
34/// @throws CryptoException internal library exception
35std::string Sha224(std::string_view data,
36 OutputEncoding encoding = OutputEncoding::kHex);
37
38/// @brief Calculates SHA-256, output format depends from encoding param
39/// @param encoding result could be returned as binary string or encoded
40/// @throws CryptoException internal library exception
41std::string Sha256(std::string_view data,
42 OutputEncoding encoding = OutputEncoding::kHex);
43
44/// @brief Calculates SHA-384, output format depends from encoding param
45/// @param encoding result could be returned as binary string or encoded
46/// @throws CryptoException internal library exception
47std::string Sha384(std::string_view data,
48 OutputEncoding encoding = OutputEncoding::kHex);
49
50/// @brief Calculates SHA-512, output format depends from encoding param
51/// @param encoding result could be returned as binary string or encoded
52/// @throws CryptoException internal library exception
53std::string Sha512(std::string_view data,
54 OutputEncoding encoding = OutputEncoding::kHex);
55
56/// @brief Calculates HMAC (using SHA-1 hash), encodes result with `encoding`
57/// algorithm
58/// @param key HMAC key
59/// @param message data to encode
60/// @param encoding result could be returned as binary string or encoded
61/// @throws CryptoException internal library exception
62std::string HmacSha1(std::string_view key, std::string_view message,
63 OutputEncoding encoding = OutputEncoding::kHex);
64
65/// @brief Calculates HMAC (using SHA-256 hash), encodes result with `encoding`
66/// algorithm
67/// @param key HMAC key
68/// @param message data to encode
69/// @param encoding result could be returned as binary string or encoded
70/// @throws CryptoException internal library exception
71std::string HmacSha256(std::string_view key, std::string_view message,
72 OutputEncoding encoding = OutputEncoding::kHex);
73
74/// @brief Calculates HMAC (using SHA-384 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 HmacSha384(std::string_view key, std::string_view message,
81 OutputEncoding encoding = OutputEncoding::kHex);
82
83/// @brief Calculates HMAC (using SHA-512 hash), encodes result with `encoding`
84/// algorithm
85/// @param key HMAC key
86/// @param message data to encode
87/// @param encoding result could be returned as binary string or encoded
88/// @throws CryptoException internal library exception
89std::string HmacSha512(std::string_view key, std::string_view message,
90 OutputEncoding encoding = OutputEncoding::kHex);
91
92/// Broken cryptographic hashes, must not be used except for compatibility
93namespace weak {
94
95/// @brief Calculates MD5, output format depends from encoding param
96/// @param encoding result could be returned as binary string or encoded
97/// @throws CryptoException internal library exception
98std::string Md5(std::string_view data,
99 OutputEncoding encoding = OutputEncoding::kHex);
100
101} // namespace weak
102} // namespace crypto::hash
103
104USERVER_NAMESPACE_END