diff options
author | Treeston <treeston.mmoc@gmail.com> | 2020-07-26 01:53:34 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-26 01:53:34 +0200 |
commit | 210176fd915cf4ba16f428d3c1a249a71f4aa7a7 (patch) | |
tree | 6998a19da1330be8679fe3e760f858915494400b /src/common/Cryptography/BigNumber.h | |
parent | cdaf890af4b5bb7ce256752b49bba2c0f3ed9264 (diff) |
Core/Authserver: Authserver cleanup (PR#25093)
- Fix a handful of 1/256 bugs with most significant byte zero in BigNumber
- Get rid of (most of) the C-style arrays in authserver
- CryptoRandom as a unified source for cryptographic randomness
- Bring our other crypto APIs into 2020
- BigNumber usability improvements
- Authserver is now actually readable as a result of all of the above
Diffstat (limited to 'src/common/Cryptography/BigNumber.h')
-rw-r--r-- | src/common/Cryptography/BigNumber.h | 22 |
1 files changed, 16 insertions, 6 deletions
diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h index 9efe66714c5..ef9281b4652 100644 --- a/src/common/Cryptography/BigNumber.h +++ b/src/common/Cryptography/BigNumber.h @@ -22,6 +22,7 @@ #include <array> #include <memory> #include <string> +#include <vector> struct bignum_st; @@ -30,13 +31,20 @@ class TC_COMMON_API BigNumber public: BigNumber(); BigNumber(BigNumber const& bn); - BigNumber(uint32); + BigNumber(uint32 v) : BigNumber() { SetDword(v); } + BigNumber(std::string const& v) : BigNumber() { SetHexStr(v); } + template <size_t Size> + BigNumber(std::array<uint8, Size> const& v) : BigNumber() { SetBinary(v.data(), Size); } + ~BigNumber(); void SetDword(uint32); void SetQword(uint64); void SetBinary(uint8 const* bytes, int32 len); + template <typename Container> + void SetBinary(Container const& c) { SetBinary(std::data(c), std::size(c)); } bool SetHexStr(char const* str); + bool SetHexStr(std::string const& str) { return SetHexStr(str.c_str()); } void SetRand(int32 numbits); @@ -103,12 +111,14 @@ class TC_COMMON_API BigNumber uint32 AsDword() const; - bool AsByteArray(uint8* buf, std::size_t bufsize, bool littleEndian = true) const; - std::unique_ptr<uint8[]> AsByteArray(int32 minSize = 0, bool littleEndian = true) const; - template <std::size_t N> std::array<uint8, N> AsByteArray(bool littleEndian = true) const + void GetBytes(uint8* buf, size_t bufsize, bool littleEndian = true) const; + std::vector<uint8> ToByteVector(int32 minSize = 0, bool littleEndian = true) const; + + template <std::size_t Size> + std::array<uint8, Size> ToByteArray(bool littleEndian = true) const { - std::array<uint8, N> buf; - AsByteArray(buf.data(), N, littleEndian); + std::array<uint8, Size> buf; + GetBytes(buf.data(), Size, littleEndian); return buf; } |