31 #ifndef _CRYPTO_HASH_H
32 #define _CRYPTO_HASH_H
44 namespace scc::crypto {
99 sha512_224_type =1007,
100 sha512_256_type =1008,
104 std::unique_ptr<HashBase> m_ptr;
111 case md5_type:
return 16;
112 case sha1_type:
return 20;
113 case sha224_type:
return 28;
114 case sha256_type:
return 32;
115 case sha384_type:
return 48;
116 case sha512_type:
return 64;
117 case sha512_224_type:
return 28;
118 case sha512_256_type:
return 32;
119 case sm3_type:
return 32;
134 Hash& operator=(
const Hash&) =
delete;
156 update(v.data(), v.size());
161 update(v.data(), v.size());
179 return get_tag(v.data(), v.size());
186 if (sz >= 0) v.resize(sz);
187 return get_tag(v.data(), v.size());
197 int final(
void*, int);
199 int final(std::vector<char>& v)
202 return final(v.data(), v.size());
206 int alg()
const {
return m_alg; }
209 int size()
const {
return alg_size(m_alg); }
222 virtual size_t read(
void* loc,
size_t len)
225 int got = m_rd.
read(m_buf.data(), len);
230 m_chk.
update((
char*)m_buf.data(), got);
231 memcpy(loc, (
char*)m_buf.data(), got);
245 virtual size_t write(
const void* loc,
size_t len)
247 int put = m_wr.
write(loc, len);
255 virtual void shutdown() {}
266 std::unique_ptr<HmacBase> m_ptr;
287 Hmac& operator=(
const Hmac&) =
delete;
302 void init(std::vector<char>& v)
304 init(v.data(), v.size());
313 update(v.data(), v.size());
315 void update(
const std::string& v)
317 update(v.data(), v.size());
328 int final(
void*, int);
330 int final(std::vector<char>& v)
333 return final(v.data(), v.size());
337 int alg()
const {
return m_alg; }
340 int size()
const {
return Hash::alg_size(m_alg); }
Helper class to hash an incoming stream.
virtual size_t read(void *loc, size_t len)
Read interface.
Helper class to hash an outgoing stream.
virtual size_t write(const void *loc, size_t len)
Write interface.
General one-way hashing algorithms.
int get_tag(std::vector< char > &v, int sz)
Get the current hash value using a vector, for length between 1 and size of hash.
int size() const
Return the current hash size.
static bool supported(Algorithm)
Test if an algorithm type is supported.
Hash(Algorithm)
Initialize with hash type.
void update(const std::string v)
Update using a string.
void update(const std::vector< char > &v)
Update using a char vector.
int alg() const
Hash algorithm.
void update(const void *, int)
Update the hash by adding data.
Hash(Hash &&other)
Move constructor.
void reset()
Reset the hash to initial value.
int get_tag(void *, int)
Get the current hash value without resetting the hash.
Hash & operator=(Hash &&other)
Move assign.
int get_tag(std::vector< char > &v)
Get the full current hash value using a secure vector.
Hmac, aka hash-based message authentication code.
static bool supported(int)
Test if an algorithm type is supported.
void init(std::vector< char > &v)
Initialize the hmac with a new key vector.
Hmac(const std::vector< char > &key, Hash::Algorithm alg)
Construct an hmac.
int alg() const
Hmac algorithm.
void reset()
Reset the hmac, using the same key.
void update(const std::vector< char > &v)
Update the hmac with new data.
Hmac & operator=(Hmac &&other)
Move assign.
Hmac(const std::string &key, Hash::Algorithm alg)
Construct an hmac.
Hmac(Hmac &&other)
Move constructor.
Hmac(const void *, int, Hash::Algorithm)
Construct an hmac.
void update(const void *, int)
Update the hmac with new data.
void init(const void *, int)
Initialize the hmac with a new key.
int size() const
Hmac size.
void resize(typename std::vector< T >::size_type)
Resize the vector.
Input/output stream base reader/writer interface classes.
Interface class for objects which can be read.
virtual size_t read(void *, size_t)=0
Read interface.
Interface class for objects which can be written.
virtual size_t write(const void *, size_t)=0
Write interface.