-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHash.h
104 lines (72 loc) · 2.85 KB
/
Hash.h
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright (c) 2015 Colum Paget <[email protected]>
* SPDX-License-Identifier: GPL-3.0
*/
#ifndef LIBUSEFUL_HASH_H
#define LIBUSEFUL_HASH_H
/* Hashing functions. Mostly you'll use:
len=HashBytes(&Hashed, "md5", "string to hash", 14, ENCODE_HEX);
or
len=HashFile(&Hashed, "sha1", "/tmp/filetohash.txt", ENCODE_BASE64);
You can get a list of available hash types with HashAvailableTypes. This should be MD5, various SHA variants, whirlpool, and variants of the jh hash
from Dr Hongjun Wu. More hash algorithms will likely be added in future.
if you need more granulated control of hashing, then it's done like this
Hash=HashInit("md5");
S=STREAMOpen("/tmp/testfile.txt","r");
result=STREAMReadBytes(S, Buffer BUFSIZ);
while (result > 0)
{
HashUpdate(Hash, Buffer, result);
result=STREAMReadBytes(S, Buffer BUFSIZ);
}
len=HashFinish(Hash, ENCODE_BASE64, HashStr);
printf("Hash was: %s\n",HashStr);
*/
//if you load Hash.h you'll want Encodings.h too
#include "Encodings.h"
#include "Stream.h"
#include "includes.h"
#define HashUpdate(Hash, text, len) (Hash->Update(Hash, text, len))
#ifdef __cplusplus
extern "C" {
#endif
typedef struct t_hash HASH;
typedef int (*HASH_INIT_FUNC)(HASH *Hash, const char *Name, int Bits);
typedef void (*HASH_UPDATE_FUNC)(HASH *Hash, const char *Data, int DataLen);
typedef HASH *(*HASH_CLONE_FUNC)(HASH *Hash);
typedef int (*HASH_FINISH_FUNC)(HASH *Hash, char **RetStr);
struct t_hash
{
char *Type;
//this is a placeholder used only by classlibraries for scripting langauges
int Encoding;
char *Key1;
unsigned int Key1Len;
char *Key2;
unsigned int Key2Len;
void *Ctx;
HASH_INIT_FUNC Init;
HASH_UPDATE_FUNC Update;
HASH_FINISH_FUNC Finish;
HASH_CLONE_FUNC Clone;
};
//this function is used internally to register a hash type
void HashRegister(const char *Name, int Len, HASH_INIT_FUNC Init);
//for backwards compatiblity provide this function. It just calls'EncodingParse'
int HashEncodingFromStr(const char *Str);
//produce a comma separated list of available hash types. This can include hash-names from external sources like openssl.
char *HashAvailableTypes(char *RetStr);
HASH *HashInit(const char *Type);
int HashFinish(HASH *Hash, int Encoding, char **Return);
void HashDestroy(HASH *Hash);
int HashBytes(char **Return, const char *Type, const char *text, int len, int Encoding);
int HashBytes2(const char *Type, int Encoding, const char *text, int len, char **RetStr);
//read from a stream (usually a file) and return the hash of it's contents
int HashSTREAM(char **Return, const char *Type, STREAM *S, int Encoding);
//hash a file at 'Path'
int HashFile(char **Return, const char *Type, const char *Path, int Encoding);
int PBK2DF2(char **Return, const char *Type, const char *Bytes, int Len, const char *Salt, int SaltLen, uint32_t Rounds, int Encoding);
#ifdef __cplusplus
}
#endif
#endif