3
3
#include < string>
4
4
5
5
#include " logging/LoggingAll.h"
6
+ #include " serialization/Base16.h"
6
7
#include " serialization/Base64.h"
7
8
8
9
#include " cryptopp/xed25519.h"
@@ -18,8 +19,11 @@ namespace l::crypto {
18
19
CryptoSigner () = default ;
19
20
virtual ~CryptoSigner () = default ;
20
21
22
+ virtual std::string_view GetAssociatedKey () = 0;
21
23
virtual void AccumulateMessage (std::string_view message) = 0;
22
24
virtual std::string SignMessageB64 () = 0;
25
+ virtual std::string SignMessageHex () = 0;
26
+
23
27
};
24
28
25
29
std::string ToPublicKeyFormat (std::string_view pkcsFormat);
@@ -30,14 +34,19 @@ namespace l::crypto {
30
34
CryptoHMacSha256 () = default ;
31
35
~CryptoHMacSha256 () = default ;
32
36
37
+ void SetAssociatedKey (std::string_view associatedKey) {
38
+ mAssociatedKey = associatedKey;
39
+ }
40
+
33
41
bool LoadSecretKeyAscii (std::string_view privateKeyAscii) {
34
- CryptoPP::SHA256 sha256;
35
42
auto keyBytes = reinterpret_cast <const CryptoPP::byte*>(privateKeyAscii.data ());
36
- sha256.CalculateDigest (mSecret , keyBytes, privateKeyAscii.size ());
37
- mHmac .SetKey (mSecret , 32 );
43
+ mHmac .SetKey (keyBytes, privateKeyAscii.size ());
38
44
return true ;
39
45
}
40
46
47
+ std::string_view GetAssociatedKey () override {
48
+ return mAssociatedKey ;
49
+ }
41
50
virtual void AccumulateMessage (std::string_view message) override {
42
51
auto p = reinterpret_cast <const CryptoPP::byte*>(message.data ());
43
52
mHmac .Update (p, message.size ());
@@ -48,6 +57,11 @@ namespace l::crypto {
48
57
return l::serialization::base64_encode (mSignature , 32 );
49
58
}
50
59
60
+ virtual std::string SignMessageHex () override {
61
+ SignMessage ();
62
+ return l::serialization::base16_encode (mSignature , 32 );
63
+ }
64
+
51
65
protected:
52
66
void SignMessage () {
53
67
mHmac .Final (mSignature );
@@ -56,6 +70,8 @@ namespace l::crypto {
56
70
CryptoPP::HMAC<CryptoPP::SHA256> mHmac ;
57
71
CryptoPP::byte mSecret [32 ];
58
72
CryptoPP::byte mSignature [32 ];
73
+
74
+ std::string mAssociatedKey ;
59
75
};
60
76
61
77
class CryptoXED25519 : public CryptoSigner {
@@ -65,17 +81,24 @@ namespace l::crypto {
65
81
66
82
void CreateNewKeys ();
67
83
84
+ void SetAssociatedKey (std::string_view associatedKey) {
85
+ mAssociatedKey = associatedKey;
86
+ }
87
+
68
88
bool LoadPrivateKeyB64 (std::string_view privateKeyB64);
69
89
bool LoadPrivateKeyHex (std::string_view privateKeyHex);
70
90
bool LoadPublicKeyB64 (std::string_view publicKeyB64);
71
91
bool LoadPublicKeyHex (std::string_view publicKeyHex);
72
92
73
93
CryptoPP::byte* SignMessage ();
74
- std::string SignMessageHex ();
75
94
std::string SignMessagePem ();
76
95
77
96
virtual void AccumulateMessage (std::string_view message) override ;
78
97
virtual std::string SignMessageB64 () override ;
98
+ virtual std::string SignMessageHex () override ;
99
+ std::string_view GetAssociatedKey () override {
100
+ return " " ;
101
+ }
79
102
80
103
bool VerifyB64 (std::string_view signatureB64);
81
104
bool VerifyHex (std::string_view signatureHex);
@@ -114,6 +137,8 @@ namespace l::crypto {
114
137
CryptoPP::byte mPrivateKey [32 ];
115
138
CryptoPP::byte mPublicKey [32 ];
116
139
CryptoPP::byte mSignature [64 ];
140
+
141
+ std::string mAssociatedKey ;
117
142
};
118
143
119
144
0 commit comments