-
Notifications
You must be signed in to change notification settings - Fork 82
/
Checksum.cc
175 lines (164 loc) · 4.07 KB
/
Checksum.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
copyright 2006-2017 Paul Dreik (earlier Paul Sundvall)
Distributed under GPL v 2.0 or later, at your option.
See LICENSE for further details.
*/
#include "config.h"
// std
#include <array>
#include <cassert>
#include <cstdio>
#include <stdexcept>
// project
#include "Checksum.hh"
Checksum::Checksum(checksumtypes type)
: m_checksumtype(type)
{
switch (m_checksumtype) {
case checksumtypes::SHA1: {
sha1_init(&m_state.sha1);
} break;
case checksumtypes::SHA256: {
sha256_init(&m_state.sha256);
} break;
case checksumtypes::SHA512: {
sha512_init(&m_state.sha512);
} break;
case checksumtypes::MD5: {
md5_init(&m_state.md5);
} break;
default:
// not allowed to have something that is not recognized.
throw std::runtime_error("wrong checksum type - programming error");
}
}
int
Checksum::update(std::size_t length, const unsigned char* buffer)
{
switch (m_checksumtype) {
case checksumtypes::SHA1:
sha1_update(&m_state.sha1, length, buffer);
break;
case checksumtypes::SHA256:
sha256_update(&m_state.sha256, length, buffer);
break;
case checksumtypes::SHA512:
sha512_update(&m_state.sha512, length, buffer);
break;
case checksumtypes::MD5:
md5_update(&m_state.md5, length, buffer);
break;
default:
return -1;
}
return 0;
}
int
Checksum::update(std::size_t length, const char* buffer)
{
return update(
length,
static_cast<const unsigned char*>(static_cast<const void*>(buffer)));
}
#if 0
// prints checksum to stdout
static void
display_hex(std::size_t length, const void* data_)
{
const char* data = static_cast<const char*>(data_);
for (std::size_t i = 0; i < length; i++) {
std::printf("%02x", data[i]);
}
std::printf("\n");
}
int
Checksum::print()
{
// the result is stored in this variable
switch (m_checksumtype) {
case SHA1: {
std::array<unsigned char, SHA1_DIGEST_SIZE> digest;
sha1_digest(&m_state.sha1, SHA1_DIGEST_SIZE, digest.data());
display_hex(SHA1_DIGEST_SIZE, digest.data());
} break;
case SHA256: {
std::array<unsigned char, SHA256_DIGEST_SIZE> digest;
sha256_digest(&m_state.sha256, digest.size(), digest.data());
display_hex(digest.size(), digest.data());
} break;
case MD5: {
std::array<unsigned char, MD5_DIGEST_SIZE> digest;
md5_digest(&m_state.md5, MD5_DIGEST_SIZE, digest.data());
display_hex(MD5_DIGEST_SIZE, digest.data());
} break;
default:
return -1;
}
return 0;
}
#endif
int
Checksum::getDigestLength() const
{
switch (m_checksumtype) {
case checksumtypes::SHA1:
return SHA1_DIGEST_SIZE;
case checksumtypes::SHA256:
return SHA256_DIGEST_SIZE;
case checksumtypes::SHA512:
return SHA512_DIGEST_SIZE;
case checksumtypes::MD5:
return MD5_DIGEST_SIZE;
default:
return -1;
}
return -1;
}
int
Checksum::printToBuffer(void* buffer, std::size_t N)
{
assert(buffer);
switch (m_checksumtype) {
case checksumtypes::SHA1:
if (N >= SHA1_DIGEST_SIZE) {
sha1_digest(
&m_state.sha1, SHA1_DIGEST_SIZE, static_cast<unsigned char*>(buffer));
} else {
// bad size.
return -1;
}
break;
case checksumtypes::SHA256:
if (N >= SHA256_DIGEST_SIZE) {
sha256_digest(&m_state.sha256,
SHA256_DIGEST_SIZE,
static_cast<unsigned char*>(buffer));
} else {
// bad size.
return -1;
}
break;
case checksumtypes::SHA512:
if (N >= SHA512_DIGEST_SIZE) {
sha512_digest(&m_state.sha512,
SHA512_DIGEST_SIZE,
static_cast<unsigned char*>(buffer));
} else {
// bad size.
return -1;
}
break;
case checksumtypes::MD5:
if (N >= MD5_DIGEST_SIZE) {
md5_digest(
&m_state.md5, MD5_DIGEST_SIZE, static_cast<unsigned char*>(buffer));
} else {
// bad size.
return -1;
}
break;
default:
return -1;
}
return 0;
}