-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcryptopp_ecdh_algorithm.cpp
More file actions
99 lines (82 loc) · 2.85 KB
/
Copy pathcryptopp_ecdh_algorithm.cpp
File metadata and controls
99 lines (82 loc) · 2.85 KB
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
#include "cryptopp_ecdh_algorithm.h"
ECDH_Algorithm::ECDH_Algorithm(): m_curve(CryptoPP::ASN1::secp256r1())
{
m_ecdh.AccessGroupParameters() = m_curve;
m_private_key.New(m_ecdh.PrivateKeyLength());
m_pubic_key.New(m_ecdh.PublicKeyLength());
m_shared.New(m_ecdh.AgreedValueLength());
}
ECDH_Algorithm::ECDH_Algorithm(CryptoPP::OID input_curve):m_curve(input_curve),m_ecdh(input_curve)
{
m_private_key.New(m_ecdh.PrivateKeyLength());
m_pubic_key.New(m_ecdh.PublicKeyLength());
m_shared.New(m_ecdh.AgreedValueLength());
}
void ECDH_Algorithm::SetCurve(CryptoPP::OID input_curve)
{
m_ecdh.AccessGroupParameters() = input_curve;
m_private_key.CleanNew(m_ecdh.PrivateKeyLength());
m_pubic_key.CleanNew(m_ecdh.PublicKeyLength());
m_shared.CleanNew(m_ecdh.AgreedValueLength());
}
void ECDH_Algorithm::GenerateKeyPair()
{
m_ecdh.GenerateKeyPair(m_rand_engine, m_private_key, m_pubic_key);
}
std::string ECDH_Algorithm::GetPublicKey()
{
string str_return(m_pubic_key.begin(),m_pubic_key.end());
return str_return;
}
std::string ECDH_Algorithm::GetPrivateKey()
{
string str_return(m_private_key.begin(),m_private_key.end());
return str_return;
}
std::string ECDH_Algorithm::GetSharedKey()
{
string str_return(m_shared.begin(),m_shared.end());
return str_return;
}
std::string ECDH_Algorithm::GetPublicKeyWithBase64()
{
return Cryptopp_Base64::encode(GetPublicKey());
}
std::string ECDH_Algorithm::GetPrivateKeyWithBase64()
{
return Cryptopp_Base64::encode(GetPrivateKey());
}
std::string ECDH_Algorithm::GetSharedKeyWithBase64()
{
return Cryptopp_Base64::encode(GetSharedKey());
}
void ECDH_Algorithm::SetPublicKey(const std::string &input_string)
{
m_pubic_key.CleanNew(input_string.size());
m_pubic_key.Assign((CryptoPP::byte *)input_string.data(),input_string.size());
}
void ECDH_Algorithm::SetPrivateKey(const std::string &input_string)
{
m_private_key.CleanNew(input_string.size());
m_private_key.Assign((CryptoPP::byte *)input_string.data(),input_string.size());
}
void ECDH_Algorithm::SetPublicKeyWithBase64(const std::string &input_string)
{
SetPublicKey(Cryptopp_Base64::decode(input_string));
}
void ECDH_Algorithm::SetPrivateKeyWithBase64(const std::string &input_string)
{
SetPrivateKey(Cryptopp_Base64::decode(input_string));
}
bool ECDH_Algorithm::AgreePublicKey(const std::string &input_string_public_key)
{
CryptoPP::SecByteBlock other_pubic_key;
other_pubic_key.CleanNew(input_string_public_key.size());
other_pubic_key.Assign((CryptoPP::byte *)input_string_public_key.data(),input_string_public_key.size());
bool bret = m_ecdh.Agree(m_shared,m_private_key,other_pubic_key);
return bret;
}
bool ECDH_Algorithm::AgreePublicKeyWithBase64(const std::string &input_string_public_key)
{
return AgreePublicKey(Cryptopp_Base64::decode(input_string_public_key));
}