-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoutils.h
112 lines (103 loc) · 2.76 KB
/
cryptoutils.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
105
106
107
108
109
110
111
112
/*
Copyright (C) 2012 Massimo Maggi
This program 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 3 of the License, or
(at your option) any later version.
This program 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, see <http://www.gnu.org/licenses/>
*/
#pragma once
#include <string.h>
#include <string>
#include <bitset>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <list>
#include <vector>
#include <iomanip>
#include <openssl/evp.h>
#include <boost/exception/exception.hpp>
#include <boost/concept_check.hpp>
#include <bits/ios_base.h>
using namespace std;
unsigned long Endian_QWord_Conversion(unsigned long dword);
string base64(const unsigned char* input, int length);
ostream& operator<<( ostream& out, const bitset<256> L );
class CryptoUtils
{
public:
CryptoUtils();
};
class HashAlgorithm
{
public:
virtual void addData(const void* ptr, size_t size)=0;
virtual void asBinary(void* ptr, unsigned int* size)=0;
virtual string asHexString();
virtual void reset()=0;
protected:
};
class CryptoException: public boost::exception {};
#include <openssl/hmac.h>
bitset<256> HMAC256(const string& key,const string& data);
bitset<256> HMAC256(const bitset<256>& key,const string& data);
string HMAC256_ashex(const bitset<256>& key,const string& data);
class OpenSSLHash: public HashAlgorithm
{
public:
virtual void addData(const void* ptr, size_t size);
virtual void asBinary(void* ptr, unsigned int* size);
virtual void reset() {
EVP_MD_CTX_destroy(ctx);
opensslhash_init(hashname);
}
protected:
OpenSSLHash() {};
string hashname;
virtual void opensslhash_init(string hname);
~OpenSSLHash();
const EVP_MD* md;
EVP_MD_CTX* ctx;
};
class SHA256Hash: public OpenSSLHash
{
public:
SHA256Hash() {
opensslhash_init("sha256");
}
};
class SHA512Hash: public OpenSSLHash
{
public:
SHA512Hash() {
opensslhash_init("sha512");
}
};
class MD5Hash: public OpenSSLHash
{
public:
MD5Hash() {
opensslhash_init("sha256");
}
};
class SHA256TreeHash: public HashAlgorithm
{
public:
SHA256TreeHash();
virtual void addData(const void* ptr, size_t size);
virtual void asBinary(void* ptr, unsigned int* size);
~SHA256TreeHash();
virtual void reset();
protected:
list<bitset<256> > mbhashes;
unsigned char* remainder;
long remaindersize;
bitset<256> SHA256HashOnePart();
bitset<256> SHA256HashDouble(bitset<256> p1,bitset<256> p2);
};