flat integer

This commit is contained in:
malloc 2018-03-16 17:08:33 -05:00
parent 6475baa50e
commit af51f4198c
9 changed files with 128 additions and 5 deletions

View file

@ -1,7 +1,10 @@
#include "base64.hpp" #include "base64.hpp"
#define B64_IGNORE 0xFE
#define B64_PADDING 0xFF
std::string sosc::cgc::base64_encode(const std::string& data) { 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) { 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; 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;
} }

View file

@ -1,7 +1,9 @@
#ifndef SOSC_CRYPTO_BASE64 #ifndef SOSC_CRYPTO_BASE64
#define SOSC_CRYPTO_BASE64 #define SOSC_CRYPTO_BASE64
#include <vector>
#include <string> #include <string>
#include <iostream>
namespace sosc { namespace sosc {
namespace cgc { namespace cgc {

View file

@ -0,0 +1 @@
#include "cipher.hpp"

View file

@ -0,0 +1,14 @@
#ifndef SOSC_CRYPTO_CIPHER_H
#define SOSC_CRYPTO_CIPHER_H
namespace sosc {
namespace cgc {
class Cipher {
public:
private:
};
}}
#endif

View file

@ -0,0 +1,2 @@
#include "keyex.hpp"

View file

@ -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

View file

@ -32,7 +32,5 @@ int main(int argc, char **argv) {
//std::string a = sosc::cgc::sha1("test", true); //std::string a = sosc::cgc::sha1("test", true);
std::cout << sosc::cgc::base64_encode("this is a longer test");
return 0; return 0;
} }

View file

@ -0,0 +1 @@
#include "bigint.hpp"

View file

@ -0,0 +1,32 @@
#ifndef SOSC_UTIL_BIGINT_H
#define SOSC_UTIL_BIGINT_H
#include <vector>
#include <cstdint>
namespace sosc {
class BigInteger {
public:
private:
std::vector<uint8_t> value;
};
}
/*
FFFFFF
FFFF x
========
FE01
FE0100
FE010000 +
==========
FEFFFF01
FE0100
FE010000
FE01000000 +
============
FFFEFF0001
*/
#endif