Skip to content

Commit 70cc937

Browse files
committed
version 8
1 parent a8a88f8 commit 70cc937

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

keccak.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ void Keccak::processBlock(const void* data)
149149
m_hash[ 1] = rotateLeft(last, 44);
150150

151151
// Chi
152-
for (unsigned int j = 0; j < 25; j += 5)
152+
for (unsigned int j = 0; j < StateSize; j += 5)
153153
{
154154
// temporaries
155155
uint64_t one = m_hash[j];
@@ -235,6 +235,11 @@ void Keccak::processBuffer()
235235
/// return latest hash as 16 hex characters
236236
std::string Keccak::getHash()
237237
{
238+
// save hash state
239+
uint64_t oldHash[StateSize];
240+
for (unsigned int i = 0; i < StateSize; i++)
241+
oldHash[i] = m_hash[i];
242+
238243
// process remaining bytes
239244
processBuffer();
240245

@@ -267,6 +272,10 @@ std::string Keccak::getHash()
267272
processed += 8;
268273
}
269274

275+
// restore state
276+
for (unsigned int i = 0; i < StateSize; i++)
277+
m_hash[i] = oldHash[i];
278+
270279
return result;
271280
}
272281

sha3.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <endian.h>
1212
#endif
1313

14+
#include <iostream>
15+
1416

1517
/// same as reset()
1618
SHA3::SHA3(Bits bits)
@@ -149,7 +151,7 @@ void SHA3::processBlock(const void* data)
149151
m_hash[ 1] = rotateLeft(last, 44);
150152

151153
// Chi
152-
for (unsigned int j = 0; j < 25; j += 5)
154+
for (unsigned int j = 0; j < StateSize; j += 5)
153155
{
154156
// temporaries
155157
uint64_t one = m_hash[j];
@@ -234,6 +236,11 @@ void SHA3::processBuffer()
234236
/// return latest hash as 16 hex characters
235237
std::string SHA3::getHash()
236238
{
239+
// save hash state
240+
uint64_t oldHash[StateSize];
241+
for (unsigned int i = 0; i < StateSize; i++)
242+
oldHash[i] = m_hash[i];
243+
237244
// process remaining bytes
238245
processBuffer();
239246

@@ -267,6 +274,10 @@ std::string SHA3::getHash()
267274
processed += 8;
268275
}
269276

277+
// restore state
278+
for (unsigned int i = 0; i < StateSize; i++)
279+
m_hash[i] = oldHash[i];
280+
270281
return result;
271282
}
272283

tests/github-issue2.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// minimal test case for https://github.com/stbrumme/hash-library/issues/2
2+
// g++ github-issue2.cpp ../sha*.cpp ../keccak.cpp ../md5.cpp -o github-issue2 && ./github-issue2
3+
4+
#include "../sha1.h"
5+
#include "../sha256.h"
6+
#include "../sha3.h"
7+
#include "../keccak.h"
8+
#include "../md5.h"
9+
#include <iostream>
10+
11+
int main()
12+
{
13+
std::string text = "hello world";
14+
15+
std::cout << "SHA1:" << std::endl;
16+
SHA1 sha1;
17+
sha1.add(text.c_str(), text.size());
18+
19+
std::cout << sha1.getHash() << std::endl;
20+
std::cout << sha1.getHash() << std::endl;
21+
std::cout << sha1.getHash() << std::endl;
22+
23+
std::cout << std::endl;
24+
25+
std::cout << "SHA256:" << std::endl;
26+
SHA256 sha256;
27+
sha256.add(text.c_str(), text.size());
28+
29+
std::cout << sha256.getHash() << std::endl;
30+
std::cout << sha256.getHash() << std::endl;
31+
std::cout << sha256.getHash() << std::endl;
32+
33+
std::cout << std::endl;
34+
35+
std::cout << "SHA3:" << std::endl;
36+
SHA3 sha3;
37+
sha3.add(text.c_str(), text.size());
38+
39+
std::cout << sha3.getHash() << std::endl;
40+
std::cout << sha3.getHash() << std::endl;
41+
std::cout << sha3.getHash() << std::endl;
42+
43+
std::cout << std::endl;
44+
45+
std::cout << "Keccak:" << std::endl;
46+
Keccak keccak;
47+
keccak.add(text.c_str(), text.size());
48+
49+
std::cout << keccak.getHash() << std::endl;
50+
std::cout << keccak.getHash() << std::endl;
51+
std::cout << keccak.getHash() << std::endl;
52+
53+
std::cout << std::endl;
54+
55+
std::cout << "MD5:" << std::endl;
56+
MD5 md5;
57+
md5.add(text.c_str(), text.size());
58+
59+
std::cout << md5.getHash() << std::endl;
60+
std::cout << md5.getHash() << std::endl;
61+
std::cout << md5.getHash() << std::endl;
62+
63+
return 0;
64+
}

tests/github-issue6.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// minimal test case for https://github.com/stbrumme/hash-library/issues/6
2+
// g++ github-issue6.cpp ../sha3.cpp -o github-issue6 && ./github-issue6
3+
4+
#include "../sha3.h"
5+
#include <iostream>
6+
7+
int main(int argc, char *argv[])
8+
{
9+
std::string text = "72a5f501151ca974002f34";
10+
11+
SHA3 hasher(SHA3::Bits512);
12+
hasher.add(text.data(), text.size());
13+
std::cout << hasher.getHash() << std::endl;
14+
15+
return 0;
16+
}

0 commit comments

Comments
 (0)