Skip to content

Commit 7cc351f

Browse files
committed
More crypto cleanup.
1 parent b4b5238 commit 7cc351f

File tree

4 files changed

+18
-45
lines changed

4 files changed

+18
-45
lines changed

packages/crypto/include/crypto/Crypto.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace l::crypto {
1616

1717
template<bool PUBLIC = true, bool X25519 = false, size_t SIZE = (PUBLIC ? 12 : 16)>
18-
const std::array<unsigned char, SIZE>& GetPemPrefix() {
18+
const std::array<unsigned char, SIZE>& Get25519PemPrefix() {
1919

2020
// secret x25519 pem key in hex
2121
// 302e020100300506032b65 6e 04220420
@@ -67,7 +67,7 @@ namespace l::crypto {
6767

6868
};
6969

70-
std::string ToPublicKeyFormat(std::string_view pkcsFormat);
70+
std::string ToPKCSFormat(std::string_view content, bool publicKey = true);
7171
std::string To25519PemKey(std::string_view rawkey, bool x25519 = true, bool publicKey = true, bool b16 = false);
7272

7373
class CryptoHMacSha256 : public CryptoSigner {
@@ -203,8 +203,6 @@ namespace l::crypto {
203203
std::string GetPriKeyHex();
204204
std::string GetPubKeyHex();
205205
std::string GetPubKeyPem();
206-
std::string GetPubKeyPem2();
207-
208206

209207
bool Verify(std::string_view signature, std::string_view message, std::string_view publicKey = "");
210208
protected:

packages/crypto/source/common/Crypto.cpp

Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
namespace l::crypto {
1313

1414
// PKCS#8 format
15-
std::string ToPublicKeyFormat(std::string_view pkcsFormat) {
15+
std::string ToPKCSFormat(std::string_view content, bool publicKey) {
1616
std::stringstream stream;
17-
stream << "-----BEGIN PUBLIC KEY-----\n";
18-
stream << pkcsFormat;
17+
stream << "-----BEGIN ";
18+
stream << (publicKey ? "PUBLIC" : "PRIVATE");
19+
stream << " KEY---- - \n";
20+
stream << content;
1921
stream << "\n";
20-
stream << "-----END PUBLIC KEY-----\n";
22+
stream << "-----END ";
23+
stream << (publicKey ? "PUBLIC" : "PRIVATE");
24+
stream << " KEY---- - \n";
2125
return stream.str();
2226
}
2327

@@ -26,25 +30,25 @@ namespace l::crypto {
2630
size_t prefixSize = 0;
2731
if (x25519) {
2832
if (publicKey) {
29-
auto& prefix = GetPemPrefix<true, true>();
33+
auto& prefix = Get25519PemPrefix<true, true>();
3034
memcpy(pem.data(), prefix.data(), prefix.size());
3135
prefixSize = prefix.size();
3236
}
3337
else {
34-
auto& prefix = GetPemPrefix<false, true>();
38+
auto& prefix = Get25519PemPrefix<false, true>();
3539
memcpy(pem.data(), prefix.data(), prefix.size());
3640
memcpy(pem.data() + prefix.size(), rawkey.data(), rawkey.size());
3741
prefixSize = prefix.size();
3842
}
3943
}
4044
else {
4145
if (publicKey) {
42-
auto& prefix = GetPemPrefix<true, false>();
46+
auto& prefix = Get25519PemPrefix<true, false>();
4347
memcpy(pem.data(), prefix.data(), prefix.size());
4448
prefixSize = prefix.size();
4549
}
4650
else {
47-
auto& prefix = GetPemPrefix<false, false>();
51+
auto& prefix = Get25519PemPrefix<false, false>();
4852
memcpy(pem.data(), prefix.data(), prefix.size());
4953
prefixSize = prefix.size();
5054
}
@@ -281,20 +285,9 @@ namespace l::crypto {
281285
}
282286

283287
std::string CryptoXED25519::GetPublicKeyPKCS8() {
284-
return ToPublicKeyFormat(To25519PemKey(std::string_view(reinterpret_cast<const char*>(mPublicKey), 32), true, true));
288+
return ToPKCSFormat(To25519PemKey(std::string_view(reinterpret_cast<const char*>(mPublicKey), 32), true, true));
285289
}
286290

287-
288-
289-
290-
291-
292-
293-
294-
295-
296-
297-
298291
bool CryptoED25519::Init() {
299292
memset(mSeed, 0, 32);
300293
if (ed25519_create_seed(&mSeed[0])) {
@@ -399,22 +392,8 @@ namespace l::crypto {
399392
}
400393

401394
std::string CryptoED25519::GetPubKeyPem() {
402-
memcpy(mPemKey, "id-Ed25519", 10);
403-
mPemKey[10] = 1;
404-
mPemKey[11] = 3;
405-
mPemKey[12] = 101;
406-
mPemKey[13] = 112;
407-
memcpy(mPemKey + 10 + 4, mPubKey, 32);
408-
auto pemKey = std::string_view(reinterpret_cast<const char*>(mPemKey), 46);
409-
return l::serialization::base64_encode(pemKey);
410-
}
411-
412-
std::string CryptoED25519::GetPubKeyPem2() {
413-
static const std::array<unsigned char, 12> mED25519Prefix = { 0x30,0x2a,0x30,0x05,0x06,0x03,0x2b,0x65,0x70,0x03,0x21,0x00 };
414-
memcpy(mPemKey, mED25519Prefix.data(), mED25519Prefix.size());
415-
memcpy(mPemKey + 12, mPubKey, 32);
416-
auto pemKey = std::string_view(reinterpret_cast<const char*>(mPemKey), 12 + 32);
417-
return l::serialization::base64_encode(pemKey);
395+
auto rawKey = std::string_view(reinterpret_cast<const char*>(mPemKey), 32);
396+
return To25519PemKey(rawKey, false, true);
418397
}
419398

420399
/*
@@ -426,8 +405,6 @@ namespace l::crypto {
426405
privateKeyEncoding: {
427406
type: 'pkcs8',
428407
format: 'pem'
429-
430-
431408
*/
432409
bool CryptoED25519::Verify(std::string_view signature, std::string_view message, std::string_view publicKey) {
433410
unsigned char* pubKey = reinterpret_cast<unsigned char*>(const_cast<char*>(publicKey.data()));

packages/crypto/tests/common/CryptoppTest.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
using namespace l;
1616

17-
1817
TEST(CryptoPP, hmacsha256) {
1918

2019
CryptoPP::HMAC<CryptoPP::SHA256> mHmac;

packages/crypto/tests/common/ed25519Test.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ TEST(Crypto, ed2519) {
6969
LOG(LogTest) << "private key base64: " << ed25519.GetPriKeyBase64();
7070
LOG(LogTest) << "public key hex: " << ed25519.GetPubKeyHex();
7171
LOG(LogTest) << "private key hex: " << ed25519.GetPriKeyHex();
72-
LOG(LogTest) << "public key pem: " << ed25519.GetPubKeyPem();
73-
LOG(LogTest) << "public key pem2: " << ed25519.GetPubKeyPem2();
72+
LOG(LogTest) << "public key pem2: " << ed25519.GetPubKeyPem();
7473

7574
auto signature = ed25519.GetSign(message);
7675

0 commit comments

Comments
 (0)