Added version file. Moved things into correct namespace.
Added version file for getting library info. Some stuff, like Base64/Sha1 wasn't in the fr namespace. Has been moved into it.
This commit is contained in:
@@ -40,7 +40,7 @@ add_definitions(-Dntodh)
|
||||
set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" )
|
||||
set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" )
|
||||
|
||||
set(SOURCE_FILES ${SOURCE_FILES} main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h src/URL.cpp include/frnetlib/URL.h include/frnetlib/Sendable.h)
|
||||
set(SOURCE_FILES ${SOURCE_FILES} main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h include/frnetlib/Listener.h src/URL.cpp include/frnetlib/URL.h include/frnetlib/Sendable.h include/frnetlib/version.h)
|
||||
|
||||
include_directories(include)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
|
||||
|
||||
@@ -6,20 +6,23 @@
|
||||
#define FRNETLIB_BASE64_H
|
||||
#include <string>
|
||||
|
||||
class Base64
|
||||
namespace fr
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* Encodes a string into Base64
|
||||
*
|
||||
* @param input The string to encode
|
||||
* @return The resulting encoded string
|
||||
*/
|
||||
static std::string encode(const std::string &input);
|
||||
class Base64
|
||||
{
|
||||
public:
|
||||
/*!
|
||||
* Encodes a string into Base64
|
||||
*
|
||||
* @param input The string to encode
|
||||
* @return The resulting encoded string
|
||||
*/
|
||||
static std::string encode(const std::string &input);
|
||||
|
||||
//There's no decode function at the moment. Maybe I'll write one eventually. Sorry.
|
||||
private:
|
||||
//There's no decode function at the moment. Maybe I'll write one eventually. Sorry.
|
||||
private:
|
||||
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
#endif //FRNETLIB_BASE64_H
|
||||
|
||||
@@ -8,36 +8,39 @@
|
||||
#include <string>
|
||||
#include "frnetlib/NetworkEncoding.h"
|
||||
|
||||
class Sha1
|
||||
namespace fr
|
||||
{
|
||||
public:
|
||||
Sha1();
|
||||
|
||||
/*!
|
||||
* Sha1 hashes a string input and returns the raw digest
|
||||
*
|
||||
* @param input The string to hash
|
||||
* @return The Sha1 digest converted to host endianness
|
||||
*/
|
||||
static std::string sha1_digest(const std::string &input)
|
||||
class Sha1
|
||||
{
|
||||
Sha1 ctx;
|
||||
ctx.update(input);
|
||||
ctx.final();
|
||||
for(unsigned int &a : ctx.digest)
|
||||
a = ntohl(a);
|
||||
public:
|
||||
Sha1();
|
||||
|
||||
return std::string((char*)&ctx.digest[0], sizeof(ctx.digest));
|
||||
}
|
||||
/*!
|
||||
* Sha1 hashes a string input and returns the raw digest
|
||||
*
|
||||
* @param input The string to hash
|
||||
* @return The Sha1 digest converted to host endianness
|
||||
*/
|
||||
static std::string sha1_digest(const std::string &input)
|
||||
{
|
||||
Sha1 ctx;
|
||||
ctx.update(input);
|
||||
ctx.final();
|
||||
for(unsigned int &a : ctx.digest)
|
||||
a = ntohl(a);
|
||||
|
||||
private:
|
||||
void update(const std::string &s);
|
||||
void update(std::istream &is);
|
||||
void final();
|
||||
return std::string((char*)&ctx.digest[0], sizeof(ctx.digest));
|
||||
}
|
||||
|
||||
uint32_t digest[5];
|
||||
std::string buffer;
|
||||
uint64_t transforms;
|
||||
};
|
||||
private:
|
||||
void update(const std::string &s);
|
||||
void update(std::istream &is);
|
||||
void final();
|
||||
|
||||
uint32_t digest[5];
|
||||
std::string buffer;
|
||||
uint64_t transforms;
|
||||
};
|
||||
}
|
||||
|
||||
#endif //FRNETLIB_SHA1_H
|
||||
|
||||
18
include/frnetlib/version.h
Normal file
18
include/frnetlib/version.h
Normal file
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// Created by fred.nicolson on 12/07/18.
|
||||
//
|
||||
|
||||
#ifndef FRNETLIB_VERSION_H
|
||||
#define FRNETLIB_VERSION_H
|
||||
|
||||
//Format: Major | Minor | Patch
|
||||
|
||||
#define FRNETLIB_VERSION_MAJOR 1
|
||||
#define FRNETLIB_VERSION_MINOR 0
|
||||
#define FRNETLIB_VERSION_PATCH 0
|
||||
|
||||
#define FRNETLIB_VERSION_NUMBER 0x01000000
|
||||
#define FRNETLIB_VERSION_STRING "1.0.0"
|
||||
#define FRNETLIB_VERSION__STRING_FULL "frnetlib 1.0.0"
|
||||
|
||||
#endif //FRNETLIB_VERSION_H
|
||||
@@ -4,39 +4,43 @@
|
||||
|
||||
#include "frnetlib/Base64.h"
|
||||
|
||||
std::string Base64::encode(const std::string &input)
|
||||
namespace fr
|
||||
{
|
||||
static const std::string base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
std::string out;
|
||||
out.reserve(input.size() + (input.size() / 3) + 1);
|
||||
int a;
|
||||
|
||||
//Do as many sets of 3 bytes as we can
|
||||
for(a = 0; a < input.size() - 2; a += 3)
|
||||
std::string Base64::encode(const std::string &input)
|
||||
{
|
||||
out += base64_table[(input[a] >> 2) & 0x3F]; //Store the first 6 bits of the first byte
|
||||
out += base64_table[((input[a] & 0x3) << 4) | (input[a + 1] & 0xF0) >> 4]; //Store the last 2 bits of the first byte combined with the first 4 bits of the second byte
|
||||
out += base64_table[((input[a + 1] & 0xF) << 2) | (input[a + 2] & 0xC0) >> 6]; //Store the last 4 bits of the second byte combined with the first 2 bits of the third byte
|
||||
out += base64_table[input[a + 2] & 0x3F]; //Store the last 6 bits of the third byte
|
||||
static const std::string base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
std::string out;
|
||||
out.reserve(input.size() + (input.size() / 3) + 1);
|
||||
int a;
|
||||
|
||||
//Do as many sets of 3 bytes as we can
|
||||
for(a = 0; a < input.size() - 2; a += 3)
|
||||
{
|
||||
out += base64_table[(input[a] >> 2) & 0x3F]; //Store the first 6 bits of the first byte
|
||||
out += base64_table[((input[a] & 0x3) << 4) | (input[a + 1] & 0xF0) >> 4]; //Store the last 2 bits of the first byte combined with the first 4 bits of the second byte
|
||||
out += base64_table[((input[a + 1] & 0xF) << 2) | (input[a + 2] & 0xC0) >> 6]; //Store the last 4 bits of the second byte combined with the first 2 bits of the third byte
|
||||
out += base64_table[input[a + 2] & 0x3F]; //Store the last 6 bits of the third byte
|
||||
}
|
||||
|
||||
//Check if there's a remainder
|
||||
if(a < input.size())
|
||||
{
|
||||
out += base64_table[(input[a] >> 2) & 0x3F]; //Store first 6 bits of the first byte
|
||||
if(a == input.size() - 2) //There's 2 bytes left
|
||||
{
|
||||
out += base64_table[((input[a] & 0x3) << 4) | (input[a + 1] & 0xF0) >> 4]; //Store last 2 bits of first byte, and first 4 bits of second byte
|
||||
out += base64_table[(input[a + 1] & 0xF) << 2]; //Finally store the last 4 bits of the second byte
|
||||
out += "=";
|
||||
}
|
||||
else //There's 1 byte left
|
||||
{
|
||||
out += base64_table[(input[a] & 0x3) << 4]; //Store last 2 bits of first byte
|
||||
out += "==";
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
//Check if there's a remainder
|
||||
if(a < input.size())
|
||||
{
|
||||
out += base64_table[(input[a] >> 2) & 0x3F]; //Store first 6 bits of the first byte
|
||||
if(a == input.size() - 2) //There's 2 bytes left
|
||||
{
|
||||
out += base64_table[((input[a] & 0x3) << 4) | (input[a + 1] & 0xF0) >> 4]; //Store last 2 bits of first byte, and first 4 bits of second byte
|
||||
out += base64_table[(input[a + 1] & 0xF) << 2]; //Finally store the last 4 bits of the second byte
|
||||
out += "=";
|
||||
}
|
||||
else //There's 1 byte left
|
||||
{
|
||||
out += base64_table[(input[a] & 0x3) << 4]; //Store last 2 bits of first byte
|
||||
out += "==";
|
||||
}
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
445
src/Sha1.cpp
445
src/Sha1.cpp
@@ -25,263 +25,276 @@
|
||||
#include <fstream>
|
||||
|
||||
|
||||
static const size_t BLOCK_INTS = 16; /* number of 32bit integers per Sha1 block */
|
||||
static const size_t BLOCK_BYTES = BLOCK_INTS * 4;
|
||||
|
||||
|
||||
static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
|
||||
namespace fr
|
||||
{
|
||||
/* Sha1 initialization constants */
|
||||
digest[0] = 0x67452301;
|
||||
digest[1] = 0xefcdab89;
|
||||
digest[2] = 0x98badcfe;
|
||||
digest[3] = 0x10325476;
|
||||
digest[4] = 0xc3d2e1f0;
|
||||
|
||||
/* Reset counters */
|
||||
buffer = "";
|
||||
transforms = 0;
|
||||
}
|
||||
static const size_t BLOCK_INTS = 16; /* number of 32bit integers per Sha1 block */
|
||||
static const size_t BLOCK_BYTES = BLOCK_INTS * 4;
|
||||
|
||||
|
||||
static uint32_t rol(const uint32_t value, const size_t bits)
|
||||
{
|
||||
return (value << bits) | (value >> (32 - bits));
|
||||
}
|
||||
static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms)
|
||||
{
|
||||
/* Sha1 initialization constants */
|
||||
digest[0] = 0x67452301;
|
||||
digest[1] = 0xefcdab89;
|
||||
digest[2] = 0x98badcfe;
|
||||
digest[3] = 0x10325476;
|
||||
digest[4] = 0xc3d2e1f0;
|
||||
|
||||
/* Reset counters */
|
||||
buffer = "";
|
||||
transforms = 0;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i)
|
||||
{
|
||||
return rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1);
|
||||
}
|
||||
static uint32_t rol(const uint32_t value, const size_t bits)
|
||||
{
|
||||
return (value << bits) | (value >> (32 - bits));
|
||||
}
|
||||
|
||||
|
||||
static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i)
|
||||
{
|
||||
return rol(block[(i + 13) & 15] ^ block[(i + 8) & 15] ^ block[(i + 2) & 15] ^ block[i], 1);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (R0+R1), R2, R3, R4 are the different operations used in Sha1
|
||||
*/
|
||||
|
||||
static void R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
static void
|
||||
R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z,
|
||||
const size_t i)
|
||||
{
|
||||
z += ((w & (x ^ y)) ^ y) + block[i] + 0x5a827999 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
static void
|
||||
R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z,
|
||||
const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += ((w & (x ^ y)) ^ y) + block[i] + 0x5a827999 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
static void
|
||||
R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z,
|
||||
const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (w ^ x ^ y) + block[i] + 0x6ed9eba1 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
static void
|
||||
R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z,
|
||||
const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (((w | x) & y) | (w & x)) + block[i] + 0x8f1bbcdc + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
static void R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
static void
|
||||
R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z,
|
||||
const size_t i)
|
||||
{
|
||||
block[i] = blk(block, i);
|
||||
z += (w ^ x ^ y) + block[i] + 0xca62c1d6 + rol(v, 5);
|
||||
w = rol(w, 30);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Hash a single 512-bit block. This is the core of the algorithm.
|
||||
*/
|
||||
|
||||
static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
|
||||
{
|
||||
/* Copy digest[] to working vars */
|
||||
uint32_t a = digest[0];
|
||||
uint32_t b = digest[1];
|
||||
uint32_t c = digest[2];
|
||||
uint32_t d = digest[3];
|
||||
uint32_t e = digest[4];
|
||||
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(block, a, b, c, d, e, 0);
|
||||
R0(block, e, a, b, c, d, 1);
|
||||
R0(block, d, e, a, b, c, 2);
|
||||
R0(block, c, d, e, a, b, 3);
|
||||
R0(block, b, c, d, e, a, 4);
|
||||
R0(block, a, b, c, d, e, 5);
|
||||
R0(block, e, a, b, c, d, 6);
|
||||
R0(block, d, e, a, b, c, 7);
|
||||
R0(block, c, d, e, a, b, 8);
|
||||
R0(block, b, c, d, e, a, 9);
|
||||
R0(block, a, b, c, d, e, 10);
|
||||
R0(block, e, a, b, c, d, 11);
|
||||
R0(block, d, e, a, b, c, 12);
|
||||
R0(block, c, d, e, a, b, 13);
|
||||
R0(block, b, c, d, e, a, 14);
|
||||
R0(block, a, b, c, d, e, 15);
|
||||
R1(block, e, a, b, c, d, 0);
|
||||
R1(block, d, e, a, b, c, 1);
|
||||
R1(block, c, d, e, a, b, 2);
|
||||
R1(block, b, c, d, e, a, 3);
|
||||
R2(block, a, b, c, d, e, 4);
|
||||
R2(block, e, a, b, c, d, 5);
|
||||
R2(block, d, e, a, b, c, 6);
|
||||
R2(block, c, d, e, a, b, 7);
|
||||
R2(block, b, c, d, e, a, 8);
|
||||
R2(block, a, b, c, d, e, 9);
|
||||
R2(block, e, a, b, c, d, 10);
|
||||
R2(block, d, e, a, b, c, 11);
|
||||
R2(block, c, d, e, a, b, 12);
|
||||
R2(block, b, c, d, e, a, 13);
|
||||
R2(block, a, b, c, d, e, 14);
|
||||
R2(block, e, a, b, c, d, 15);
|
||||
R2(block, d, e, a, b, c, 0);
|
||||
R2(block, c, d, e, a, b, 1);
|
||||
R2(block, b, c, d, e, a, 2);
|
||||
R2(block, a, b, c, d, e, 3);
|
||||
R2(block, e, a, b, c, d, 4);
|
||||
R2(block, d, e, a, b, c, 5);
|
||||
R2(block, c, d, e, a, b, 6);
|
||||
R2(block, b, c, d, e, a, 7);
|
||||
R3(block, a, b, c, d, e, 8);
|
||||
R3(block, e, a, b, c, d, 9);
|
||||
R3(block, d, e, a, b, c, 10);
|
||||
R3(block, c, d, e, a, b, 11);
|
||||
R3(block, b, c, d, e, a, 12);
|
||||
R3(block, a, b, c, d, e, 13);
|
||||
R3(block, e, a, b, c, d, 14);
|
||||
R3(block, d, e, a, b, c, 15);
|
||||
R3(block, c, d, e, a, b, 0);
|
||||
R3(block, b, c, d, e, a, 1);
|
||||
R3(block, a, b, c, d, e, 2);
|
||||
R3(block, e, a, b, c, d, 3);
|
||||
R3(block, d, e, a, b, c, 4);
|
||||
R3(block, c, d, e, a, b, 5);
|
||||
R3(block, b, c, d, e, a, 6);
|
||||
R3(block, a, b, c, d, e, 7);
|
||||
R3(block, e, a, b, c, d, 8);
|
||||
R3(block, d, e, a, b, c, 9);
|
||||
R3(block, c, d, e, a, b, 10);
|
||||
R3(block, b, c, d, e, a, 11);
|
||||
R4(block, a, b, c, d, e, 12);
|
||||
R4(block, e, a, b, c, d, 13);
|
||||
R4(block, d, e, a, b, c, 14);
|
||||
R4(block, c, d, e, a, b, 15);
|
||||
R4(block, b, c, d, e, a, 0);
|
||||
R4(block, a, b, c, d, e, 1);
|
||||
R4(block, e, a, b, c, d, 2);
|
||||
R4(block, d, e, a, b, c, 3);
|
||||
R4(block, c, d, e, a, b, 4);
|
||||
R4(block, b, c, d, e, a, 5);
|
||||
R4(block, a, b, c, d, e, 6);
|
||||
R4(block, e, a, b, c, d, 7);
|
||||
R4(block, d, e, a, b, c, 8);
|
||||
R4(block, c, d, e, a, b, 9);
|
||||
R4(block, b, c, d, e, a, 10);
|
||||
R4(block, a, b, c, d, e, 11);
|
||||
R4(block, e, a, b, c, d, 12);
|
||||
R4(block, d, e, a, b, c, 13);
|
||||
R4(block, c, d, e, a, b, 14);
|
||||
R4(block, b, c, d, e, a, 15);
|
||||
|
||||
/* Add the working vars back into digest[] */
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
|
||||
/* Count the number of transformations */
|
||||
transforms++;
|
||||
}
|
||||
|
||||
|
||||
static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
|
||||
{
|
||||
/* Convert the std::string (byte buffer) to a uint32_t array (MSB) */
|
||||
for (size_t i = 0; i < BLOCK_INTS; i++)
|
||||
static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms)
|
||||
{
|
||||
block[i] = (buffer[4*i+3] & 0xff)
|
||||
| (buffer[4*i+2] & 0xff)<<8
|
||||
| (buffer[4*i+1] & 0xff)<<16
|
||||
| (buffer[4*i+0] & 0xff)<<24;
|
||||
/* Copy digest[] to working vars */
|
||||
uint32_t a = digest[0];
|
||||
uint32_t b = digest[1];
|
||||
uint32_t c = digest[2];
|
||||
uint32_t d = digest[3];
|
||||
uint32_t e = digest[4];
|
||||
|
||||
/* 4 rounds of 20 operations each. Loop unrolled. */
|
||||
R0(block, a, b, c, d, e, 0);
|
||||
R0(block, e, a, b, c, d, 1);
|
||||
R0(block, d, e, a, b, c, 2);
|
||||
R0(block, c, d, e, a, b, 3);
|
||||
R0(block, b, c, d, e, a, 4);
|
||||
R0(block, a, b, c, d, e, 5);
|
||||
R0(block, e, a, b, c, d, 6);
|
||||
R0(block, d, e, a, b, c, 7);
|
||||
R0(block, c, d, e, a, b, 8);
|
||||
R0(block, b, c, d, e, a, 9);
|
||||
R0(block, a, b, c, d, e, 10);
|
||||
R0(block, e, a, b, c, d, 11);
|
||||
R0(block, d, e, a, b, c, 12);
|
||||
R0(block, c, d, e, a, b, 13);
|
||||
R0(block, b, c, d, e, a, 14);
|
||||
R0(block, a, b, c, d, e, 15);
|
||||
R1(block, e, a, b, c, d, 0);
|
||||
R1(block, d, e, a, b, c, 1);
|
||||
R1(block, c, d, e, a, b, 2);
|
||||
R1(block, b, c, d, e, a, 3);
|
||||
R2(block, a, b, c, d, e, 4);
|
||||
R2(block, e, a, b, c, d, 5);
|
||||
R2(block, d, e, a, b, c, 6);
|
||||
R2(block, c, d, e, a, b, 7);
|
||||
R2(block, b, c, d, e, a, 8);
|
||||
R2(block, a, b, c, d, e, 9);
|
||||
R2(block, e, a, b, c, d, 10);
|
||||
R2(block, d, e, a, b, c, 11);
|
||||
R2(block, c, d, e, a, b, 12);
|
||||
R2(block, b, c, d, e, a, 13);
|
||||
R2(block, a, b, c, d, e, 14);
|
||||
R2(block, e, a, b, c, d, 15);
|
||||
R2(block, d, e, a, b, c, 0);
|
||||
R2(block, c, d, e, a, b, 1);
|
||||
R2(block, b, c, d, e, a, 2);
|
||||
R2(block, a, b, c, d, e, 3);
|
||||
R2(block, e, a, b, c, d, 4);
|
||||
R2(block, d, e, a, b, c, 5);
|
||||
R2(block, c, d, e, a, b, 6);
|
||||
R2(block, b, c, d, e, a, 7);
|
||||
R3(block, a, b, c, d, e, 8);
|
||||
R3(block, e, a, b, c, d, 9);
|
||||
R3(block, d, e, a, b, c, 10);
|
||||
R3(block, c, d, e, a, b, 11);
|
||||
R3(block, b, c, d, e, a, 12);
|
||||
R3(block, a, b, c, d, e, 13);
|
||||
R3(block, e, a, b, c, d, 14);
|
||||
R3(block, d, e, a, b, c, 15);
|
||||
R3(block, c, d, e, a, b, 0);
|
||||
R3(block, b, c, d, e, a, 1);
|
||||
R3(block, a, b, c, d, e, 2);
|
||||
R3(block, e, a, b, c, d, 3);
|
||||
R3(block, d, e, a, b, c, 4);
|
||||
R3(block, c, d, e, a, b, 5);
|
||||
R3(block, b, c, d, e, a, 6);
|
||||
R3(block, a, b, c, d, e, 7);
|
||||
R3(block, e, a, b, c, d, 8);
|
||||
R3(block, d, e, a, b, c, 9);
|
||||
R3(block, c, d, e, a, b, 10);
|
||||
R3(block, b, c, d, e, a, 11);
|
||||
R4(block, a, b, c, d, e, 12);
|
||||
R4(block, e, a, b, c, d, 13);
|
||||
R4(block, d, e, a, b, c, 14);
|
||||
R4(block, c, d, e, a, b, 15);
|
||||
R4(block, b, c, d, e, a, 0);
|
||||
R4(block, a, b, c, d, e, 1);
|
||||
R4(block, e, a, b, c, d, 2);
|
||||
R4(block, d, e, a, b, c, 3);
|
||||
R4(block, c, d, e, a, b, 4);
|
||||
R4(block, b, c, d, e, a, 5);
|
||||
R4(block, a, b, c, d, e, 6);
|
||||
R4(block, e, a, b, c, d, 7);
|
||||
R4(block, d, e, a, b, c, 8);
|
||||
R4(block, c, d, e, a, b, 9);
|
||||
R4(block, b, c, d, e, a, 10);
|
||||
R4(block, a, b, c, d, e, 11);
|
||||
R4(block, e, a, b, c, d, 12);
|
||||
R4(block, d, e, a, b, c, 13);
|
||||
R4(block, c, d, e, a, b, 14);
|
||||
R4(block, b, c, d, e, a, 15);
|
||||
|
||||
/* Add the working vars back into digest[] */
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
|
||||
/* Count the number of transformations */
|
||||
transforms++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Sha1::Sha1()
|
||||
{
|
||||
reset(digest, buffer, transforms);
|
||||
}
|
||||
|
||||
|
||||
void Sha1::update(const std::string &s)
|
||||
{
|
||||
std::istringstream is(s);
|
||||
update(is);
|
||||
}
|
||||
|
||||
|
||||
void Sha1::update(std::istream &is)
|
||||
{
|
||||
while (true)
|
||||
static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS])
|
||||
{
|
||||
char sbuf[BLOCK_BYTES];
|
||||
is.read(sbuf, BLOCK_BYTES - buffer.size());
|
||||
buffer.append(sbuf, is.gcount());
|
||||
if (buffer.size() != BLOCK_BYTES)
|
||||
/* Convert the std::string (byte buffer) to a uint32_t array (MSB) */
|
||||
for(size_t i = 0; i < BLOCK_INTS; i++)
|
||||
{
|
||||
return;
|
||||
block[i] = (buffer[4 * i + 3] & 0xff)
|
||||
| (buffer[4 * i + 2] & 0xff) << 8
|
||||
| (buffer[4 * i + 1] & 0xff) << 16
|
||||
| (buffer[4 * i + 0] & 0xff) << 24;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Sha1::Sha1()
|
||||
{
|
||||
reset(digest, buffer, transforms);
|
||||
}
|
||||
|
||||
|
||||
void Sha1::update(const std::string &s)
|
||||
{
|
||||
std::istringstream is(s);
|
||||
update(is);
|
||||
}
|
||||
|
||||
|
||||
void Sha1::update(std::istream &is)
|
||||
{
|
||||
while(true)
|
||||
{
|
||||
char sbuf[BLOCK_BYTES];
|
||||
is.read(sbuf, BLOCK_BYTES - buffer.size());
|
||||
buffer.append(sbuf, is.gcount());
|
||||
if(buffer.size() != BLOCK_BYTES)
|
||||
{
|
||||
return;
|
||||
}
|
||||
uint32_t block[BLOCK_INTS];
|
||||
buffer_to_block(buffer, block);
|
||||
transform(digest, block, transforms);
|
||||
buffer.clear();
|
||||
}
|
||||
uint32_t block[BLOCK_INTS];
|
||||
buffer_to_block(buffer, block);
|
||||
transform(digest, block, transforms);
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Add padding and finish up
|
||||
*/
|
||||
|
||||
void Sha1::final()
|
||||
{
|
||||
/* Total number of hashed bits */
|
||||
uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8;
|
||||
|
||||
/* Padding */
|
||||
buffer += 0x80;
|
||||
size_t orig_size = buffer.size();
|
||||
while (buffer.size() < BLOCK_BYTES)
|
||||
void Sha1::final()
|
||||
{
|
||||
buffer += (char)0x00;
|
||||
}
|
||||
/* Total number of hashed bits */
|
||||
uint64_t total_bits = (transforms * BLOCK_BYTES + buffer.size()) * 8;
|
||||
|
||||
uint32_t block[BLOCK_INTS];
|
||||
buffer_to_block(buffer, block);
|
||||
|
||||
if (orig_size > BLOCK_BYTES - 8)
|
||||
{
|
||||
transform(digest, block, transforms);
|
||||
for (size_t i = 0; i < BLOCK_INTS - 2; i++)
|
||||
/* Padding */
|
||||
buffer += 0x80;
|
||||
size_t orig_size = buffer.size();
|
||||
while(buffer.size() < BLOCK_BYTES)
|
||||
{
|
||||
block[i] = 0;
|
||||
buffer += (char) 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
/* Append total_bits, split this uint64_t into two uint32_t */
|
||||
block[BLOCK_INTS - 1] = total_bits;
|
||||
block[BLOCK_INTS - 2] = (total_bits >> 32);
|
||||
transform(digest, block, transforms);
|
||||
}
|
||||
uint32_t block[BLOCK_INTS];
|
||||
buffer_to_block(buffer, block);
|
||||
|
||||
if(orig_size > BLOCK_BYTES - 8)
|
||||
{
|
||||
transform(digest, block, transforms);
|
||||
for(size_t i = 0; i < BLOCK_INTS - 2; i++)
|
||||
{
|
||||
block[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Append total_bits, split this uint64_t into two uint32_t */
|
||||
block[BLOCK_INTS - 1] = total_bits;
|
||||
block[BLOCK_INTS - 2] = (total_bits >> 32);
|
||||
transform(digest, block, transforms);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user