-
Notifications
You must be signed in to change notification settings - Fork 82
/
Checksum.hh
61 lines (51 loc) · 1.29 KB
/
Checksum.hh
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
copyright 2006-2017 Paul Dreik (earlier Paul Sundvall)
Distributed under GPL v 2.0 or later, at your option.
See LICENSE for further details.
*/
#ifndef RDFIND_CHECKSUM_HH
#define RDFIND_CHECKSUM_HH
#include <cstddef>
#include <nettle/md5.h>
#include <nettle/sha.h>
/**
* class for checksum calculation
*/
class Checksum
{
public:
// these are the checksums that can be calculated
enum class checksumtypes
{
NOTSET = 0,
MD5,
SHA1,
SHA256,
SHA512
};
explicit Checksum(checksumtypes type);
int update(std::size_t length, const unsigned char* buffer);
int update(std::size_t length, const char* buffer);
#if 0
/// prints the checksum on stdout
int print();
#endif
// writes the checksum to buffer.
// returns 0 if everything went ok.
int printToBuffer(void* buffer, std::size_t N);
// returns the number of bytes that the buffer needs to be
// returns negative if something is wrong.
[[gnu::pure]] int getDigestLength() const;
private:
// to know what type of checksum we are doing
const checksumtypes m_checksumtype = checksumtypes::NOTSET;
// the checksum calculation internal state
union ChecksumStruct
{
sha1_ctx sha1;
sha256_ctx sha256;
sha512_ctx sha512;
md5_ctx md5;
} m_state;
};
#endif // RDFIND_CHECKSUM_HH