This commit is contained in:
malloc 2018-03-15 17:02:10 -05:00
parent 7aaad6943c
commit 6475baa50e
5 changed files with 82 additions and 8 deletions

View file

@ -1 +1,37 @@
#include "base64.hpp"
std::string sosc::cgc::base64_encode(const std::string& data) {
return base64_decode(data.c_str());
}
std::string sosc::cgc::base64_encode(const void* raw, size_t length) {
uint8_t* data = (uint8_t*)raw;
const std::string char_table
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string encoded;
for(size_t i = 0; i < length; i += 3) {
if(i > 0 && ((i / 3) * 4) % 76 == 0)
encoded += "\r\n";
int index = 0, padding = 0;
for(int j = 0; j < 3; ++j) {
if(i + j < length)
index += data[i + j] << (8 * (2 - j));
else
++padding;
}
for(int j = 0; j < 4 - padding; ++j)
encoded += char_table[(index >> (6 * (3 - j))) & 0x3F];
for(int j = 0; j < padding; ++j)
encoded += '=';
}
return encoded;
}
std::string base64_decode(const std::string& data) {
}

View file

@ -1 +1,14 @@
#ifndef SOSC_CRYPTO_BASE64
#define SOSC_CRYPTO_BASE64
#include <string>
namespace sosc {
namespace cgc {
std::string base64_encode(const std::string& data);
std::string base64_encode(const void* raw, size_t length);
std::string base64_decode(const std::string& data);
}}
#endif

View file

@ -48,19 +48,19 @@ static uint32_t K(uint8_t t) {
return 0xCA62C1D6; return 0xCA62C1D6;
} }
std::string sosc::cgc::sha1(const std::string& data) { std::string sosc::cgc::sha1(const std::string& data, bool raw) {
int last_block = data.length() % 64, int last_block = data.length() % 64,
pad_size = SHA1_BLOCK_SIZE - last_block; pad_size = SHA1_BLOCK_SIZE - last_block;
if(last_block >= 55) if(last_block >= 55)
pad_size += SHA1_BLOCK_SIZE; pad_size += SHA1_BLOCK_SIZE;
std::string padding(0, pad_size); std::string padding(pad_size, 0);
padding[0] = 0x80; padding[0] = 0x80;
for(int i = 0; i < 8; ++i) { for(int i = 0; i < 8; ++i) {
padding[padding.length() - i - 1] = padding[padding.length() - i - 1] =
(padding.length() >> (8 * i)) & 0xFF; ((data.length() * 8ull) >> (8 * i)) & 0xFF;
} }
uint32_t H[5] = { uint32_t H[5] = {
@ -76,8 +76,10 @@ std::string sosc::cgc::sha1(const std::string& data) {
for(int j = 0; j < 16; ++j) { for(int j = 0; j < 16; ++j) {
W[j] = 0; W[j] = 0;
for(int k = 0; k < 4; ++k) { for(int k = 0; k < 4; ++k) {
W[j] |= (((i + j + k < data.length()) W[j] |= (((i + j * 4 + k < data.length())
? data : padding)[i + j + k] >> (8 * (3 - i))) & 0xFF; ? data[i + j * 4 + k]
: padding[(i + j * 4 + k) - data.length()]) & 0xFF)
<< (8 * (3 - k));
} }
} }
@ -100,5 +102,23 @@ std::string sosc::cgc::sha1(const std::string& data) {
H[j] += O[j]; H[j] += O[j];
} }
return ""; if(raw) {
std::string hash(20, 0);
for(int i = 0; i < 5; ++i) {
for(int j = 0; j < 4; ++j)
hash[i * 4 + j] = (H[i] >> (8 * (3 - j))) & 0xFF;
}
return hash;
} else {
std::stringstream stream;
for(int i = 0; i < 5; ++i) {
stream << std::setfill('0')
<< std::setw(8)
<< std::hex
<< H[i];
}
return stream.str();
}
} }

View file

@ -3,10 +3,12 @@
#include <cstdint> #include <cstdint>
#include <string> #include <string>
#include <sstream>
#include <iomanip>
namespace sosc { namespace sosc {
namespace cgc { namespace cgc {
std::string sha1(const std::string& data); std::string sha1(const std::string& data, bool raw = false);
}} }}
#endif #endif

View file

@ -6,6 +6,7 @@
#include "utils/time.hpp" #include "utils/time.hpp"
#include "sock/tcpsock.hpp" #include "sock/tcpsock.hpp"
#include "crypto/sha1.hpp" #include "crypto/sha1.hpp"
#include "crypto/base64.hpp"
int main(int argc, char **argv) { int main(int argc, char **argv) {
//auto sock = sosc::TcpClient(); //auto sock = sosc::TcpClient();
@ -29,7 +30,9 @@ int main(int argc, char **argv) {
std::cout << got << std::endl; std::cout << got << std::endl;
}*/ }*/
std::string a = sosc::cgc::sha1("test"); //std::string a = sosc::cgc::sha1("test", true);
std::cout << sosc::cgc::base64_encode("this is a longer test");
return 0; return 0;
} }