larra/library/include/larra/encryption.hpp

107 lines
3.7 KiB
C++

#pragma once
#include <algorithm>
#include <string>
namespace larra::xmpp {
constexpr std::size_t kSha512ResultSize = 64;
constexpr std::size_t kSha256ResultSize = 32;
constexpr std::size_t kSha1ResultSize = 20;
struct UnsignedCharTrait {
using char_type = unsigned char;
static constexpr auto assign(char_type& c1, const char_type& c2) -> void {
c1 = c2;
}
static constexpr auto assign(char_type* c1, std::size_t n, const char_type c2) -> char_type* {
std::ranges::fill_n(c1, static_cast<std::iter_difference_t<unsigned char>>(n), c2);
return c1;
}
static constexpr auto copy(char_type* c1, const char_type* c2, std::size_t n) -> char_type* {
std::ranges::copy_n(c2, static_cast<std::iter_difference_t<unsigned char>>(n), c1);
return c1;
}
static constexpr auto move(char_type* c1, const char_type* c2, std::size_t n) -> char_type* {
return copy(c1, c2, n);
}
};
using UnsignedStringView = std::basic_string_view<unsigned char, UnsignedCharTrait>;
using UnsignedString = std::basic_string<unsigned char, UnsignedCharTrait>;
auto EncodeBase64(std::string_view) -> std::string;
auto DecodeBase64(std::string_view) -> std::string;
auto GenerateNonce(std::size_t length = 24) -> std::string; // NOLINT
namespace sha512sum {
struct EncryptionTag {
friend auto ToString(const EncryptionTag&) -> std::string;
};
auto Pbdkf2(std::string_view password, UnsignedStringView salt, int iterations, EncryptionTag = {}) -> UnsignedString;
auto Hash(UnsignedStringView data, EncryptionTag = {}) -> UnsignedString;
auto Hmac(std::string_view key, UnsignedStringView message, EncryptionTag = {}) -> UnsignedString;
auto GenerateScramAuthMessage(std::string_view password,
std::string salt,
std::string_view serverNonce,
std::string_view firstServerMessage,
std::string_view initialMessage,
int iterations,
EncryptionTag = {}) -> std::string;
} // namespace sha512sum
namespace sha256sum {
struct EncryptionTag {
friend auto ToString(const EncryptionTag&) -> std::string;
};
auto Pbdkf2(std::string_view password, UnsignedStringView salt, int iterations, EncryptionTag = {}) -> UnsignedString;
auto Hash(UnsignedStringView data, EncryptionTag = {}) -> UnsignedString;
auto Hmac(std::string_view key, UnsignedStringView message, EncryptionTag = {}) -> UnsignedString;
auto GenerateScramAuthMessage(std::string_view password,
std::string salt,
std::string_view serverNonce,
std::string_view firstServerMessage,
std::string_view initialMessage,
int iterations,
EncryptionTag = {}) -> std::string;
} // namespace sha256sum
namespace sha1sum {
struct EncryptionTag {
friend auto ToString(const EncryptionTag&) -> std::string;
};
auto Pbdkf2(std::string_view password, UnsignedStringView salt, int iterations, EncryptionTag = {}) -> UnsignedString;
auto Hash(UnsignedStringView data, EncryptionTag = {}) -> UnsignedString;
auto Hmac(std::string_view key, UnsignedStringView message, EncryptionTag = {}) -> UnsignedString;
auto GenerateScramAuthMessage(std::string_view password,
std::string salt,
std::string_view serverNonce,
std::string_view firstServerMessage,
std::string_view initialMessage,
int iterations,
EncryptionTag = {}) -> std::string;
} // namespace sha1sum
} // namespace larra::xmpp