-
Notifications
You must be signed in to change notification settings - Fork 0
/
sha1.h
38 lines (30 loc) · 811 Bytes
/
sha1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma once
#include <openssl/sha.h>
#include <array>
#include <string>
class sha1 {
public:
template <size_t N>
using buf = std::array<unsigned char, N>;
using hash = buf<SHA_DIGEST_LENGTH>;
inline sha1() {
SHA1_Init(&_ctx);
}
inline sha1(sha1 const &o): _ctx(o._ctx) { }
template <typename T>
inline sha1(T const &data) : sha1() {
update(data);
}
template <size_t N>
inline bool update(buf<N> const &data, size_t s=0) {
return SHA1_Update(&_ctx, data.data(), s != 0 ? s : data.size());
}
inline bool update(std::string const &data) {
return SHA1_Update(&_ctx, data.c_str(), data.size());
}
inline bool finalize(hash &hash) {
return SHA1_Final(hash.data(), &_ctx);
}
private:
SHA_CTX _ctx;
};