-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.cpp
71 lines (57 loc) · 1.58 KB
/
hash.cpp
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
#include <gcrypt.h>
#include <iostream>
#include "hash.h"
#include "miracl.h"
#define KEY_SIZE 16
#define SHA256_DIGEST_LENGTH 32
#define TRUE 1
#define FALSE 0
// void uocrypt_error(gcry_error_t err)
// {
// if (err)
// {
// fprintf(stderr, "Failure: %s/%s\n", gcry_strsource(err), gcry_strerror(err));
// abort();
// }
// }
// void KeyedHash(uint8_t *key, int keyLength, uint8_t *data, int dataLength, uint8_t *mac, int macLength)
// {
// gcry_md_hd_t hd;
// // gcry_error_t err = GPG_ERR_NO_ERROR;
// gcry_md_open(&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
// // uocrypt_error(err);
// gcry_md_setkey(hd, key, keyLength); //keyLength must be 32 bytes or less
// // uocrypt_error(err);
// gcry_md_write(hd, data, dataLength);
// uint8_t *p = gcry_md_read(hd, GCRY_MD_SHA256);
// memcpy(mac, p, SHA256_DIGEST_LENGTH);
// gcry_md_close(hd);
// }
void KeyedHash(uint8_t *key, int keyLength, uint8_t *data, int dataLength, uint8_t *mac, int macLength)
{
int i;
sha256 md;
shs256_init(&md); //The SHA for hashing
for(i=0; i<keyLength; i++)
shs256_process(&md, key[i]);
for(i=0; i<dataLength; i++)
shs256_process(&md, data[i]);
shs256_hash(&md, (char*)mac);
}
int permutation(int seed, bool *array, int len)
{
srand(seed);
size_t j;
if (len > 1)
{
size_t i;
for (i = len - 1; i > 0; i--)
{
j = (unsigned int)rand() % i;
bool t = array[j];
array[j] = array[i];
array[i] = t;
}
}
return seed;
}