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, const std::vector<std::byte>& publicKey, std::span<const std::byte> signature) {
31 CryptoPP::VectorSource publicKeySource{reinterpret_cast<const std::vector<CryptoPP::byte>&>(publicKey), true};
32 const CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA256>::Verifier verifier{publicKeySource};
33 return verifier.VerifyMessage(reinterpret_cast<const CryptoPP::byte*>(buffer.data()), buffer.size(), reinterpret_cast<const CryptoPP::byte*>(signature.data()), signature.size());
34}
35
36std::vector<std::byte> crypto::signDataWithSHA256PrivateKey(const std::vector<std::byte>& buffer, const std::vector<std::byte>& privateKey) {
37 CryptoPP::AutoSeededRandomPool rng;
38
39 CryptoPP::VectorSource privateKeySource{reinterpret_cast<const std::vector<CryptoPP::byte>&>(privateKey), true};
40 const CryptoPP::RSASS<CryptoPP::PKCS1v15, CryptoPP::SHA256>::Signer signer{privateKeySource};
41
42 std::vector<std::byte> out;
43 CryptoPP::VectorSource signData{reinterpret_cast<const std::vector<CryptoPP::byte>&>(buffer), true, new CryptoPP::SignerFilter{rng, signer, new CryptoPP::VectorSink{reinterpret_cast<std::vector<CryptoPP::byte>&>(out)}}};
44 return out;
45}
bool verifySHA256PublicKey(std::span< const std::byte > buffer, const std::vector< std::byte > &publicKey, std::span< const std::byte > signature)
Definition: RSA.cpp:30
std::vector< std::byte > signDataWithSHA256PrivateKey(const std::vector< std::byte > &buffer, const std::vector< std::byte > &privateKey)
Definition: RSA.cpp:36
std::pair< std::string, std::string > computeSHA256KeyPair(uint16_t size=2048)
Definition: RSA.cpp:9
Definition: LZMA.h:11