Skip to content

Commit 9d6dcdf

Browse files
authored
Merge pull request wolfSSL#496 from SparkiDev/hash_examples_update
Hash examples: add more
2 parents feca698 + ae63bfd commit 9d6dcdf

10 files changed

Lines changed: 719 additions & 4 deletions

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,14 @@ x509_acert/pkey_new.pem
288288
x509_acert/openssl_acert
289289
x509_acert/wolfssl_acert
290290

291+
hash/hash-file
291292
hash/sha256-hash
293+
hash/sha256-hash-oneshot-string
294+
hash/sha256-hash-string
295+
hash/sha3-256-hash
296+
hash/sha3-256-hash-oneshot-string
297+
hash/sha3-256-hash-string
298+
hash/sha512-hash
292299

293300
ocsp/ocsp_nonblock/ocsp_nonblock
294301

hash/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Hash Examples Makefile
2-
CC = gcc -fsanitize=address
2+
CC = gcc
33
WOLFSSL_INSTALL_DIR = /usr/local
44
CFLAGS = -Wall -I$(WOLFSSL_INSTALL_DIR)/include
55
LIBS = -L$(WOLFSSL_INSTALL_DIR)/lib -lm

hash/README.md

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,80 @@ LIBS+=$(STATIC_LIB)
3939

4040
### `sha256-hash`
4141

42-
This example shows how to hash an input file using sha256.
42+
This example shows how to hash an input file using SHA-256.
4343

4444
```
45-
./sha256 input.txt
45+
./sha256-hash input.txt
4646
Hash input file input.txt
47-
Hash result is: c279d0c6e7308e9401d3e5ff217fd03af404b3fe6b2e5028aa5138714e85b599
47+
Hash result is: 75294625788129796c09fcbf313ea16e2883356e322adc2f956b37dbdc10b6a7
48+
```
49+
50+
### `sha512-hash`
51+
52+
This example shows how to hash an input file using SHA-512.
53+
54+
```
55+
./sha512-hash input.txt
56+
Hash input file input.txt
57+
Hash result is: ead56209da2dfb3562263aadc57d9382f0f7cb579ebb6dbf2f20bfd3cb68aaaad422f6ce6f1a88ec6c326edcf8456f650579b6e20eb39f3bb444bee8b65615ed
58+
```
59+
60+
### `sha3-256-hash`
61+
62+
This example shows how to hash an input file using SHA3-256.
63+
64+
```
65+
./sha3-256-hash input.txt
66+
Hash input file input.txt
67+
Hash result is: 0704c6ca55e7e5c706b543f07da1daed8149c838549096df6a52dac5f95f2fe0
68+
```
69+
70+
### `hash-file`
71+
72+
This example shows how to hash an input file using hash wrapper API.
73+
74+
```
75+
./hash-file SHA256 input.txt
76+
Hash input file input.txt
77+
Hash result is: 75294625788129796c09fcbf313ea16e2883356e322adc2f956b37dbdc10b6a7
78+
79+
./hash-file SHA512 input.txt
80+
Hash input file input.txt
81+
Hash result is: ead56209da2dfb3562263aadc57d9382f0f7cb579ebb6dbf2f20bfd3cb68aaaad422f6ce6f1a88ec6c326edcf8456f650579b6e20eb39f3bb444bee8b65615ed
82+
83+
./hash-file SHA3-256 input.txt
84+
Hash input file input.txt
85+
Hash result is: 0704c6ca55e7e5c706b543f07da1daed8149c838549096df6a52dac5f95f2fe0
86+
```
87+
88+
### `sha256-hash-string`
89+
90+
This example shows how to hash a string using SHA256.
91+
92+
```
93+
./sha256-hash-string
94+
String to hash: 'String to hash'
95+
Hash result is: d4476d30fd94c746eb38d8a1b3931aa81d1e485be5a6362f47598017a91cb5d2
96+
```
97+
98+
### `sha256-hash-oneshot-string`
99+
100+
This example shows how to hash a string using wolfSSL's SHA-256 oneshot API.
101+
102+
```
103+
./sha256-hash-oneshot-string
104+
String to hash: 'String to hash'
105+
Hash result is: d4476d30fd94c746eb38d8a1b3931aa81d1e485be5a6362f47598017a91cb5d2
106+
```
107+
108+
### `sha3-256-hash-oneshot-string`
109+
110+
This example shows how to hash a string using wolfSSL's SHA3-256 oneshot API.
111+
112+
```
113+
./sha3-256-hash-oneshot-string
114+
String to hash: 'String to hash'
115+
Hash result is: 10d69e59ac10b1d81755733f323bdadca3e04e4b17df72f5b343d6da701a4225
48116
```
49117

50118
## Support

hash/hash-file.c

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/* hash-file.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
23+
#ifndef WOLFSSL_USER_SETTINGS
24+
#include <wolfssl/options.h>
25+
#endif
26+
#include <wolfssl/wolfcrypt/settings.h>
27+
#include <wolfssl/ssl.h>
28+
#include <wolfssl/wolfcrypt/hash.h>
29+
#include <wolfssl/wolfcrypt/error-crypt.h>
30+
31+
#ifndef CHUNK_SIZE
32+
#define CHUNK_SIZE 1024
33+
#endif
34+
35+
#ifndef NO_HASH_WRAPPERS
36+
enum wc_HashType hash_type_from_string(char* name)
37+
{
38+
if (strcmp(name, "MD2") == 0) {
39+
return WC_HASH_TYPE_MD2;
40+
}
41+
else if (strcmp(name, "MD4") == 0) {
42+
return WC_HASH_TYPE_MD4;
43+
}
44+
else if (strcmp(name, "MD5") == 0) {
45+
return WC_HASH_TYPE_MD5;
46+
}
47+
else if (strcmp(name, "SHA") == 0) {
48+
return WC_HASH_TYPE_SHA;
49+
}
50+
else if (strcmp(name, "SHA224") == 0) {
51+
return WC_HASH_TYPE_SHA224;
52+
}
53+
else if (strcmp(name, "SHA256") == 0) {
54+
return WC_HASH_TYPE_SHA256;
55+
}
56+
else if (strcmp(name, "SHA384") == 0) {
57+
return WC_HASH_TYPE_SHA384;
58+
}
59+
else if (strcmp(name, "SHA512") == 0) {
60+
return WC_HASH_TYPE_SHA512;
61+
}
62+
else if (strcmp(name, "SHA3-224") == 0) {
63+
return WC_HASH_TYPE_SHA3_224;
64+
}
65+
else if (strcmp(name, "SHA3-256") == 0) {
66+
return WC_HASH_TYPE_SHA3_256;
67+
}
68+
else if (strcmp(name, "SHA3-384") == 0) {
69+
return WC_HASH_TYPE_SHA3_384;
70+
}
71+
else if (strcmp(name, "SHA3-512") == 0) {
72+
return WC_HASH_TYPE_SHA3_512;
73+
}
74+
else if (strcmp(name, "BLAKE2B") == 0) {
75+
return WC_HASH_TYPE_BLAKE2B;
76+
}
77+
else if (strcmp(name, "BLAKE2S") == 0) {
78+
return WC_HASH_TYPE_BLAKE2S;
79+
}
80+
#ifndef WOLFSSL_NOSHA512_224
81+
else if (strcmp(name, "SHA512-224") == 0) {
82+
return WC_HASH_TYPE_SHA512_224;
83+
}
84+
#endif
85+
#ifndef WOLFSSL_NOSHA512_256
86+
else if (strcmp(name, "SHA512-256") == 0) {
87+
return WC_HASH_TYPE_SHA512_256;
88+
}
89+
#endif
90+
#ifdef WOLFSSL_SHAKE128
91+
else if (strcmp(name, "SHAKE128") == 0) {
92+
return WC_HASH_TYPE_SHAKE128;
93+
}
94+
#endif
95+
#ifdef WOLFSSL_SHAKE256
96+
else if (strcmp(name, "SHAKE256") == 0) {
97+
return WC_HASH_TYPE_SHAKE256;
98+
}
99+
#endif
100+
#ifdef WOLFSSL_SM3
101+
else if (strcmp(name, "SM3") == 0) {
102+
return WC_HASH_TYPE_SHAKE256;
103+
}
104+
#endif
105+
else {
106+
return WC_HASH_TYPE_NONE;
107+
}
108+
}
109+
110+
void print_wolfssl_error(const char* msg, int err)
111+
{
112+
#ifndef NO_ERROR_STRINGS
113+
printf("%s: %s (%d)\n", msg, wc_GetErrorString(err), err);
114+
#else
115+
printf("%s: %d\n", msg, err);
116+
#endif
117+
}
118+
119+
void usage(void)
120+
{
121+
printf("./hash-file <alg> <file to hash>\n");
122+
exit(-99);
123+
}
124+
#endif
125+
126+
int main(int argc, char** argv)
127+
{
128+
int ret = -1;
129+
#ifndef NO_HASH_WRAPPERS
130+
wc_HashAlg hashAlg;
131+
enum wc_HashType hashType;
132+
byte hash[WC_MAX_DIGEST_SIZE];
133+
byte rawInput[CHUNK_SIZE];
134+
FILE* inputStream;
135+
char* hashName = NULL;
136+
char* fName = NULL;
137+
int fileLength = 0;
138+
int i, chunkSz;
139+
140+
if (argc < 3)
141+
usage();
142+
hashName = argv[1];
143+
fName = argv[2];
144+
145+
printf("Hash algorithme %s\n", hashName);
146+
hashType = hash_type_from_string(hashName);
147+
if (hashType == WC_HASH_TYPE_NONE) {
148+
printf("ERROR: hash algorithm not recognized.\n");
149+
return -1;
150+
}
151+
152+
printf("Hash input file %s\n", fName);
153+
inputStream = fopen(fName, "rb");
154+
if (inputStream == NULL) {
155+
printf("ERROR: Unable to open file\n");
156+
return -1;
157+
}
158+
159+
/* find length of the file */
160+
fseek(inputStream, 0, SEEK_END);
161+
fileLength = (int) ftell(inputStream);
162+
fseek(inputStream, 0, SEEK_SET);
163+
164+
ret = wc_HashInit(&hashAlg, hashType);
165+
if (ret != 0) {
166+
print_wolfssl_error("Failed to initialize hash structure", ret);
167+
fclose(inputStream);
168+
return -1;
169+
}
170+
171+
/* Loop reading a block at a time, finishing with any excess */
172+
for (i = 0; i < fileLength; i += CHUNK_SIZE) {
173+
chunkSz = CHUNK_SIZE;
174+
if (chunkSz > fileLength - i)
175+
chunkSz = fileLength - i;
176+
177+
ret = fread(rawInput, 1, chunkSz, inputStream);
178+
if (ret != chunkSz) {
179+
printf("ERROR: Failed to read the appropriate amount\n");
180+
ret = -1;
181+
break;
182+
}
183+
184+
ret = wc_HashUpdate(&hashAlg, hashType, rawInput, chunkSz);
185+
if (ret != 0) {
186+
print_wolfssl_error("Failed to update the hash", ret);
187+
break;
188+
}
189+
}
190+
191+
if (ret == 0) {
192+
ret = wc_HashFinal(&hashAlg, hashType, hash);
193+
if (ret != 0) {
194+
print_wolfssl_error("Failed to generate hash", ret);
195+
}
196+
}
197+
198+
if (ret != 0) {
199+
printf("ERROR: Hash operation failed");
200+
}
201+
else {
202+
printf("Hash result is: ");
203+
int sz = wc_HashGetDigestSize(hashType);
204+
for (i = 0; i < sz; i++)
205+
printf("%02x", hash[i]);
206+
printf("\n");
207+
}
208+
209+
fclose(inputStream);
210+
wc_HashFree(&hashAlg, hashType);
211+
#else
212+
printf("Please remove NO_HASH_WRAPPERS from wolfCrypt configuration\n");
213+
#endif
214+
return ret;
215+
}

hash/input.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras ullamcorper justo iaculis, fringilla felis a, finibus dui. Proin sed mattis diam, in blandit ante. Interdum et malesuada fames ac ante ipsum primis in faucibus. Mauris quis purus sit amet dolor ullamcorper ultricies. Sed id sem non augue tincidunt sagittis et et leo. Etiam imperdiet, tortor a pretium luctus, mauris urna mollis dolor, a vehicula neque quam non tellus. Fusce vel elementum nibh. Nam egestas urna velit, quis molestie tortor tempus ut. Proin convallis tincidunt purus, vitae condimentum felis. Cras sed erat ac quam tincidunt ullamcorper sit amet id quam. Suspendisse blandit lacinia ex. Suspendisse turpis diam, dapibus et gravida ac, commodo eget ante. Nunc egestas ex id ultricies blandit. Nullam eget dui lacinia dui lacinia vestibulum. Pellentesque pulvinar nunc sit amet feugiat sollicitudin.
4+
5+
Nulla id neque ac lorem facilisis elementum pulvinar non tortor. Nulla nisi est, feugiat in lorem vestibulum, dapibus vehicula justo. Pellentesque leo enim, dapibus nec blandit eget, commodo vitae nibh. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; In hac habitasse platea dictumst. Nam posuere facilisis massa suscipit ultrices. Aliquam lobortis interdum elit vitae vehicula. Maecenas tincidunt mauris in accumsan sagittis. Vivamus vitae turpis quis elit fermentum placerat in sed ligula. Morbi efficitur est a vehicula malesuada. Donec tincidunt mattis aliquam.
6+
7+
In quam erat, facilisis nec convallis sed, venenatis vitae eros. Nam euismod dictum mollis. Nulla facilisi. Nullam ac lacus ac nunc euismod tincidunt. Pellentesque aliquam felis tortor, ut vehicula tellus hendrerit rhoncus. Phasellus sed urna ut nisl pretium lacinia. Suspendisse potenti. In nibh libero, sollicitudin in odio eu, ultrices pulvinar nisi.
8+
9+
Morbi vitae pellentesque enim. Ut nisl turpis, viverra a aliquet et, varius ac odio. Curabitur a lorem condimentum orci consectetur volutpat. Cras pellentesque eleifend orci sed blandit. Nulla venenatis libero sit amet tellus interdum, vel rhoncus lacus venenatis. Quisque purus leo, viverra vel auctor vel, fermentum sit amet dolor. Suspendisse potenti. In id orci semper, aliquet felis sit amet, convallis sem. Duis urna eros, condimentum eget mauris sed, tempus vehicula est. Fusce pharetra at tortor vel lacinia. Aliquam placerat porta ligula ac egestas. Nam est lorem, interdum ut faucibus nec, euismod quis ante. Vestibulum eget vulputate metus, vitae vestibulum risus. Proin ullamcorper libero dapibus urna sollicitudin laoreet. Vivamus sodales sollicitudin maximus. Nunc sem orci, suscipit a sagittis sit amet, lacinia quis felis.
10+
11+
Morbi sollicitudin odio nec malesuada rhoncus. Etiam ullamcorper augue nisl, eget fermentum sapien elementum non. Integer lobortis mi augue, a tincidunt purus rhoncus in. Phasellus dapibus mauris a mi sodales, commodo consectetur sapien hendrerit. Integer mattis turpis quam, ac consectetur diam condimentum ac. Cras ut dapibus dui. Duis ut cursus nunc. Etiam euismod nibh iaculis tristique scelerisque. Ut rutrum nisl ipsum, non elementum eros tincidunt quis. Praesent id vestibulum justo.

hash/sha256-hash-oneshot-string.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* sha256-hash-oneshot-string.c
2+
*
3+
* Copyright (C) 2006-2020 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
*/
21+
22+
23+
#ifndef WOLFSSL_USER_SETTINGS
24+
#include <wolfssl/options.h>
25+
#endif
26+
#include <wolfssl/wolfcrypt/settings.h>
27+
#include <wolfssl/wolfcrypt/hash.h>
28+
#include <wolfssl/wolfcrypt/error-crypt.h>
29+
30+
int main(int argc, char** argv)
31+
{
32+
int ret = -1;
33+
#ifndef NO_SHA256
34+
unsigned char hash[WC_SHA256_DIGEST_SIZE];
35+
const char* string = "String to hash";
36+
int i;
37+
38+
printf("String to hash: '%s'\n", string);
39+
40+
ret = wc_Sha256Hash((unsigned char*)string, XSTRLEN(string), hash);
41+
if (ret != 0) {
42+
printf("ERROR: Hash operation failed");
43+
goto prog_end;
44+
}
45+
46+
printf("Hash result is: ");
47+
for (i = 0; i < WC_SHA256_DIGEST_SIZE; i++)
48+
printf("%02x", hash[i]);
49+
printf("\n");
50+
51+
prog_end:
52+
#else
53+
printf("Please enable sha256 (--enable-sha256) in wolfCrypt\n");
54+
#endif
55+
return ret;
56+
}

0 commit comments

Comments
 (0)