Hash examples: add more
More examples based on different hash algorithms. Examples hashing a string rather than a file. Examples showing oneshot API. Example of using hash wrapping API.pull/496/head
parent
dc36abdfd4
commit
ae63bfdb40
|
@ -288,7 +288,14 @@ x509_acert/pkey_new.pem
|
|||
x509_acert/openssl_acert
|
||||
x509_acert/wolfssl_acert
|
||||
|
||||
hash/hash-file
|
||||
hash/sha256-hash
|
||||
hash/sha256-hash-oneshot-string
|
||||
hash/sha256-hash-string
|
||||
hash/sha3-256-hash
|
||||
hash/sha3-256-hash-oneshot-string
|
||||
hash/sha3-256-hash-string
|
||||
hash/sha512-hash
|
||||
|
||||
ocsp/ocsp_nonblock/ocsp_nonblock
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# Hash Examples Makefile
|
||||
CC = gcc -fsanitize=address
|
||||
CC = gcc
|
||||
WOLFSSL_INSTALL_DIR = /usr/local
|
||||
CFLAGS = -Wall -I$(WOLFSSL_INSTALL_DIR)/include
|
||||
LIBS = -L$(WOLFSSL_INSTALL_DIR)/lib -lm
|
||||
|
|
|
@ -39,12 +39,80 @@ LIBS+=$(STATIC_LIB)
|
|||
|
||||
### `sha256-hash`
|
||||
|
||||
This example shows how to hash an input file using sha256.
|
||||
This example shows how to hash an input file using SHA-256.
|
||||
|
||||
```
|
||||
./sha256 input.txt
|
||||
./sha256-hash input.txt
|
||||
Hash input file input.txt
|
||||
Hash result is: c279d0c6e7308e9401d3e5ff217fd03af404b3fe6b2e5028aa5138714e85b599
|
||||
Hash result is: 75294625788129796c09fcbf313ea16e2883356e322adc2f956b37dbdc10b6a7
|
||||
```
|
||||
|
||||
### `sha512-hash`
|
||||
|
||||
This example shows how to hash an input file using SHA-512.
|
||||
|
||||
```
|
||||
./sha512-hash input.txt
|
||||
Hash input file input.txt
|
||||
Hash result is: ead56209da2dfb3562263aadc57d9382f0f7cb579ebb6dbf2f20bfd3cb68aaaad422f6ce6f1a88ec6c326edcf8456f650579b6e20eb39f3bb444bee8b65615ed
|
||||
```
|
||||
|
||||
### `sha3-256-hash`
|
||||
|
||||
This example shows how to hash an input file using SHA3-256.
|
||||
|
||||
```
|
||||
./sha3-256-hash input.txt
|
||||
Hash input file input.txt
|
||||
Hash result is: 0704c6ca55e7e5c706b543f07da1daed8149c838549096df6a52dac5f95f2fe0
|
||||
```
|
||||
|
||||
### `hash-file`
|
||||
|
||||
This example shows how to hash an input file using hash wrapper API.
|
||||
|
||||
```
|
||||
./hash-file SHA256 input.txt
|
||||
Hash input file input.txt
|
||||
Hash result is: 75294625788129796c09fcbf313ea16e2883356e322adc2f956b37dbdc10b6a7
|
||||
|
||||
./hash-file SHA512 input.txt
|
||||
Hash input file input.txt
|
||||
Hash result is: ead56209da2dfb3562263aadc57d9382f0f7cb579ebb6dbf2f20bfd3cb68aaaad422f6ce6f1a88ec6c326edcf8456f650579b6e20eb39f3bb444bee8b65615ed
|
||||
|
||||
./hash-file SHA3-256 input.txt
|
||||
Hash input file input.txt
|
||||
Hash result is: 0704c6ca55e7e5c706b543f07da1daed8149c838549096df6a52dac5f95f2fe0
|
||||
```
|
||||
|
||||
### `sha256-hash-string`
|
||||
|
||||
This example shows how to hash a string using SHA256.
|
||||
|
||||
```
|
||||
./sha256-hash-string
|
||||
String to hash: 'String to hash'
|
||||
Hash result is: d4476d30fd94c746eb38d8a1b3931aa81d1e485be5a6362f47598017a91cb5d2
|
||||
```
|
||||
|
||||
### `sha256-hash-oneshot-string`
|
||||
|
||||
This example shows how to hash a string using wolfSSL's SHA-256 oneshot API.
|
||||
|
||||
```
|
||||
./sha256-hash-oneshot-string
|
||||
String to hash: 'String to hash'
|
||||
Hash result is: d4476d30fd94c746eb38d8a1b3931aa81d1e485be5a6362f47598017a91cb5d2
|
||||
```
|
||||
|
||||
### `sha3-256-hash-oneshot-string`
|
||||
|
||||
This example shows how to hash a string using wolfSSL's SHA3-256 oneshot API.
|
||||
|
||||
```
|
||||
./sha3-256-hash-oneshot-string
|
||||
String to hash: 'String to hash'
|
||||
Hash result is: 10d69e59ac10b1d81755733f323bdadca3e04e4b17df72f5b343d6da701a4225
|
||||
```
|
||||
|
||||
## Support
|
||||
|
|
|
@ -0,0 +1,215 @@
|
|||
/* hash-file.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/wolfcrypt/hash.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifndef CHUNK_SIZE
|
||||
#define CHUNK_SIZE 1024
|
||||
#endif
|
||||
|
||||
#ifndef NO_HASH_WRAPPERS
|
||||
enum wc_HashType hash_type_from_string(char* name)
|
||||
{
|
||||
if (strcmp(name, "MD2") == 0) {
|
||||
return WC_HASH_TYPE_MD2;
|
||||
}
|
||||
else if (strcmp(name, "MD4") == 0) {
|
||||
return WC_HASH_TYPE_MD4;
|
||||
}
|
||||
else if (strcmp(name, "MD5") == 0) {
|
||||
return WC_HASH_TYPE_MD5;
|
||||
}
|
||||
else if (strcmp(name, "SHA") == 0) {
|
||||
return WC_HASH_TYPE_SHA;
|
||||
}
|
||||
else if (strcmp(name, "SHA224") == 0) {
|
||||
return WC_HASH_TYPE_SHA224;
|
||||
}
|
||||
else if (strcmp(name, "SHA256") == 0) {
|
||||
return WC_HASH_TYPE_SHA256;
|
||||
}
|
||||
else if (strcmp(name, "SHA384") == 0) {
|
||||
return WC_HASH_TYPE_SHA384;
|
||||
}
|
||||
else if (strcmp(name, "SHA512") == 0) {
|
||||
return WC_HASH_TYPE_SHA512;
|
||||
}
|
||||
else if (strcmp(name, "SHA3-224") == 0) {
|
||||
return WC_HASH_TYPE_SHA3_224;
|
||||
}
|
||||
else if (strcmp(name, "SHA3-256") == 0) {
|
||||
return WC_HASH_TYPE_SHA3_256;
|
||||
}
|
||||
else if (strcmp(name, "SHA3-384") == 0) {
|
||||
return WC_HASH_TYPE_SHA3_384;
|
||||
}
|
||||
else if (strcmp(name, "SHA3-512") == 0) {
|
||||
return WC_HASH_TYPE_SHA3_512;
|
||||
}
|
||||
else if (strcmp(name, "BLAKE2B") == 0) {
|
||||
return WC_HASH_TYPE_BLAKE2B;
|
||||
}
|
||||
else if (strcmp(name, "BLAKE2S") == 0) {
|
||||
return WC_HASH_TYPE_BLAKE2S;
|
||||
}
|
||||
#ifndef WOLFSSL_NOSHA512_224
|
||||
else if (strcmp(name, "SHA512-224") == 0) {
|
||||
return WC_HASH_TYPE_SHA512_224;
|
||||
}
|
||||
#endif
|
||||
#ifndef WOLFSSL_NOSHA512_256
|
||||
else if (strcmp(name, "SHA512-256") == 0) {
|
||||
return WC_HASH_TYPE_SHA512_256;
|
||||
}
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHAKE128
|
||||
else if (strcmp(name, "SHAKE128") == 0) {
|
||||
return WC_HASH_TYPE_SHAKE128;
|
||||
}
|
||||
#endif
|
||||
#ifdef WOLFSSL_SHAKE256
|
||||
else if (strcmp(name, "SHAKE256") == 0) {
|
||||
return WC_HASH_TYPE_SHAKE256;
|
||||
}
|
||||
#endif
|
||||
#ifdef WOLFSSL_SM3
|
||||
else if (strcmp(name, "SM3") == 0) {
|
||||
return WC_HASH_TYPE_SHAKE256;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
return WC_HASH_TYPE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
void print_wolfssl_error(const char* msg, int err)
|
||||
{
|
||||
#ifndef NO_ERROR_STRINGS
|
||||
printf("%s: %s (%d)\n", msg, wc_GetErrorString(err), err);
|
||||
#else
|
||||
printf("%s: %d\n", msg, err);
|
||||
#endif
|
||||
}
|
||||
|
||||
void usage(void)
|
||||
{
|
||||
printf("./hash-file <alg> <file to hash>\n");
|
||||
exit(-99);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifndef NO_HASH_WRAPPERS
|
||||
wc_HashAlg hashAlg;
|
||||
enum wc_HashType hashType;
|
||||
byte hash[WC_MAX_DIGEST_SIZE];
|
||||
byte rawInput[CHUNK_SIZE];
|
||||
FILE* inputStream;
|
||||
char* hashName = NULL;
|
||||
char* fName = NULL;
|
||||
int fileLength = 0;
|
||||
int i, chunkSz;
|
||||
|
||||
if (argc < 3)
|
||||
usage();
|
||||
hashName = argv[1];
|
||||
fName = argv[2];
|
||||
|
||||
printf("Hash algorithme %s\n", hashName);
|
||||
hashType = hash_type_from_string(hashName);
|
||||
if (hashType == WC_HASH_TYPE_NONE) {
|
||||
printf("ERROR: hash algorithm not recognized.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Hash input file %s\n", fName);
|
||||
inputStream = fopen(fName, "rb");
|
||||
if (inputStream == NULL) {
|
||||
printf("ERROR: Unable to open file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* find length of the file */
|
||||
fseek(inputStream, 0, SEEK_END);
|
||||
fileLength = (int) ftell(inputStream);
|
||||
fseek(inputStream, 0, SEEK_SET);
|
||||
|
||||
ret = wc_HashInit(&hashAlg, hashType);
|
||||
if (ret != 0) {
|
||||
print_wolfssl_error("Failed to initialize hash structure", ret);
|
||||
fclose(inputStream);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Loop reading a block at a time, finishing with any excess */
|
||||
for (i = 0; i < fileLength; i += CHUNK_SIZE) {
|
||||
chunkSz = CHUNK_SIZE;
|
||||
if (chunkSz > fileLength - i)
|
||||
chunkSz = fileLength - i;
|
||||
|
||||
ret = fread(rawInput, 1, chunkSz, inputStream);
|
||||
if (ret != chunkSz) {
|
||||
printf("ERROR: Failed to read the appropriate amount\n");
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
ret = wc_HashUpdate(&hashAlg, hashType, rawInput, chunkSz);
|
||||
if (ret != 0) {
|
||||
print_wolfssl_error("Failed to update the hash", ret);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ret = wc_HashFinal(&hashAlg, hashType, hash);
|
||||
if (ret != 0) {
|
||||
print_wolfssl_error("Failed to generate hash", ret);
|
||||
}
|
||||
}
|
||||
|
||||
if (ret != 0) {
|
||||
printf("ERROR: Hash operation failed");
|
||||
}
|
||||
else {
|
||||
printf("Hash result is: ");
|
||||
int sz = wc_HashGetDigestSize(hashType);
|
||||
for (i = 0; i < sz; i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
fclose(inputStream);
|
||||
wc_HashFree(&hashAlg, hashType);
|
||||
#else
|
||||
printf("Please remove NO_HASH_WRAPPERS from wolfCrypt configuration\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
|
@ -0,0 +1,56 @@
|
|||
/* sha256-hash-oneshot-string.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/hash.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifndef NO_SHA256
|
||||
unsigned char hash[WC_SHA256_DIGEST_SIZE];
|
||||
const char* string = "String to hash";
|
||||
int i;
|
||||
|
||||
printf("String to hash: '%s'\n", string);
|
||||
|
||||
ret = wc_Sha256Hash((unsigned char*)string, XSTRLEN(string), hash);
|
||||
if (ret != 0) {
|
||||
printf("ERROR: Hash operation failed");
|
||||
goto prog_end;
|
||||
}
|
||||
|
||||
printf("Hash result is: ");
|
||||
for (i = 0; i < WC_SHA256_DIGEST_SIZE; i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
|
||||
prog_end:
|
||||
#else
|
||||
printf("Please enable sha256 (--enable-sha256) in wolfCrypt\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
/* sha256-hash-string.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/sha256.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifndef NO_SHA256
|
||||
wc_Sha256 sha256;
|
||||
unsigned char hash[WC_SHA256_DIGEST_SIZE];
|
||||
const char* string = "String to hash";
|
||||
int i;
|
||||
|
||||
printf("String to hash: '%s'\n", string);
|
||||
|
||||
ret = wc_InitSha256(&sha256);
|
||||
if (ret != 0) {
|
||||
printf("Failed to initialize sha structure\n");
|
||||
goto prog_end;
|
||||
}
|
||||
|
||||
ret = wc_Sha256Update(&sha256, (unsigned char*)string, XSTRLEN(string));
|
||||
if (ret != 0) {
|
||||
printf("Failed to update the hash\n");
|
||||
goto prog_end;
|
||||
}
|
||||
|
||||
ret = wc_Sha256Final(&sha256, hash);
|
||||
if (ret != 0) {
|
||||
printf("ERROR: Hash operation failed");
|
||||
goto prog_end;
|
||||
}
|
||||
|
||||
printf("Hash result is: ");
|
||||
for (i = 0; i < WC_SHA256_DIGEST_SIZE; i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
|
||||
prog_end:
|
||||
wc_Sha256Free(&sha256);
|
||||
#else
|
||||
printf("Please enable sha256 (--enable-sha256) in wolfCrypt\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/* sha3-256-hash-oneshot-string.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/wolfcrypt/hash.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifdef WOLFSSL_SHA3
|
||||
unsigned char hash[WC_SHA3_256_DIGEST_SIZE];
|
||||
const char* string = "String to hash";
|
||||
int i;
|
||||
|
||||
printf("String to hash: '%s'\n", string);
|
||||
|
||||
ret = wc_Sha3_256Hash((unsigned char*)string, XSTRLEN(string), hash);
|
||||
if (ret != 0) {
|
||||
printf("ERROR: Hash operation failed");
|
||||
goto prog_end;
|
||||
}
|
||||
|
||||
printf("Hash result is: ");
|
||||
for (i = 0; i < WC_SHA3_256_DIGEST_SIZE; i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
|
||||
prog_end:
|
||||
#else
|
||||
printf("Please enable sha3 (--enable-sha3) in wolfCrypt\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
/* sha3-256-hash.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/wolfcrypt/sha3.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifndef CHUNK_SIZE
|
||||
#define CHUNK_SIZE 1024
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SHA3
|
||||
void usage(void)
|
||||
{
|
||||
printf("./sha3-256-hash <file to hash>\n");
|
||||
exit(-99);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifdef WOLFSSL_SHA3
|
||||
wc_Sha3 sha3;
|
||||
byte hash[WC_SHA3_256_DIGEST_SIZE];
|
||||
byte rawInput[CHUNK_SIZE];
|
||||
FILE* inputStream;
|
||||
char* fName = NULL;
|
||||
int fileLength = 0;
|
||||
int i, chunkSz;
|
||||
|
||||
if (argc < 2)
|
||||
usage();
|
||||
fName = argv[1];
|
||||
printf("Hash input file %s\n", fName);
|
||||
|
||||
inputStream = fopen(fName, "rb");
|
||||
if (inputStream == NULL) {
|
||||
printf("ERROR: Unable to open file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* find length of the file */
|
||||
fseek(inputStream, 0, SEEK_END);
|
||||
fileLength = (int) ftell(inputStream);
|
||||
fseek(inputStream, 0, SEEK_SET);
|
||||
|
||||
ret = wc_InitSha3_256(&sha3, NULL , INVALID_DEVID);
|
||||
if (ret != 0) {
|
||||
printf("Failed to initialize sha structure\n");
|
||||
fclose(inputStream);
|
||||
}
|
||||
|
||||
/* Loop reading a block at a time, finishing with any excess */
|
||||
for (i = 0; i < fileLength; i += CHUNK_SIZE) {
|
||||
chunkSz = CHUNK_SIZE;
|
||||
if (chunkSz > fileLength - i)
|
||||
chunkSz = fileLength - i;
|
||||
|
||||
ret = fread(rawInput, 1, chunkSz, inputStream);
|
||||
if (ret != chunkSz) {
|
||||
printf("ERROR: Failed to read the appropriate amount\n");
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
ret = wc_Sha3_256_Update(&sha3, rawInput, chunkSz);
|
||||
if (ret != 0) {
|
||||
printf("Failed to update the hash\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha3_256_Final(&sha3, hash);
|
||||
}
|
||||
if (ret != 0) {
|
||||
printf("ERROR: Hash operation failed");
|
||||
}
|
||||
else {
|
||||
printf("Hash result is: ");
|
||||
for (i = 0; i < WC_SHA3_256_DIGEST_SIZE; i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
fclose(inputStream);
|
||||
wc_Sha3_256_Free(&sha3);
|
||||
#else
|
||||
printf("Please enable sha3 (--enable-sha3) in wolfCrypt\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
/* sha512-hash.c
|
||||
*
|
||||
* Copyright (C) 2006-2020 wolfSSL Inc.
|
||||
*
|
||||
* This file is part of wolfSSL.
|
||||
*
|
||||
* wolfSSL is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* wolfSSL is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef WOLFSSL_USER_SETTINGS
|
||||
#include <wolfssl/options.h>
|
||||
#endif
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
#include <wolfssl/ssl.h>
|
||||
#include <wolfssl/wolfcrypt/sha512.h>
|
||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||
|
||||
#ifndef CHUNK_SIZE
|
||||
#define CHUNK_SIZE 1024
|
||||
#endif
|
||||
|
||||
#ifdef WOLFSSL_SHA512
|
||||
void usage(void)
|
||||
{
|
||||
printf("./sha512-hash <file to hash>\n");
|
||||
exit(-99);
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = -1;
|
||||
#ifdef WOLFSSL_SHA512
|
||||
wc_Sha512 sha512;
|
||||
byte hash[WC_SHA512_DIGEST_SIZE];
|
||||
byte rawInput[CHUNK_SIZE];
|
||||
FILE* inputStream;
|
||||
char* fName = NULL;
|
||||
int fileLength = 0;
|
||||
int i, chunkSz;
|
||||
|
||||
if (argc < 2)
|
||||
usage();
|
||||
fName = argv[1];
|
||||
printf("Hash input file %s\n", fName);
|
||||
|
||||
inputStream = fopen(fName, "rb");
|
||||
if (inputStream == NULL) {
|
||||
printf("ERROR: Unable to open file\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* find length of the file */
|
||||
fseek(inputStream, 0, SEEK_END);
|
||||
fileLength = (int) ftell(inputStream);
|
||||
fseek(inputStream, 0, SEEK_SET);
|
||||
|
||||
ret = wc_InitSha512(&sha512);
|
||||
if (ret != 0) {
|
||||
printf("Failed to initialize sha structure\n");
|
||||
fclose(inputStream);
|
||||
}
|
||||
|
||||
/* Loop reading a block at a time, finishing with any excess */
|
||||
for (i = 0; i < fileLength; i += CHUNK_SIZE) {
|
||||
chunkSz = CHUNK_SIZE;
|
||||
if (chunkSz > fileLength - i)
|
||||
chunkSz = fileLength - i;
|
||||
|
||||
ret = fread(rawInput, 1, chunkSz, inputStream);
|
||||
if (ret != chunkSz) {
|
||||
printf("ERROR: Failed to read the appropriate amount\n");
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
ret = wc_Sha512Update(&sha512, rawInput, chunkSz);
|
||||
if (ret != 0) {
|
||||
printf("Failed to update the hash\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret == 0) {
|
||||
ret = wc_Sha512Final(&sha512, hash);
|
||||
}
|
||||
if (ret != 0) {
|
||||
printf("ERROR: Hash operation failed");
|
||||
}
|
||||
else {
|
||||
printf("Hash result is: ");
|
||||
for (i = 0; i < WC_SHA512_DIGEST_SIZE; i++)
|
||||
printf("%02x", hash[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
fclose(inputStream);
|
||||
wc_Sha512Free(&sha512);
|
||||
#else
|
||||
printf("Please enable sha512 (--enable-sha512) in wolfCrypt\n");
|
||||
#endif
|
||||
return ret;
|
||||
}
|
Loading…
Reference in New Issue