nothing else tonight

This commit is contained in:
malloc 2018-03-14 23:01:24 -05:00
parent 4f1c530941
commit 7aaad6943c
4 changed files with 46 additions and 10 deletions

View file

@ -2,6 +2,17 @@
#define SHA1_BLOCK_SIZE 64
#undef A
#define A 0
#undef B
#define B 1
#undef C
#define C 2
#undef D
#define D 3
#undef E
#define E 4
static inline uint32_t Z(uint32_t x, uint32_t y) {
return x + y;
}
@ -12,17 +23,17 @@ static inline uint32_t S(uint32_t X, uint8_t n) {
}
static uint32_t f
(uint8_t t, uint32_t B, uint32_t C, uint32_t D)
(uint8_t t, uint32_t b, uint32_t c, uint32_t d)
{
t = t % 80;
if(t <= 19)
return (B & C) | (~B & D);
return (b & c) | (~b & d);
else if(t >= 20 && t <= 39)
return B ^ C ^ D;
return b ^ c ^ d;
else if(t >= 40 && t <= 59)
return (B & C) | (B & D) | (C & D);
return (b & c) | (b & d) | (c & d);
else
return B ^ C ^ D;
return b ^ c ^ d;
}
static uint32_t K(uint8_t t) {
@ -58,16 +69,36 @@ std::string sosc::cgc::sha1(const std::string& data) {
0x98BADCFE,
0x10325476,
0xC3D2E1F0
}, W[80], A, B, C, D, E;
}, W[80], T, O[5];
for(int i = 0; i < data.length() + padding.length(); i += SHA1_BLOCK_SIZE)
{
for(int j = 0; j < 16; ++j) {
W[j] = 0;
for(int k = 0; k < 4; ++k) {
if(i + j + k < data.length())
W[j] |= (data[i + j + k]
W[j] |= (((i + j + k < data.length())
? data : padding)[i + j + k] >> (8 * (3 - i))) & 0xFF;
}
}
for(int j = 16; j < 80; ++j)
W[j] = S(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1);
for(int j = 0; j < 5; ++j)
O[j] = H[j];
for(int j = 0; j < 80; ++j) {
T = S(O[A], 5) + f(j, O[B], O[C], O[D]) + O[E] + W[j] + K(j);
O[E] = O[D];
O[D] = O[C];
O[C] = S(O[B], 30);
O[B] = O[A];
O[A] = T;
}
for(int j = 0; j < 5; ++j)
H[j] += O[j];
}
return "";
}

View file

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

View file

@ -133,3 +133,7 @@ bool sosc::Packet::Check(int region_count, ...) {
return true;
}
std::string sosc::Packet::ToString() const {
return "TODO";
}