-
Notifications
You must be signed in to change notification settings - Fork 11
/
dump_android_hash.h
105 lines (93 loc) · 2.96 KB
/
dump_android_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
#include <vector>
// hashing functions for android - sha1 and md5 are present in the bionic libc.a
// so we avoid having to link an external openssl library
typedef int (*PFN_Init)(void *state);
typedef int (*PFN_Update)(void *state, const unsigned char *data, size_t len);
typedef int (*PFN_Final)(unsigned char *hash, void *state);
struct hashdefinition {
const char *name;
int statesize;
int hashsize;
int (*init)(void *state);
int (*update)(void *state, const unsigned char *data, size_t len);
int (*final)(unsigned char *hash, void *state);
};
#define MD5_DIGEST_LENGTH 20
typedef struct {
uint32_t hash[4];
uint32_t block[16];
uint64_t count;
} MD5_CTX;
extern "C" {
extern void MD5_Init(MD5_CTX *);
extern void MD5_Update(MD5_CTX *, const u_char *, u_int);
extern void MD5_Final(u_char[MD5_DIGEST_LENGTH], MD5_CTX *);
}
#define SHA1_DIGEST_LENGTH 20
typedef struct {
uint32_t state[5];
uint32_t count[2];
uint8_t buffer[64];
} SHA1_CTX;
extern "C" {
extern void SHA1Init(SHA1_CTX *);
extern void SHA1Update(SHA1_CTX *, const u_char *, u_int);
extern void SHA1Final(u_char[SHA1_DIGEST_LENGTH], SHA1_CTX *);
};
struct hashdefinition hashdefs[]= {
{"MD5", sizeof(MD5_CTX), MD5_DIGEST_LENGTH, (PFN_Init)MD5_Init, (PFN_Update)MD5_Update, (PFN_Final)MD5_Final},
{"SHA1", sizeof(SHA1_CTX), SHA1_DIGEST_LENGTH, (PFN_Init)SHA1Init, (PFN_Update)SHA1Update, (PFN_Final)SHA1Final},
};
#define NRHASHTYPES (sizeof(hashdefs)/sizeof(*hashdefs))
class CryptHash {
private:
int m_type;
std::vector<uint8_t> m_state;
public:
enum {
MD5,
SHA1,
HASHTYPECOUNT
};
CryptHash() : m_type(-1) { }
bool Close() { return true; }
bool InitHash(int type)
{
if (type<0 || type>=(int)NRHASHTYPES)
return false;
m_type= type;
m_state.resize(hashdefs[m_type].statesize);
return 0!=hashdefs[m_type].init(&m_state[0]);
}
bool AddData(const std::vector<uint8_t> & data)
{
if (m_type<0) return false;
return 0!=hashdefs[m_type].update(&m_state[0], &data[0], data.size());
}
bool GetHash(std::vector<uint8_t> & hash)
{
if (m_type<0) return false;
hash.resize(hashdefs[m_type].hashsize);
return 0!=hashdefs[m_type].final(&hash[0], &m_state[0]);
}
bool CalcHash(const std::vector<uint8_t> & data, std::vector<uint8_t> & hash, int type)
{
if (type<0 || type>=(int)NRHASHTYPES) return false;
hashdefinition & H= hashdefs[type];
hash.resize(H.hashsize);
std::vector<uint8_t> state(H.statesize);
if (!H.init(&state[0]))
return false;
if (!H.update(&state[0], &data[0], data.size()))
return false;
if (!H.final(&hash[0], &state[0]))
return false;
return true;
}
int hashtype() const { return m_type; }
const char*hashname() const
{
if (m_type<0) return "unknown";
return hashdefs[m_type].name;
}
};