SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
RSA.cpp
Go to the documentation of this file.
2
3#include <cryptopp/hex.h>
4#include <cryptopp/osrng.h>
5#include <cryptopp/rsa.h>
6
7using namespace sourcepp;
8
9std::pair<std::string, std::string> crypto::computeSHA256KeyPair(uint16_t size) {
10 CryptoPP::AutoSeededRandomPool rng;
11
12 CryptoPP::RSAES_OAEP_SHA256_Decryptor privateKey{rng, size};
13 CryptoPP::RSAES_OAEP_SHA256_Encryptor publicKey{privateKey};
14
15 std::vector<CryptoPP::byte> privateKeyData;
16 CryptoPP::VectorSink privateKeyDataSink{privateKeyData};
17 privateKey.AccessMaterial().Save(privateKeyDataSink);
18 std::string privateKeyStr;
19 CryptoPP::StringSource privateKeyStringSource{privateKeyData.data(), privateKeyData.size(), true, new CryptoPP::HexEncoder{new CryptoPP::StringSink{privateKeyStr}}};
20
21 std::vector<CryptoPP::byte> publicKeyData;
22 CryptoPP::VectorSink publicKeyDataArraySink{publicKeyData};
23 publicKey.AccessMaterial().Save(publicKeyDataArraySink);
24 std::string publicKeyStr;
25 CryptoPP::StringSource publicKeyStringSource{publicKeyData.data(), publicKeyData.size(), true, new CryptoPP::HexEncoder{new CryptoPP::StringSink{publicKeyStr}}};
26
27 return std::make_pair(std::move(privateKeyStr), std::move(publicKeyStr));
28}
29
30bool crypto::verifySHA256PublicKey(std::span<const std::byte> buffer, std::span<const std::byte> publicKey, std::span<const std::byte> signature) {
31 const CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA256>::Verifier verifier{
32 CryptoPP::VectorSource(reinterpret_cast<const std::vector<CryptoPP::byte>&>(publicKey), true).Ref()
33 };
34 return verifier.VerifyMessage(reinterpret_cast<const CryptoPP::byte*>(buffer.data()), buffer.size(),
35 reinterpret_cast<const CryptoPP::byte*>(signature.data()), signature.size());
36}
37
38std::vector<std::byte> crypto::signDataWithSHA256PrivateKey(std::span<const std::byte> buffer, std::span<const std::byte> privateKey) {
39 CryptoPP::AutoSeededRandomPool rng;
40
41 const CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA256>::Signer signer{
42 CryptoPP::VectorSource(reinterpret_cast<const std::vector<CryptoPP::byte>&>(privateKey), true).Ref()
43 };
44
45 std::vector<std::byte> out;
46 CryptoPP::VectorSource signData{
47 reinterpret_cast<const std::vector<CryptoPP::byte> &>(buffer), true,
48 new CryptoPP::SignerFilter{rng, signer, new CryptoPP::VectorSink{reinterpret_cast<std::vector<CryptoPP::byte> &>(out)}}
49 };
50 return out;
51}
std::vector< std::byte > signDataWithSHA256PrivateKey(std::span< const std::byte > buffer, std::span< const std::byte > privateKey)
Definition: RSA.cpp:38
bool verifySHA256PublicKey(std::span< const std::byte > buffer, std::span< const std::byte > publicKey, std::span< const std::byte > signature)
Definition: RSA.cpp:30
std::pair< std::string, std::string > computeSHA256KeyPair(uint16_t size=2048)
Definition: RSA.cpp:9
Definition: LZMA.h:11