124 lines
3.0 KiB
C++
124 lines
3.0 KiB
C++
#include <ts6bot/Crypto.hpp>
|
|
|
|
#include <openssl/rand.h>
|
|
#include <openssl/sha.h>
|
|
|
|
#include <stdexcept>
|
|
|
|
namespace ts6bot::crypto
|
|
{
|
|
|
|
static void rand_bytes(std::span<std::uint8_t> out)
|
|
{
|
|
if (out.empty()) {
|
|
return;
|
|
}
|
|
if (RAND_bytes(out.data(), static_cast<int>(out.size())) != 1) {
|
|
throw std::runtime_error("RAND_bytes failed");
|
|
}
|
|
}
|
|
|
|
std::string GenerateCodeVerifier(std::size_t length)
|
|
{
|
|
if (length < 43 || length > 128) {
|
|
throw std::runtime_error("code_verifier length must be 43..128");
|
|
}
|
|
|
|
static constexpr char kAlphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
"abcdefghijklmnopqrstuvwxyz"
|
|
"0123456789"
|
|
"-._~";
|
|
|
|
std::vector<std::uint8_t> rnd(length);
|
|
rand_bytes(rnd);
|
|
|
|
std::string out;
|
|
out.reserve(length);
|
|
for (std::uint8_t b : rnd) {
|
|
out.push_back(kAlphabet[b % (sizeof(kAlphabet) - 1)]);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string Base64UrlEncode(std::span<const std::uint8_t> data)
|
|
{
|
|
static constexpr char tbl[] =
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
std::string out;
|
|
out.reserve(((data.size() + 2) / 3) * 4);
|
|
|
|
std::size_t i = 0;
|
|
while (i + 3 <= data.size()) {
|
|
std::uint32_t n = (static_cast<std::uint32_t>(data[i]) << 16) |
|
|
(static_cast<std::uint32_t>(data[i + 1]) << 8) |
|
|
(static_cast<std::uint32_t>(data[i + 2]));
|
|
out.push_back(tbl[(n >> 18) & 63]);
|
|
out.push_back(tbl[(n >> 12) & 63]);
|
|
out.push_back(tbl[(n >> 6) & 63]);
|
|
out.push_back(tbl[n & 63]);
|
|
i += 3;
|
|
}
|
|
|
|
std::size_t rem = data.size() - i;
|
|
if (rem == 1) {
|
|
std::uint32_t n = (static_cast<std::uint32_t>(data[i]) << 16);
|
|
out.push_back(tbl[(n >> 18) & 63]);
|
|
out.push_back(tbl[(n >> 12) & 63]);
|
|
out.push_back('=');
|
|
out.push_back('=');
|
|
} else if (rem == 2) {
|
|
std::uint32_t n = (static_cast<std::uint32_t>(data[i]) << 16) |
|
|
(static_cast<std::uint32_t>(data[i + 1]) << 8);
|
|
out.push_back(tbl[(n >> 18) & 63]);
|
|
out.push_back(tbl[(n >> 12) & 63]);
|
|
out.push_back(tbl[(n >> 6) & 63]);
|
|
out.push_back('=');
|
|
}
|
|
|
|
// base64url + no padding
|
|
for (char& c : out) {
|
|
if (c == '+') {
|
|
c = '-';
|
|
} else if (c == '/') {
|
|
c = '_';
|
|
}
|
|
}
|
|
while (!out.empty() && out.back() == '=') {
|
|
out.pop_back();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::string GenerateState(std::size_t bytes)
|
|
{
|
|
std::vector<std::uint8_t> rnd(bytes);
|
|
rand_bytes(rnd);
|
|
return Base64UrlEncode(rnd);
|
|
}
|
|
|
|
std::vector<std::uint8_t> Sha256(std::span<const std::uint8_t> data)
|
|
{
|
|
std::vector<std::uint8_t> out(SHA256_DIGEST_LENGTH);
|
|
SHA256_CTX ctx;
|
|
if (SHA256_Init(&ctx) != 1) {
|
|
throw std::runtime_error("SHA256_Init failed");
|
|
}
|
|
if (!data.empty() &&
|
|
SHA256_Update(&ctx, data.data(), data.size()) != 1) {
|
|
throw std::runtime_error("SHA256_Update failed");
|
|
}
|
|
if (SHA256_Final(out.data(), &ctx) != 1) {
|
|
throw std::runtime_error("SHA256_Final failed");
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::vector<std::uint8_t> Sha256(std::span<const char> data)
|
|
{
|
|
auto* ptr = reinterpret_cast<const std::uint8_t*>(data.data());
|
|
return Sha256(std::span{ptr, data.size()});
|
|
}
|
|
|
|
} // namespace ts6bot::crypto
|