Skip to content

Commit d945021

Browse files
author
James Cleveland
committed
Merge pull request #2 from oohlaf/master
Port to Python 3
2 parents d90a7a8 + 52849b3 commit d945021

File tree

6 files changed

+246
-63
lines changed

6 files changed

+246
-63
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
build/
22
dist/
33
MANIFEST
4+
*.egg-info
5+
*.pyc
6+
*.so

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ The Whirlpool reference implementations are public domain, as is this code.
77

88
Wrapper written by James Cleveland with help from #python on irc.freenode.net.
99

10-
Wrapper modified by Olaf Conradi to use the hashlib interface.
10+
Wrapper extended to use the hashlib interface and ported to Python 3 by
11+
Olaf Conradi.
1112

1213
Usage
1314
-----
@@ -23,12 +24,26 @@ Python's hashlib.
2324
wp.update("My Salt")
2425
hashed_string = wp.hexdigest()
2526

27+
Starting with Python 3 text strings (as shown above) are stored as unicode.
28+
You need to specify the encoding of these strings before hashing.
29+
30+
wp = whirlpool.new(data.encoding('utf-8'))
31+
32+
Strings that are marked as binary do not need encoding.
33+
2634
Deprecated usage
2735
----------------
2836

29-
For backward compatibility the old interface remains available.
37+
For backward compatibility the old interface remains available. From Python 3
38+
onwards, the old interface is dropped.
3039

3140
import whirlpool
3241

3342
hashed_string = whirlpool.hash("My String")
3443

44+
45+
Testing
46+
-------
47+
48+
This module is tested using Python 2.7 and Python 3.3.
49+

Whirlpool.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ static void display(const u8 array[], int length) {
17151715
* 2. hashing all 512-bit strings containing a single set bit;
17161716
* 3. the iterated hashing of the 512-bit string of zero bits a large number of times.
17171717
*/
1718-
void makeNESSIETestVectors() {
1718+
void makeNESSIETestVectors(void) {
17191719
int i;
17201720
struct NESSIEstruct w;
17211721
u8 digest[DIGESTBYTES];
@@ -1758,7 +1758,7 @@ void makeNESSIETestVectors() {
17581758
/*
17591759
#define TIMING_ITERATIONS 100000
17601760
1761-
static void timing() {
1761+
static void timing(void) {
17621762
int i;
17631763
NESSIEstruct w;
17641764
u8 digest[DIGESTBYTES];
@@ -1824,7 +1824,7 @@ void testAPI(void) {
18241824
}
18251825
NESSIEfinalize(&w, computedDigest);
18261826
if (memcmp(computedDigest, expectedDigest, DIGESTBYTES) != 0) {
1827-
fprintf(stderr, "API error @ pieceLen = %lu\n", pieceLen);
1827+
fprintf(stderr, "API error @ pieceLen = %u\n", pieceLen);
18281828
display(computedDigest, DIGESTBYTES); printf("\n\n");
18291829
display(expectedDigest, DIGESTBYTES); printf("\n\n");
18301830
return;
@@ -1842,7 +1842,7 @@ void testAPI(void) {
18421842
printf("No error detected.\n");
18431843
}
18441844

1845-
void makeISOTestVectors() {
1845+
void makeISOTestVectors(void) {
18461846
struct NESSIEstruct w;
18471847
u8 digest[DIGESTBYTES];
18481848
static u8 data[1000000];
@@ -1858,49 +1858,49 @@ void makeISOTestVectors() {
18581858

18591859
printf("2. In this example the data-string consists of a single byte, namely the ASCII-coded version of the letter 'a'.\n\n");
18601860
NESSIEinit(&w);
1861-
NESSIEadd("a", 8*1, &w);
1861+
NESSIEadd((u8*)"a", 8*1, &w);
18621862
NESSIEfinalize(&w, digest);
18631863
printf("The hash-code is the following 512-bit string.\n\n");
18641864
display(digest, DIGESTBYTES); printf("\n\n");
18651865

18661866
printf("3. In this example the data-string is the three-byte string consisting of the ASCII-coded version of 'abc'.\n\n");
18671867
NESSIEinit(&w);
1868-
NESSIEadd("abc", 8*3, &w);
1868+
NESSIEadd((u8*)"abc", 8*3, &w);
18691869
NESSIEfinalize(&w, digest);
18701870
printf("The hash-code is the following 512-bit string.\n\n");
18711871
display(digest, DIGESTBYTES); printf("\n\n");
18721872

18731873
printf("4. In this example the data-string is the 14-byte string consisting of the ASCII-coded version of 'message digest'.\n\n");
18741874
NESSIEinit(&w);
1875-
NESSIEadd("message digest", 8*14, &w);
1875+
NESSIEadd((u8*)"message digest", 8*14, &w);
18761876
NESSIEfinalize(&w, digest);
18771877
printf("The hash-code is the following 512-bit string.\n\n");
18781878
display(digest, DIGESTBYTES); printf("\n\n");
18791879

18801880
printf("5. In this example the data-string is the 26-byte string consisting of the ASCII-coded version of 'abcdefghijklmnopqrstuvwxyz'.\n\n");
18811881
NESSIEinit(&w);
1882-
NESSIEadd("abcdefghijklmnopqrstuvwxyz", 8*26, &w);
1882+
NESSIEadd((u8*)"abcdefghijklmnopqrstuvwxyz", 8*26, &w);
18831883
NESSIEfinalize(&w, digest);
18841884
printf("The hash-code is the following 512-bit string.\n\n");
18851885
display(digest, DIGESTBYTES); printf("\n\n");
18861886

18871887
printf("6. In this example the data-string is the 62-byte string consisting of the ASCII-coded version of 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.\n\n");
18881888
NESSIEinit(&w);
1889-
NESSIEadd("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 8*62, &w);
1889+
NESSIEadd((u8*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 8*62, &w);
18901890
NESSIEfinalize(&w, digest);
18911891
printf("The hash-code is the following 512-bit string.\n\n");
18921892
display(digest, DIGESTBYTES); printf("\n\n");
18931893

18941894
printf("7. In this example the data-string is the 80-byte string consisting of the ASCII-coded version of eight repetitions of '1234567890'.\n\n");
18951895
NESSIEinit(&w);
1896-
NESSIEadd("12345678901234567890123456789012345678901234567890123456789012345678901234567890", 8*80, &w);
1896+
NESSIEadd((u8*)"12345678901234567890123456789012345678901234567890123456789012345678901234567890", 8*80, &w);
18971897
NESSIEfinalize(&w, digest);
18981898
printf("The hash-code is the following 512-bit string.\n\n");
18991899
display(digest, DIGESTBYTES); printf("\n\n");
19001900

19011901
printf("8. In this example the data-string is the 32-byte string consisting of the ASCII-coded version of 'abcdbcdecdefdefgefghfghighijhijk'.\n\n");
19021902
NESSIEinit(&w);
1903-
NESSIEadd("abcdbcdecdefdefgefghfghighijhijk", 8*32, &w);
1903+
NESSIEadd((u8*)"abcdbcdecdefdefgefghfghighijhijk", 8*32, &w);
19041904
NESSIEfinalize(&w, digest);
19051905
printf("The hash-code is the following 512-bit string.\n\n");
19061906
display(digest, DIGESTBYTES); printf("\n\n");
@@ -1920,7 +1920,7 @@ void makeISOTestVectors() {
19201920
}
19211921

19221922
#ifdef TRACE_INTERMEDIATE_VALUES
1923-
static void makeIntermediateValues() {
1923+
static void makeIntermediateValues(void) {
19241924
struct NESSIEstruct w;
19251925
u8 digest[DIGESTBYTES];
19261926

0 commit comments

Comments
 (0)