-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_functions.h
56 lines (44 loc) · 1.56 KB
/
common_functions.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
#ifndef BLSIG_COMMON_H_INCLUDED
# define BLSIG_COMMON_H_INCLUDED
# include "includes.h"
using namespace CryptoPP;
static const std::regex PEM_Key_Regex_Public(
"-----BEGIN (?:RSA )?PUBLIC KEY-----[\\r\\n]+([^-]*)[\\r\\n]+-----END (?:RSA )?PUBLIC KEY-----");
static const std::regex PEM_Key_Regex_Private(
"-----BEGIN (?:RSA )?PRIVATE KEY-----[\\r\\n]+([^-]*)[\\r\\n]+-----END (?:RSA )?PRIVATE KEY-----");
/* Generates the SHA512 hash of an arbitrary string.
*/
Integer GenerateHash(const std::string &message)
{
SHA512 hash;
SecByteBlock buff;
SecByteBlock orig((const byte*)message.c_str(), message.size());
buff.resize(SHA512::DIGESTSIZE);
hash.CalculateTruncatedDigest(buff, buff.size(), orig, orig.size());
Integer hashed_message(buff.data(), buff.size());
#if DEBUG
std::cout << "Message: " << message << std::endl;
std::cout << "Hash: " << std::hex << hashed_message << std::dec << std::endl;
#endif
return hashed_message;
}
/* Loads an RSA Public Key from the specified file.
*/
RSA::PublicKey ReadPEMPublicKey(std::string file_name)
{
RSA::PublicKey public_key;
FileSource public_key_file(file_name.c_str(), true);
PEM_Load(public_key_file, public_key);
return public_key;
}
/* Loads an RSA Private Key from the specified file.
* The key must not be password protected.
*/
RSA::PrivateKey ReadPEMPrivateKey(std::string file_name)
{
RSA::PrivateKey private_key;
FileSource private_key_file(file_name.c_str(), true);
PEM_Load(private_key_file, private_key);
return private_key;
}
#endif