From af51f4198cbd97584e6d6938f980c84d81880d21 Mon Sep 17 00:00:00 2001 From: malloc Date: Fri, 16 Mar 2018 17:08:33 -0500 Subject: [PATCH] flat integer --- server/src/crypto/base64.cpp | 63 ++++++++++++++++++++++++++++++++++-- server/src/crypto/base64.hpp | 2 ++ server/src/crypto/cipher.cpp | 1 + server/src/crypto/cipher.hpp | 14 ++++++++ server/src/crypto/keyex.cpp | 2 ++ server/src/crypto/keyex.hpp | 16 +++++++++ server/src/main.cpp | 2 -- server/src/utils/bigint.cpp | 1 + server/src/utils/bigint.hpp | 32 ++++++++++++++++++ 9 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 server/src/crypto/cipher.cpp create mode 100644 server/src/crypto/cipher.hpp create mode 100644 server/src/crypto/keyex.cpp create mode 100644 server/src/crypto/keyex.hpp create mode 100644 server/src/utils/bigint.cpp create mode 100644 server/src/utils/bigint.hpp diff --git a/server/src/crypto/base64.cpp b/server/src/crypto/base64.cpp index 6cec9d5..868b454 100644 --- a/server/src/crypto/base64.cpp +++ b/server/src/crypto/base64.cpp @@ -1,7 +1,10 @@ #include "base64.hpp" +#define B64_IGNORE 0xFE +#define B64_PADDING 0xFF + std::string sosc::cgc::base64_encode(const std::string& data) { - return base64_decode(data.c_str()); + return base64_encode(data.c_str(), data.length()); } std::string sosc::cgc::base64_encode(const void* raw, size_t length) { @@ -32,6 +35,60 @@ std::string sosc::cgc::base64_encode(const void* raw, size_t length) { return encoded; } -std::string base64_decode(const std::string& data) { - +static uint8_t base64_decode_char(char c) { + if(c >= 'A' && c <= 'Z') + return c - 'A'; + else if(c >= 'a' && c <= 'z') + return c - 'a' + 26; + else if(c >= '0' && c <= '9') + return c - '0' + 52; + else if(c == '+') + return 62; + else if(c == '/') + return 63; + else if(c == '=') + return B64_PADDING; + else + return B64_IGNORE; +} + +static void base64_decode_append(std::string* decoded, int data) { + *decoded += (char)(data >> 16); + + if((data & (1 << 30)) == 0) + *decoded += (char)(data >> 8); + + if((data & (1 << 29)) == 0) + *decoded += (char)data; +} + +std::string sosc::cgc::base64_decode(const std::string& data) { + std::string decoded; + int index = 0, j = 0; + + for(std::string::size_type i = 0; i < data.length(); ++i) { + int sextet = base64_decode_char(data[i]); + + if(sextet == B64_IGNORE || sextet == B64_PADDING) { + if(sextet == B64_PADDING && i >= data.length() - 2 && j > 1) + index |= 1 << (31 - j + 1); + + continue; + } + + index |= sextet << (6 * (3 - j)); + + j = (j + 1) % 4; + if(j == 0) { + base64_decode_append(&decoded, index); + index = 0; + } + } + + if(j > 1) { + index |= (j == 2) ? 3 << 29 : 1 << 29; + base64_decode_append(&decoded, index); + } + + return decoded; } diff --git a/server/src/crypto/base64.hpp b/server/src/crypto/base64.hpp index 6c04310..10e402c 100644 --- a/server/src/crypto/base64.hpp +++ b/server/src/crypto/base64.hpp @@ -1,7 +1,9 @@ #ifndef SOSC_CRYPTO_BASE64 #define SOSC_CRYPTO_BASE64 +#include #include +#include namespace sosc { namespace cgc { diff --git a/server/src/crypto/cipher.cpp b/server/src/crypto/cipher.cpp new file mode 100644 index 0000000..c1bb66a --- /dev/null +++ b/server/src/crypto/cipher.cpp @@ -0,0 +1 @@ +#include "cipher.hpp" diff --git a/server/src/crypto/cipher.hpp b/server/src/crypto/cipher.hpp new file mode 100644 index 0000000..a05674f --- /dev/null +++ b/server/src/crypto/cipher.hpp @@ -0,0 +1,14 @@ +#ifndef SOSC_CRYPTO_CIPHER_H +#define SOSC_CRYPTO_CIPHER_H + +namespace sosc { +namespace cgc { +class Cipher { +public: + +private: + +}; +}} + +#endif diff --git a/server/src/crypto/keyex.cpp b/server/src/crypto/keyex.cpp new file mode 100644 index 0000000..8cc2d17 --- /dev/null +++ b/server/src/crypto/keyex.cpp @@ -0,0 +1,2 @@ +#include "keyex.hpp" + diff --git a/server/src/crypto/keyex.hpp b/server/src/crypto/keyex.hpp new file mode 100644 index 0000000..c76a8c7 --- /dev/null +++ b/server/src/crypto/keyex.hpp @@ -0,0 +1,16 @@ +#ifndef SOSC_CRYPTO_KEYEX_H +#define SOSC_CRYPTO_KEYEX_H + +namespace sosc { +namespace cgc { +class KeyExchange { +public: + const int key_size = 512; + const int key_size_bytes = key_size / 8; + const int generator = 2; +private: + +}; +}} + +#endif diff --git a/server/src/main.cpp b/server/src/main.cpp index 739cf26..c0e83c4 100644 --- a/server/src/main.cpp +++ b/server/src/main.cpp @@ -32,7 +32,5 @@ int main(int argc, char **argv) { //std::string a = sosc::cgc::sha1("test", true); - std::cout << sosc::cgc::base64_encode("this is a longer test"); - return 0; } diff --git a/server/src/utils/bigint.cpp b/server/src/utils/bigint.cpp new file mode 100644 index 0000000..60800e2 --- /dev/null +++ b/server/src/utils/bigint.cpp @@ -0,0 +1 @@ +#include "bigint.hpp" diff --git a/server/src/utils/bigint.hpp b/server/src/utils/bigint.hpp new file mode 100644 index 0000000..7a7b258 --- /dev/null +++ b/server/src/utils/bigint.hpp @@ -0,0 +1,32 @@ +#ifndef SOSC_UTIL_BIGINT_H +#define SOSC_UTIL_BIGINT_H + +#include +#include + +namespace sosc { +class BigInteger { +public: + +private: + std::vector value; +}; +} + +/* + FFFFFF + FFFF x + ======== + FE01 + FE0100 + FE010000 + + ========== + FEFFFF01 + FE0100 + FE010000 + FE01000000 + + ============ + FFFEFF0001 +*/ + +#endif