From a67dc94484cf9869793fc1861914b800a6559a74 Mon Sep 17 00:00:00 2001 From: amelia squires Date: Tue, 30 Sep 2025 18:10:02 -0500 Subject: fix indentation!!! --- src/hash/adler.c | 2 +- src/hash/blake.c | 266 +++++++++++----------- src/hash/blake.h | 18 +- src/hash/blake2.c | 588 ++++++++++++++++++++++++------------------------ src/hash/bsdchecksum.c | 10 +- src/hash/buzhash.c | 28 +-- src/hash/crc.c | 84 +++---- src/hash/crc.h | 6 +- src/hash/djb2.c | 2 +- src/hash/djb2.h | 2 +- src/hash/fletcher.c | 18 +- src/hash/fletcher.h | 8 +- src/hash/fnv.c | 16 +- src/hash/fnv.h | 2 +- src/hash/md5.c | 34 +-- src/hash/murmur.c | 116 +++++----- src/hash/pearson.c | 6 +- src/hash/pearson.h | 2 +- src/hash/pjw.c | 38 ++-- src/hash/pjw.h | 2 +- src/hash/sdbm.h | 2 +- src/hash/sha01.c | 152 ++++++------- src/hash/sha01.h | 2 +- src/hash/sha2-256.c | 300 ++++++++++++------------ src/hash/sha2-256.h | 14 +- src/hash/sha2xx.c | 228 +++++++++---------- src/hash/sha2xx.h | 8 +- src/hash/sysvchecksum.c | 4 +- src/hash/sysvchecksum.h | 2 +- src/hash/xor.c | 20 +- src/hash/xor.h | 4 +- 31 files changed, 992 insertions(+), 992 deletions(-) (limited to 'src/hash') diff --git a/src/hash/adler.c b/src/hash/adler.c index b4a1135..de007e0 100644 --- a/src/hash/adler.c +++ b/src/hash/adler.c @@ -44,7 +44,7 @@ int l_adler32(lua_State* L){ if(lua_gettop(L) == 0) return l_adler32_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[32]; uint32_t u = adler32(a, len); diff --git a/src/hash/blake.c b/src/hash/blake.c index 38d7da2..ca07482 100644 --- a/src/hash/blake.c +++ b/src/hash/blake.c @@ -88,65 +88,65 @@ void compress256(uint32_t* hash, char *block, uint64_t compressed){ #define bs 64 struct blake256_hash blake256_init(){ - struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - a.hash = calloc(sizeof * a.hash, 8); - a.hash[0] = 0x6a09e667; - a.hash[1] = 0xbb67ae85; - a.hash[2] = 0x3c6ef372; - a.hash[3] = 0xa54ff53a; - a.hash[4] = 0x510e527f; - a.hash[5] = 0x9b05688c; - a.hash[6] = 0x1f83d9ab; - a.hash[7] = 0x5be0cd19; - return a; + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0x6a09e667; + a.hash[1] = 0xbb67ae85; + a.hash[2] = 0x3c6ef372; + a.hash[3] = 0xa54ff53a; + a.hash[4] = 0x510e527f; + a.hash[5] = 0x9b05688c; + a.hash[6] = 0x1f83d9ab; + a.hash[7] = 0x5be0cd19; + return a; } struct blake256_hash blake256_init_l(lua_State* L){ - struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - a.hash = calloc(sizeof * a.hash, 8); - memset(a.buffer, 0, bs); - a.hash[0] = 0x6a09e667; - a.hash[1] = 0xbb67ae85; - a.hash[2] = 0x3c6ef372; - a.hash[3] = 0xa54ff53a; - a.hash[4] = 0x510e527f; - a.hash[5] = 0x9b05688c; - a.hash[6] = 0x1f83d9ab; - a.hash[7] = 0x5be0cd19; - return a; + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + memset(a.buffer, 0, bs); + a.hash[0] = 0x6a09e667; + a.hash[1] = 0xbb67ae85; + a.hash[2] = 0x3c6ef372; + a.hash[3] = 0xa54ff53a; + a.hash[4] = 0x510e527f; + a.hash[5] = 0x9b05688c; + a.hash[6] = 0x1f83d9ab; + a.hash[7] = 0x5be0cd19; + return a; } struct blake256_hash blake224_init(){ - struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - a.hash = calloc(sizeof * a.hash, 8); - a.hash[0] = 0xc1059ed8; - a.hash[1] = 0x367cd507; - a.hash[2] = 0x3070dd17; - a.hash[3] = 0xf70e5939; - a.hash[4] = 0xffc00b31; - a.hash[5] = 0x68581511; - a.hash[6] = 0x64f98fa7; - a.hash[7] = 0xbefa4fa4; - return a; + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0xc1059ed8; + a.hash[1] = 0x367cd507; + a.hash[2] = 0x3070dd17; + a.hash[3] = 0xf70e5939; + a.hash[4] = 0xffc00b31; + a.hash[5] = 0x68581511; + a.hash[6] = 0x64f98fa7; + a.hash[7] = 0xbefa4fa4; + return a; } struct blake256_hash blake224_init_l(lua_State* L){ - struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - a.hash = calloc(sizeof * a.hash, 8); - memset(a.buffer, 0, bs); - a.hash[0] = 0xc1059ed8; - a.hash[1] = 0x367cd507; - a.hash[2] = 0x3070dd17; - a.hash[3] = 0xf70e5939; - a.hash[4] = 0xffc00b31; - a.hash[5] = 0x68581511; - a.hash[6] = 0x64f98fa7; - a.hash[7] = 0xbefa4fa4; - return a; + struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + memset(a.buffer, 0, bs); + a.hash[0] = 0xc1059ed8; + a.hash[1] = 0x367cd507; + a.hash[2] = 0x3070dd17; + a.hash[3] = 0xf70e5939; + a.hash[4] = 0xffc00b31; + a.hash[5] = 0x68581511; + a.hash[6] = 0x64f98fa7; + a.hash[7] = 0xbefa4fa4; + return a; } int blake256_free_l(lua_State* L){ @@ -250,7 +250,7 @@ void blake224_final(struct blake256_hash* hash, char* out_stream){ for(int i = 0; i != 7; i++){ sprintf(out_stream + i * 8, "%08x",(hash->hash)[i]); } - + memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); memcpy(hash->hash, hhash, sizeof * hhash * 8); @@ -285,7 +285,7 @@ void blake224(char *out, char *in, uint64_t inlen){ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){ uint64_t v[16], m[16], i; - for( i = 0; i < 16; ++i ) m[i] = U8TO64_BIG( block + i * 8 ); + for( i = 0; i < 16; ++i ) m[i] = U8TO64_BIG( block + i * 8 ); for(int i = 0; i < 8; i++) v[i] = hash[i]; @@ -308,7 +308,7 @@ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){ blake_round_512(1, 6, 11, 12, 10); blake_round_512(2, 7, 8, 13, 12); blake_round_512(3, 4, 9, 14, 14); - + } for(int i = 0; i < 16; i++) hash[i % 8] ^= v[i]; @@ -318,65 +318,65 @@ void compress512(uint64_t* hash, uint8_t *block, uint64_t compressed){ #define bs_2 128 struct blake512_hash blake512_init(){ - struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs_2); - a.hash = calloc(sizeof * a.hash, 8); - a.hash[0] = 0x6a09e667f3bcc908ULL; - a.hash[1] = 0xbb67ae8584caa73bULL; - a.hash[2] = 0x3c6ef372fe94f82bULL; - a.hash[3] = 0xa54ff53a5f1d36f1ULL; - a.hash[4] = 0x510e527fade682d1ULL; - a.hash[5] = 0x9b05688c2b3e6c1fULL; - a.hash[6] = 0x1f83d9abfb41bd6bULL; - a.hash[7] = 0x5be0cd19137e2179ULL; - return a; + struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0x6a09e667f3bcc908ULL; + a.hash[1] = 0xbb67ae8584caa73bULL; + a.hash[2] = 0x3c6ef372fe94f82bULL; + a.hash[3] = 0xa54ff53a5f1d36f1ULL; + a.hash[4] = 0x510e527fade682d1ULL; + a.hash[5] = 0x9b05688c2b3e6c1fULL; + a.hash[6] = 0x1f83d9abfb41bd6bULL; + a.hash[7] = 0x5be0cd19137e2179ULL; + return a; } struct blake512_hash blake512_init_l(lua_State* L){ - struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs_2); - a.hash = calloc(sizeof * a.hash, 8); - memset(a.buffer, 0, bs_2); - a.hash[0] = 0x6a09e667f3bcc908ULL; - a.hash[1] = 0xbb67ae8584caa73bULL; - a.hash[2] = 0x3c6ef372fe94f82bULL; - a.hash[3] = 0xa54ff53a5f1d36f1ULL; - a.hash[4] = 0x510e527fade682d1ULL; - a.hash[5] = 0x9b05688c2b3e6c1fULL; - a.hash[6] = 0x1f83d9abfb41bd6bULL; - a.hash[7] = 0x5be0cd19137e2179ULL; - return a; + struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + memset(a.buffer, 0, bs_2); + a.hash[0] = 0x6a09e667f3bcc908ULL; + a.hash[1] = 0xbb67ae8584caa73bULL; + a.hash[2] = 0x3c6ef372fe94f82bULL; + a.hash[3] = 0xa54ff53a5f1d36f1ULL; + a.hash[4] = 0x510e527fade682d1ULL; + a.hash[5] = 0x9b05688c2b3e6c1fULL; + a.hash[6] = 0x1f83d9abfb41bd6bULL; + a.hash[7] = 0x5be0cd19137e2179ULL; + return a; } struct blake384_hash blake384_init(){ - struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs_2); - a.hash = calloc(sizeof * a.hash, 8); - a.hash[0] = 0xcbbb9d5dc1059ed8ULL; - a.hash[1] = 0x629a292a367cd507ULL; - a.hash[2] = 0x9159015a3070dd17ULL; - a.hash[3] = 0x152fecd8f70e5939ULL; - a.hash[4] = 0x67332667ffc00b31ULL; - a.hash[5] = 0x8eb44a8768581511ULL; - a.hash[6] = 0xdb0c2e0d64f98fa7ULL; - a.hash[7] = 0x47b5481dbefa4fa4ULL; - return a; + struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + a.hash[0] = 0xcbbb9d5dc1059ed8ULL; + a.hash[1] = 0x629a292a367cd507ULL; + a.hash[2] = 0x9159015a3070dd17ULL; + a.hash[3] = 0x152fecd8f70e5939ULL; + a.hash[4] = 0x67332667ffc00b31ULL; + a.hash[5] = 0x8eb44a8768581511ULL; + a.hash[6] = 0xdb0c2e0d64f98fa7ULL; + a.hash[7] = 0x47b5481dbefa4fa4ULL; + return a; } struct blake384_hash blake384_init_l(lua_State* L){ - struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = calloc(sizeof * a.buffer, bs_2); - a.hash = calloc(sizeof * a.hash, 8); - memset(a.buffer, 0, bs_2); - a.hash[0] = 0xcbbb9d5dc1059ed8ULL; - a.hash[1] = 0x629a292a367cd507ULL; - a.hash[2] = 0x9159015a3070dd17ULL; - a.hash[3] = 0x152fecd8f70e5939ULL; - a.hash[4] = 0x67332667ffc00b31ULL; - a.hash[5] = 0x8eb44a8768581511ULL; - a.hash[6] = 0xdb0c2e0d64f98fa7ULL; - a.hash[7] = 0x47b5481dbefa4fa4ULL; - return a; + struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + memset(a.buffer, 0, bs_2); + a.hash[0] = 0xcbbb9d5dc1059ed8ULL; + a.hash[1] = 0x629a292a367cd507ULL; + a.hash[2] = 0x9159015a3070dd17ULL; + a.hash[3] = 0x152fecd8f70e5939ULL; + a.hash[4] = 0x67332667ffc00b31ULL; + a.hash[5] = 0x8eb44a8768581511ULL; + a.hash[6] = 0xdb0c2e0d64f98fa7ULL; + a.hash[7] = 0x47b5481dbefa4fa4ULL; + return a; } int blake512_free_l(lua_State* L){ @@ -528,17 +528,17 @@ int l_blake256_final(lua_State* L){ } int l_blake256(lua_State* L){ - if(lua_gettop(L) == 0) return l_blake256_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + if(lua_gettop(L) == 0) return l_blake256_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[257] = {0}; + char digest[257] = {0}; - blake256(digest, (char*)a, len); + blake256(digest, (char*)a, len); - lua_pushstring(L, digest); + lua_pushstring(L, digest); - return 1; + return 1; } int l_blake224_clone(lua_State* L){ @@ -569,17 +569,17 @@ int l_blake224_final(lua_State* L){ } int l_blake224(lua_State* L){ - if(lua_gettop(L) == 0) return l_blake224_init(L); - size_t len = 0; - char* a = (char*)luaL_checklstring(L, 1, &len); + if(lua_gettop(L) == 0) return l_blake224_init(L); + size_t len = 0; + char* a = (char*)luaL_checklstring(L, 1, &len); - char digest[257] = {0}; + char digest[257] = {0}; - blake224(digest, (char*)a, len); + blake224(digest, (char*)a, len); - lua_pushstring(L, digest); + lua_pushstring(L, digest); - return 1; + return 1; } int l_blake512_clone(lua_State* L){ @@ -610,18 +610,18 @@ int l_blake512_final(lua_State* L){ } int l_blake512(lua_State* L){ - if(lua_gettop(L) == 0) return l_blake512_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + if(lua_gettop(L) == 0) return l_blake512_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[513] = {0}; - //memset(digest, 0, 513); + char digest[513] = {0}; + //memset(digest, 0, 513); - //blake512(digest, a, len, b512); - blake512(a, len, digest); - lua_pushstring(L, digest); + //blake512(digest, a, len, b512); + blake512(a, len, digest); + lua_pushstring(L, digest); - return 1; + return 1; } int l_blake384_clone(lua_State* L){ @@ -652,14 +652,14 @@ int l_blake384_final(lua_State* L){ } int l_blake384(lua_State* L){ - if(lua_gettop(L) == 0) return l_blake384_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + if(lua_gettop(L) == 0) return l_blake384_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[513] = {0}; + char digest[513] = {0}; - blake384(a, len, digest); - lua_pushstring(L, digest); + blake384(a, len, digest); + lua_pushstring(L, digest); - return 1; + return 1; } diff --git a/src/hash/blake.h b/src/hash/blake.h index 07caf3d..3dacef5 100644 --- a/src/hash/blake.h +++ b/src/hash/blake.h @@ -19,18 +19,18 @@ #define wtf(b) (b[0] << 24)&0xff000000 | (b[1] << 16)&0xff0000 | (b[2] << 8)&0xff00 | b[3]&0xff struct blake256_hash { - uint8_t* buffer; - size_t bufflen; - uint32_t total, *hash; - uint64_t compressed; + uint8_t* buffer; + size_t bufflen; + uint32_t total, *hash; + uint64_t compressed; }; #define blake224_hash blake256_hash struct blake512_hash { - uint8_t* buffer; - size_t bufflen; - uint64_t total, *hash; - uint64_t compressed; + uint8_t* buffer; + size_t bufflen; + uint64_t total, *hash; + uint64_t compressed; }; #define blake384_hash blake512_hash @@ -52,4 +52,4 @@ int l_blake512_final(lua_State* L); int l_blake384(lua_State* L); int l_blake384_init(lua_State* L); int l_blake384_update(lua_State* L); -int l_blake384_final(lua_State* L); \ No newline at end of file +int l_blake384_final(lua_State* L); diff --git a/src/hash/blake2.c b/src/hash/blake2.c index 42dc0ab..974a323 100644 --- a/src/hash/blake2.c +++ b/src/hash/blake2.c @@ -5,125 +5,125 @@ #include "../crypto.h" void mix2b(uint64_t* a, uint64_t* b, uint64_t* c, uint64_t* d, int64_t x, int64_t y){ - *a = *a + *b + x; - *d = rotr64((*d ^ *a), 32); + *a = *a + *b + x; + *d = rotr64((*d ^ *a), 32); - *c += *d; - *b = rotr64((*b ^ *c), 24); + *c += *d; + *b = rotr64((*b ^ *c), 24); - *a += *b + y; - *d = rotr64((*d ^ *a), 16); + *a += *b + y; + *d = rotr64((*d ^ *a), 16); - *c += *d; - *b = rotr64((*b ^ *c), 63); + *c += *d; + *b = rotr64((*b ^ *c), 63); } void mix2s(uint32_t* a, uint32_t* b, uint32_t* c, uint32_t* d, int32_t x, int32_t y){ - *a = *a + *b + x; - *d = rotr32((*d ^ *a), 16); + *a = *a + *b + x; + *d = rotr32((*d ^ *a), 16); - *c += *d; - *b = rotr32((*b ^ *c), 12); + *c += *d; + *b = rotr32((*b ^ *c), 12); - *a += *b + y; - *d = rotr32((*d ^ *a), 8); + *a += *b + y; + *d = rotr32((*d ^ *a), 8); - *c += *d; - *b = rotr32((*b ^ *c), 7); + *c += *d; + *b = rotr32((*b ^ *c), 7); } #define baking(type) \ - mix2##type(&v[0], &v[4], &v[8], &v[12], m[s[0]], m[s[1]]);\ - mix2##type(&v[1], &v[5], &v[9], &v[13], m[s[2]], m[s[3]]);\ - mix2##type(&v[2], &v[6], &v[10], &v[14], m[s[4]], m[s[5]]);\ - mix2##type(&v[3], &v[7], &v[11], &v[15], m[s[6]], m[s[7]]);\ - \ - mix2##type(&v[0], &v[5], &v[10], &v[15], m[s[8]], m[s[9]]);\ - mix2##type(&v[1], &v[6], &v[11], &v[12], m[s[10]], m[s[11]]);\ - mix2##type(&v[2], &v[7], &v[8], &v[13], m[s[12]], m[s[13]]);\ - mix2##type(&v[3], &v[4], &v[9], &v[14], m[s[14]], m[s[15]]); + mix2##type(&v[0], &v[4], &v[8], &v[12], m[s[0]], m[s[1]]);\ + mix2##type(&v[1], &v[5], &v[9], &v[13], m[s[2]], m[s[3]]);\ + mix2##type(&v[2], &v[6], &v[10], &v[14], m[s[4]], m[s[5]]);\ + mix2##type(&v[3], &v[7], &v[11], &v[15], m[s[6]], m[s[7]]);\ + \ + mix2##type(&v[0], &v[5], &v[10], &v[15], m[s[8]], m[s[9]]);\ + mix2##type(&v[1], &v[6], &v[11], &v[12], m[s[10]], m[s[11]]);\ + mix2##type(&v[2], &v[7], &v[8], &v[13], m[s[12]], m[s[13]]);\ + mix2##type(&v[3], &v[4], &v[9], &v[14], m[s[14]], m[s[15]]); void compress2b(uint64_t* hash, uint8_t* inp, uint64_t compressed, int final){ - uint64_t v[16], s[16], m[16]; - - #pragma unroll - for(int i = 0; i != 8; i++) - v[i] = hash[i]; - - for(int i = 0; i != 16; i++) - m[i] = ((uint64_t*)inp)[i]; - - v[8] = sha512_iv.h0; - v[9] = sha512_iv.h1; - v[10] = sha512_iv.h2; - v[11] = sha512_iv.h3; - v[12] = sha512_iv.h4; - v[13] = sha512_iv.h5; - v[14] = sha512_iv.h6; - v[15] = sha512_iv.h7; - - v[12] ^= compressed; - v[13] ^= 0; - - if(final) - v[14] ^= 0xFFFFFFFFFFFFFFFF; - - for(int i = 0; i != 12; i++){ - for(int j = 0; j != 16; j++){ - s[j] = blake2b_sigma[i%10][j]; - } - - baking(b); - } - - for (int i = 0; i < 8; i++) { - hash[i] = hash[i] ^ v[i] ^ v[i + 8]; + uint64_t v[16], s[16], m[16]; + +#pragma unroll + for(int i = 0; i != 8; i++) + v[i] = hash[i]; + + for(int i = 0; i != 16; i++) + m[i] = ((uint64_t*)inp)[i]; + + v[8] = sha512_iv.h0; + v[9] = sha512_iv.h1; + v[10] = sha512_iv.h2; + v[11] = sha512_iv.h3; + v[12] = sha512_iv.h4; + v[13] = sha512_iv.h5; + v[14] = sha512_iv.h6; + v[15] = sha512_iv.h7; + + v[12] ^= compressed; + v[13] ^= 0; + + if(final) + v[14] ^= 0xFFFFFFFFFFFFFFFF; + + for(int i = 0; i != 12; i++){ + for(int j = 0; j != 16; j++){ + s[j] = blake2b_sigma[i%10][j]; } + + baking(b); + } + + for (int i = 0; i < 8; i++) { + hash[i] = hash[i] ^ v[i] ^ v[i + 8]; + } } void compress2s(uint32_t* hash, uint8_t* inp, uint32_t compressed, int final){ - uint32_t v[16], s[16], m[16]; - - #pragma unroll - for(int i = 0; i != 8; i++) - v[i] = hash[i]; - - for(int i = 0; i != 16; i++) - m[i] = ((uint32_t*)inp)[i]; - - v[8] = sha512_iv.h0 >> 32; - v[9] = sha512_iv.h1 >> 32; - v[10] = sha512_iv.h2 >> 32; - v[11] = sha512_iv.h3 >> 32; - v[12] = sha512_iv.h4 >> 32; - v[13] = sha512_iv.h5 >> 32; - v[14] = sha512_iv.h6 >> 32; - v[15] = sha512_iv.h7 >> 32; - - v[12] ^= compressed; //make this 64bit - v[13] ^= 0; - - if(final) - v[14] ^= 0xFFFFFFFFFFFFFFFF >> 32; - - for(int i = 0; i != 10; i++){ - for(int j = 0; j != 16; j++){ - s[j] = blake2b_sigma[i][j]; - } - - baking(s); - } - - for (int i = 0; i < 8; i++) { - hash[i] = hash[i] ^ v[i] ^ v[i + 8]; + uint32_t v[16], s[16], m[16]; + +#pragma unroll + for(int i = 0; i != 8; i++) + v[i] = hash[i]; + + for(int i = 0; i != 16; i++) + m[i] = ((uint32_t*)inp)[i]; + + v[8] = sha512_iv.h0 >> 32; + v[9] = sha512_iv.h1 >> 32; + v[10] = sha512_iv.h2 >> 32; + v[11] = sha512_iv.h3 >> 32; + v[12] = sha512_iv.h4 >> 32; + v[13] = sha512_iv.h5 >> 32; + v[14] = sha512_iv.h6 >> 32; + v[15] = sha512_iv.h7 >> 32; + + v[12] ^= compressed; //make this 64bit + v[13] ^= 0; + + if(final) + v[14] ^= 0xFFFFFFFFFFFFFFFF >> 32; + + for(int i = 0; i != 10; i++){ + for(int j = 0; j != 16; j++){ + s[j] = blake2b_sigma[i][j]; } + + baking(s); + } + + for (int i = 0; i < 8; i++) { + hash[i] = hash[i] ^ v[i] ^ v[i + 8]; + } } struct blake2b_hash { - uint8_t *buffer, *key; - size_t bufflen, keylen; - uint64_t total, *hash; - uint64_t compressed, digest_len; + uint8_t *buffer, *key; + size_t bufflen, keylen; + uint64_t total, *hash; + uint64_t compressed, digest_len; }; void blake2b_round(struct blake2b_hash* hash, int last){ @@ -132,60 +132,60 @@ void blake2b_round(struct blake2b_hash* hash, int last){ #define bs_2 128 struct blake2b_hash blake2b_init(char* key, int key_len, int digest_len){ - struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; - a.buffer = calloc(sizeof * a.buffer, bs_2); - a.hash = calloc(sizeof * a.hash, 8); - a.key = calloc(sizeof* a.key, key_len); - memcpy(a.key, key, key_len); - memcpy(a.buffer, key, key_len); - - a.hash[0] = sha512_iv.h0; - a.hash[0] ^= digest_len; - a.hash[0] ^= key_len << 8; - a.hash[0] ^= 0x01010000; - a.hash[1] = sha512_iv.h1; - a.hash[2] = sha512_iv.h2; - a.hash[3] = sha512_iv.h3; - a.hash[4] = sha512_iv.h4; - a.hash[5] = sha512_iv.h5; - a.hash[6] = sha512_iv.h6; - a.hash[7] = sha512_iv.h7; - if(key_len != 0){ - a.compressed = 128; - blake2b_round(&a, 0); - memset(a.buffer, 0, bs_2); - a.bufflen = 0; - } - return a; + struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + a.key = calloc(sizeof* a.key, key_len); + memcpy(a.key, key, key_len); + memcpy(a.buffer, key, key_len); + + a.hash[0] = sha512_iv.h0; + a.hash[0] ^= digest_len; + a.hash[0] ^= key_len << 8; + a.hash[0] ^= 0x01010000; + a.hash[1] = sha512_iv.h1; + a.hash[2] = sha512_iv.h2; + a.hash[3] = sha512_iv.h3; + a.hash[4] = sha512_iv.h4; + a.hash[5] = sha512_iv.h5; + a.hash[6] = sha512_iv.h6; + a.hash[7] = sha512_iv.h7; + if(key_len != 0){ + a.compressed = 128; + blake2b_round(&a, 0); + memset(a.buffer, 0, bs_2); + a.bufflen = 0; + } + return a; } struct blake2b_hash blake2b_init_l(lua_State* L, char* key, int key_len, int digest_len){ - struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; - a.buffer = calloc(sizeof * a.buffer, bs_2); - a.hash = calloc(sizeof * a.hash, 8); - a.key = calloc(sizeof* a.key, key_len); + struct blake2b_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; + a.buffer = calloc(sizeof * a.buffer, bs_2); + a.hash = calloc(sizeof * a.hash, 8); + a.key = calloc(sizeof* a.key, key_len); + memset(a.buffer, 0, bs_2); + memcpy(a.key, key, key_len); + memcpy(a.buffer, key, key_len); + + a.hash[0] = sha512_iv.h0; + a.hash[0] ^= digest_len; + a.hash[0] ^= key_len << 8; + a.hash[0] ^= 0x01010000; + a.hash[1] = sha512_iv.h1; + a.hash[2] = sha512_iv.h2; + a.hash[3] = sha512_iv.h3; + a.hash[4] = sha512_iv.h4; + a.hash[5] = sha512_iv.h5; + a.hash[6] = sha512_iv.h6; + a.hash[7] = sha512_iv.h7; + if(key_len != 0){ + a.compressed = 128; + blake2b_round(&a, 0); memset(a.buffer, 0, bs_2); - memcpy(a.key, key, key_len); - memcpy(a.buffer, key, key_len); - - a.hash[0] = sha512_iv.h0; - a.hash[0] ^= digest_len; - a.hash[0] ^= key_len << 8; - a.hash[0] ^= 0x01010000; - a.hash[1] = sha512_iv.h1; - a.hash[2] = sha512_iv.h2; - a.hash[3] = sha512_iv.h3; - a.hash[4] = sha512_iv.h4; - a.hash[5] = sha512_iv.h5; - a.hash[6] = sha512_iv.h6; - a.hash[7] = sha512_iv.h7; - if(key_len != 0){ - a.compressed = 128; - blake2b_round(&a, 0); - memset(a.buffer, 0, bs_2); - a.bufflen = 0; - } - return a; + a.bufflen = 0; + } + return a; } int blake2b_free_l(lua_State* L){ @@ -205,30 +205,30 @@ int blake2s_free_l(lua_State* L){ } void blake2b_update(uint8_t* input, size_t len, struct blake2b_hash* hash){ - hash->total += len; - size_t total_add = len + hash->bufflen; - size_t read = 0; - if(total_add < bs_2){ - memcpy(hash->buffer + hash->bufflen, input, len); - hash->bufflen += len; - return; - } + hash->total += len; + size_t total_add = len + hash->bufflen; + size_t read = 0; + if(total_add < bs_2){ + memcpy(hash->buffer + hash->bufflen, input, len); + hash->bufflen += len; + return; + } - for(; total_add >= bs_2;){ - memcpy(hash->buffer + hash->bufflen, input + read, bs_2 - hash->bufflen); - total_add -= bs_2; - hash->bufflen = 0; - read += bs_2; - hash->compressed += 64; - blake2b_round(hash, 0); - } + for(; total_add >= bs_2;){ + memcpy(hash->buffer + hash->bufflen, input + read, bs_2 - hash->bufflen); + total_add -= bs_2; + hash->bufflen = 0; + read += bs_2; + hash->compressed += 64; + blake2b_round(hash, 0); + } - memset(hash->buffer, 0, bs_2); + memset(hash->buffer, 0, bs_2); - if(0 != total_add){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } + if(0 != total_add){ + memcpy(hash->buffer, input + read, total_add); + hash->bufflen = total_add; + } } void blake2b_final(struct blake2b_hash* hash, char* out_stream){ @@ -245,26 +245,26 @@ void blake2b_final(struct blake2b_hash* hash, char* out_stream){ blake2b_round(hash, 1); for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream + i * 2, "%02x", (((uint8_t*)hash->hash)[i])); - + memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs_2 * sizeof * old); memcpy(hash->hash, hashh, 8 * sizeof * hashh); } void blake2b(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* out){ - struct blake2b_hash aa = blake2b_init(key, key_len, dig_len); - blake2b_update(inp, len, &aa); - blake2b_final(&aa, out); - free(aa.buffer); - free(aa.hash); - free(aa.key); + struct blake2b_hash aa = blake2b_init(key, key_len, dig_len); + blake2b_update(inp, len, &aa); + blake2b_final(&aa, out); + free(aa.buffer); + free(aa.hash); + free(aa.key); } struct blake2s_hash { - uint8_t *buffer, *key; - size_t bufflen, keylen; - uint32_t total, *hash; - uint64_t compressed, digest_len; + uint8_t *buffer, *key; + size_t bufflen, keylen; + uint32_t total, *hash; + uint64_t compressed, digest_len; }; void blake2s_round(struct blake2s_hash* hash, int last){ @@ -273,87 +273,87 @@ void blake2s_round(struct blake2s_hash* hash, int last){ #define bs 64 struct blake2s_hash blake2s_init(char* key, int key_len, int digest_len){ - struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; - a.buffer = calloc(sizeof * a.buffer, bs); - a.hash = calloc(sizeof * a.hash, 8); - a.key = calloc(sizeof* a.key, key_len); - memcpy(a.key, key, key_len); - memcpy(a.buffer, key, key_len); - - a.hash[0] = sha512_iv.h0 >> 32; - a.hash[0] ^= digest_len; - a.hash[0] ^= key_len << 8; - a.hash[0] ^= 0x01010000; - a.hash[1] = sha512_iv.h1 >> 32; - a.hash[2] = sha512_iv.h2 >> 32; - a.hash[3] = sha512_iv.h3 >> 32; - a.hash[4] = sha512_iv.h4 >> 32; - a.hash[5] = sha512_iv.h5 >> 32; - a.hash[6] = sha512_iv.h6 >> 32; - a.hash[7] = sha512_iv.h7 >> 32; - if(key_len != 0){ - a.compressed = 64; - blake2s_round(&a, 0); - memset(a.buffer, 0, bs); - a.bufflen = 0; - } - return a; + struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + a.key = calloc(sizeof* a.key, key_len); + memcpy(a.key, key, key_len); + memcpy(a.buffer, key, key_len); + + a.hash[0] = sha512_iv.h0 >> 32; + a.hash[0] ^= digest_len; + a.hash[0] ^= key_len << 8; + a.hash[0] ^= 0x01010000; + a.hash[1] = sha512_iv.h1 >> 32; + a.hash[2] = sha512_iv.h2 >> 32; + a.hash[3] = sha512_iv.h3 >> 32; + a.hash[4] = sha512_iv.h4 >> 32; + a.hash[5] = sha512_iv.h5 >> 32; + a.hash[6] = sha512_iv.h6 >> 32; + a.hash[7] = sha512_iv.h7 >> 32; + if(key_len != 0){ + a.compressed = 64; + blake2s_round(&a, 0); + memset(a.buffer, 0, bs); + a.bufflen = 0; + } + return a; } struct blake2s_hash blake2s_init_l(lua_State* L, char* key, int key_len, int digest_len){ - struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; - a.buffer = calloc(sizeof * a.buffer, bs); - a.hash = calloc(sizeof * a.hash, 8); - a.key = calloc(sizeof* a.key, key_len); - memcpy(a.key, key, key_len); + struct blake2s_hash a = {.bufflen = key_len, .total = 0, .compressed = 0, .keylen = key_len, .digest_len = digest_len}; + a.buffer = calloc(sizeof * a.buffer, bs); + a.hash = calloc(sizeof * a.hash, 8); + a.key = calloc(sizeof* a.key, key_len); + memcpy(a.key, key, key_len); + memset(a.buffer, 0, bs); + memcpy(a.buffer, key, key_len); + + a.hash[0] = sha512_iv.h0 >> 32; + a.hash[0] ^= digest_len; + a.hash[0] ^= key_len << 8; + a.hash[0] ^= 0x01010000; + a.hash[1] = sha512_iv.h1 >> 32; + a.hash[2] = sha512_iv.h2 >> 32; + a.hash[3] = sha512_iv.h3 >> 32; + a.hash[4] = sha512_iv.h4 >> 32; + a.hash[5] = sha512_iv.h5 >> 32; + a.hash[6] = sha512_iv.h6 >> 32; + a.hash[7] = sha512_iv.h7 >> 32; + if(key_len != 0){ + a.compressed = 64; //TODO: allow for keys larger than 64 chars + blake2s_round(&a, 0); memset(a.buffer, 0, bs); - memcpy(a.buffer, key, key_len); - - a.hash[0] = sha512_iv.h0 >> 32; - a.hash[0] ^= digest_len; - a.hash[0] ^= key_len << 8; - a.hash[0] ^= 0x01010000; - a.hash[1] = sha512_iv.h1 >> 32; - a.hash[2] = sha512_iv.h2 >> 32; - a.hash[3] = sha512_iv.h3 >> 32; - a.hash[4] = sha512_iv.h4 >> 32; - a.hash[5] = sha512_iv.h5 >> 32; - a.hash[6] = sha512_iv.h6 >> 32; - a.hash[7] = sha512_iv.h7 >> 32; - if(key_len != 0){ - a.compressed = 64; //TODO: allow for keys larger than 64 chars - blake2s_round(&a, 0); - memset(a.buffer, 0, bs); - a.bufflen = 0; - } - return a; + a.bufflen = 0; + } + return a; } void blake2s_update(uint8_t* input, size_t len, struct blake2s_hash* hash){ - hash->total += len; - size_t total_add = len + hash->bufflen; - size_t read = 0; - if(total_add < bs){ - memcpy(hash->buffer + hash->bufflen, input, len); - hash->bufflen += len; - return; - } + hash->total += len; + size_t total_add = len + hash->bufflen; + size_t read = 0; + if(total_add < bs){ + memcpy(hash->buffer + hash->bufflen, input, len); + hash->bufflen += len; + return; + } - for(; total_add >= bs;){ - memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen); - total_add -= bs; - hash->bufflen = 0; - read += bs; - hash->compressed += 64; - blake2s_round(hash, 0); - } + for(; total_add >= bs;){ + memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen); + total_add -= bs; + hash->bufflen = 0; + read += bs; + hash->compressed += 64; + blake2s_round(hash, 0); + } - memset(hash->buffer, 0, bs); + memset(hash->buffer, 0, bs); - if(0 != total_add){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } + if(0 != total_add){ + memcpy(hash->buffer, input + read, total_add); + hash->bufflen = total_add; + } } void blake2s_final(struct blake2s_hash* hash, char* out_stream){ @@ -364,7 +364,7 @@ void blake2s_final(struct blake2s_hash* hash, char* out_stream){ memcpy(old, hash->buffer, bs); memcpy(hashh, hash->hash, 8 * sizeof * hashh); - hash->compressed += hash->bufflen; + hash->compressed += hash->bufflen; if(hash->bufflen > 55) { //too large, needs another buffer @@ -377,19 +377,19 @@ void blake2s_final(struct blake2s_hash* hash, char* out_stream){ blake2s_round(hash, 1); for(int i = 0; i != hash->digest_len; i++)sprintf(out_stream + i * 2, "%02x", (((uint8_t*)hash->hash)[i])); - + memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); memcpy(hash->hash, hashh, 8 * sizeof * hashh); } void blake2s(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* out){ - struct blake2s_hash aa = blake2s_init(key, key_len, dig_len); - blake2s_update(inp, len, &aa); - blake2s_final(&aa, out); - free(aa.buffer); - free(aa.hash); - free(aa.key); + struct blake2s_hash aa = blake2s_init(key, key_len, dig_len); + blake2s_update(inp, len, &aa); + blake2s_final(&aa, out); + free(aa.buffer); + free(aa.hash); + free(aa.key); } int l_blake2b_clone(lua_State* L){ @@ -404,7 +404,7 @@ int l_blake2b_clone(lua_State* L){ memcpy(b->hash, a->hash, 8 * sizeof * b->hash); memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); - + b->keylen = a->keylen; b->digest_len = a->digest_len; b->keylen = a->keylen; @@ -422,8 +422,8 @@ int l_blake2b_init(lua_State* L){ size_t keylen = 0, outlen = 64; if(lua_gettop(L) > 1){ - key = (char*)luaL_checklstring(L, -1, &keylen); - outlen = luaL_checkinteger(L, -2); + key = (char*)luaL_checklstring(L, -1, &keylen); + outlen = luaL_checkinteger(L, -2); } else if(lua_gettop(L) > 0) outlen = luaL_checkinteger(L, -1); struct blake2b_hash* a = (struct blake2b_hash*)lua_newuserdata(L, sizeof * a); @@ -448,25 +448,25 @@ int l_blake2b_final(lua_State* L){ } int l_blake2b(lua_State* L){ - if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2b_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); + if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2b_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + int argv = lua_gettop(L); + + uint32_t out_len = 64; + if(argv > 1) out_len = luaL_checkinteger(L, 2); - uint32_t out_len = 64; - if(argv > 1) out_len = luaL_checkinteger(L, 2); + char* key = NULL; + size_t key_len = 0; + if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len); - char* key = NULL; - size_t key_len = 0; - if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len); - - char digest[out_len * 8]; - memset(digest, 0, out_len * 8); + char digest[out_len * 8]; + memset(digest, 0, out_len * 8); - blake2b(a, len, key, key_len, out_len, digest); - lua_pushstring(L, digest); + blake2b(a, len, key, key_len, out_len, digest); + lua_pushstring(L, digest); - return 1; + return 1; } int l_blake2s_clone(lua_State* L){ @@ -481,7 +481,7 @@ int l_blake2s_clone(lua_State* L){ memcpy(b->hash, a->hash, 8 * sizeof * b->hash); memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); - + b->keylen = a->keylen; b->digest_len = a->digest_len; b->keylen = a->keylen; @@ -499,8 +499,8 @@ int l_blake2s_init(lua_State* L){ size_t keylen = 0, outlen = 32; if(lua_gettop(L) > 1){ - key = (char*)luaL_checklstring(L, -1, &keylen); - outlen = luaL_checkinteger(L, -2); + key = (char*)luaL_checklstring(L, -1, &keylen); + outlen = luaL_checkinteger(L, -2); } else if(lua_gettop(L) > 0) outlen = luaL_checkinteger(L, -1); struct blake2s_hash* a = (struct blake2s_hash*)lua_newuserdata(L, sizeof * a); @@ -525,23 +525,23 @@ int l_blake2s_final(lua_State* L){ } int l_blake2s(lua_State* L){ - if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2s_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); + if(lua_gettop(L) == 0 || lua_type(L, 1) == LUA_TNUMBER) return l_blake2s_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + int argv = lua_gettop(L); - uint32_t out_len = 32; - if(argv > 1) out_len = luaL_checkinteger(L, 2); + uint32_t out_len = 32; + if(argv > 1) out_len = luaL_checkinteger(L, 2); - char* key = NULL; - size_t key_len = 0; - if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len); - - char digest[out_len * 8]; - memset(digest, 0, out_len * 8); + char* key = NULL; + size_t key_len = 0; + if(argv > 2) key = (char*)luaL_checklstring(L, 3, &key_len); - blake2s(a, len, key, key_len, out_len, digest); - lua_pushstring(L, digest); + char digest[out_len * 8]; + memset(digest, 0, out_len * 8); - return 1; + blake2s(a, len, key, key_len, out_len, digest); + lua_pushstring(L, digest); + + return 1; } diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c index 18bae0c..4fb0b90 100644 --- a/src/hash/bsdchecksum.c +++ b/src/hash/bsdchecksum.c @@ -12,10 +12,10 @@ int bsdchecksum_free_l(lua_State* L){ void bsdchecksum_update(uint8_t* aa, size_t len, struct bsdchecksum_hash* hash){ for(int i = 0; i != len; i++){ - uint8_t a = aa[i]; - hash->check = (hash->check >> 1) + ((hash->check & 1) << 15); - hash->check += a; - hash->check &= 0xffff; + uint8_t a = aa[i]; + hash->check = (hash->check >> 1) + ((hash->check & 1) << 15); + hash->check += a; + hash->check &= 0xffff; } } @@ -46,7 +46,7 @@ int l_bsdchecksum(lua_State* L){ if(lua_gettop(L) == 0) return l_bsdchecksum_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[16]; //uint16_t u = i_bsdchecksum(a, len); diff --git a/src/hash/buzhash.c b/src/hash/buzhash.c index 4016110..bb35f73 100644 --- a/src/hash/buzhash.c +++ b/src/hash/buzhash.c @@ -20,28 +20,28 @@ static uint8_t T[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, 252,253,254,255}; uint8_t buzhash8(uint8_t* in, size_t len){ - uint8_t hash = 0; + uint8_t hash = 0; - for(int i = 0; i != len; i++){ - hash ^= rotl8(T[(uint8_t)in[i]],len - (i + 1)); - } + for(int i = 0; i != len; i++){ + hash ^= rotl8(T[(uint8_t)in[i]],len - (i + 1)); + } - return hash; + return hash; } uint16_t buzhash16(uint8_t* in, size_t len){ - uint16_t hash = 0; + uint16_t hash = 0; - for(int i = 0; i != len; i++){ - hash ^= rotl16(T[(uint8_t)in[i]],len - (i + 1)); - } + for(int i = 0; i != len; i++){ + hash ^= rotl16(T[(uint8_t)in[i]],len - (i + 1)); + } - return hash; + return hash; } int l_setbuzhash(lua_State* L){ luaL_checktype(L, 1, LUA_TTABLE); size_t len = lua_objlen(L,1); - + if(len != 256) { p_error("new table must have a length of 256"); exit(0); @@ -51,7 +51,7 @@ int l_setbuzhash(lua_State* L){ lua_pushinteger(L,i+1); lua_gettable(L,1); - + T[i] = luaL_checknumber(L, -1); lua_pop(L,1); } @@ -64,7 +64,7 @@ int l_buzhash8(lua_State* L){ char digest[3]; uint8_t u = buzhash8(a, len); - + sprintf(digest,"%x",u); lua_pushstring(L, digest); @@ -78,7 +78,7 @@ int l_buzhash16(lua_State* L){ char digest[6]; uint16_t u = buzhash16(a, len); - + sprintf(digest,"%04x",u); lua_pushstring(L, digest); diff --git a/src/hash/crc.c b/src/hash/crc.c index e1433bd..c3be7f3 100644 --- a/src/hash/crc.c +++ b/src/hash/crc.c @@ -9,81 +9,81 @@ int crc16_free_l(lua_State* L){return 0;} int crc8_free_l(lua_State* L){return 0;} struct crc32_hash crc32_init(){ - return (struct crc32_hash){.crc = 0xFFFFFFFF}; + return (struct crc32_hash){.crc = 0xFFFFFFFF}; } void crc32_update(uint8_t* data, size_t len, struct crc32_hash* hash){ - for(int i = 0; i < len; i++){ - uint32_t extract = data[i]; - for(int z = 0; z < 8; z++){ - uint32_t b = (extract^hash->crc)&1; - hash->crc>>=1; - if(b) hash->crc^=0xEDB88320; - extract>>=1; - } + for(int i = 0; i < len; i++){ + uint32_t extract = data[i]; + for(int z = 0; z < 8; z++){ + uint32_t b = (extract^hash->crc)&1; + hash->crc>>=1; + if(b) hash->crc^=0xEDB88320; + extract>>=1; } + } } uint32_t crc32_final(struct crc32_hash* hash){ - return -(hash->crc+1); + return -(hash->crc+1); } uint32_t crc32(uint8_t* data, size_t len){ - struct crc32_hash a = crc32_init(); - crc32_update(data, len, &a); - return crc32_final(&a); + struct crc32_hash a = crc32_init(); + crc32_update(data, len, &a); + return crc32_final(&a); } struct crc16_hash crc16_init(){ - return (struct crc16_hash){.crc = 0x0}; + return (struct crc16_hash){.crc = 0x0}; } void crc16_update(uint8_t *aa, size_t len, struct crc16_hash *hash){ - for(int i = 0; i != len; i++){ - uint8_t a = aa[i]; - hash->crc ^= a; - for (int z = 0; z < 8; z++){ - if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001; - else hash->crc = (hash->crc >> 1); - } + for(int i = 0; i != len; i++){ + uint8_t a = aa[i]; + hash->crc ^= a; + for (int z = 0; z < 8; z++){ + if (hash->crc & 1) hash->crc = (hash->crc >> 1) ^ 0xA001; + else hash->crc = (hash->crc >> 1); } + } } uint16_t crc16_final(struct crc16_hash *hash){ - return hash->crc; + return hash->crc; } uint16_t crc16(uint8_t *aa, size_t len){ - struct crc16_hash a = crc16_init(); - crc16_update(aa, len, &a); - return crc16_final(&a); + struct crc16_hash a = crc16_init(); + crc16_update(aa, len, &a); + return crc16_final(&a); } struct crc8_hash crc8_init(){ - return (struct crc8_hash){.crc = 0x00}; + return (struct crc8_hash){.crc = 0x00}; } void crc8_update(uint8_t *aa, size_t len, struct crc8_hash *hash){ - for(int i = 0; i != len; i++){ - uint8_t a = aa[i]; - - for (int z = 0; z < 8; z++){ - uint8_t b = (hash->crc ^ a) & 1; - hash->crc >>= 1; - if(b) hash->crc ^= 0x8c; - a >>=1; - } + for(int i = 0; i != len; i++){ + uint8_t a = aa[i]; + + for (int z = 0; z < 8; z++){ + uint8_t b = (hash->crc ^ a) & 1; + hash->crc >>= 1; + if(b) hash->crc ^= 0x8c; + a >>=1; } + } } uint8_t crc8_final(struct crc8_hash *hash){ - return hash->crc; + return hash->crc; } uint8_t crc8(uint8_t *aa, size_t len){ - struct crc8_hash a = crc8_init(); - crc8_update(aa, len, &a); - return crc8_final(&a); + struct crc8_hash a = crc8_init(); + crc8_update(aa, len, &a); + return crc8_final(&a); } common_hash_clone(crc32); @@ -125,7 +125,7 @@ int l_crc8(lua_State* L){ if(lua_gettop(L) == 0) return l_crc8_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[8]; uint8_t u = crc8(a, len); @@ -139,7 +139,7 @@ int l_crc16(lua_State* L){ if(lua_gettop(L) == 0) return l_crc16_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[16]; uint16_t u = crc16(a, len); @@ -153,7 +153,7 @@ int l_crc32(lua_State* L){ if(lua_gettop(L) == 0) return l_crc32_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[32]; uint32_t u = crc32(a, len); diff --git a/src/hash/crc.h b/src/hash/crc.h index b3fdbf9..233debd 100644 --- a/src/hash/crc.h +++ b/src/hash/crc.h @@ -2,15 +2,15 @@ #include struct crc32_hash { - uint32_t crc; + uint32_t crc; }; struct crc16_hash { - uint16_t crc; + uint16_t crc; }; struct crc8_hash { - uint8_t crc; + uint8_t crc; }; uint8_t crc8(uint8_t*, size_t len); diff --git a/src/hash/djb2.c b/src/hash/djb2.c index e75a0ed..a001e8f 100644 --- a/src/hash/djb2.c +++ b/src/hash/djb2.c @@ -45,7 +45,7 @@ int l_djb2(lua_State* L){ if(lua_gettop(L) == 0) return l_djb2_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[64]; uint32_t u = djb2(a, len); diff --git a/src/hash/djb2.h b/src/hash/djb2.h index d944d11..5c6fe19 100644 --- a/src/hash/djb2.h +++ b/src/hash/djb2.h @@ -2,7 +2,7 @@ #include struct djb2_hash { - uint32_t hash; + uint32_t hash; }; struct djb2_hash djb2_init(); diff --git a/src/hash/fletcher.c b/src/hash/fletcher.c index bfd3e7c..930d07b 100644 --- a/src/hash/fletcher.c +++ b/src/hash/fletcher.c @@ -12,8 +12,8 @@ struct fletcher8_hash fletcher8_init(){ void fletcher8_update(uint8_t *aa, size_t len, struct fletcher8_hash *hash){ for(int i = 0; i != len; i++){ - hash->s1 = (hash->s1 + aa[i]) % 15; - hash->s2 = (hash->s2 + hash->s1) % 15; + hash->s1 = (hash->s1 + aa[i]) % 15; + hash->s2 = (hash->s2 + hash->s1) % 15; } } @@ -33,8 +33,8 @@ struct fletcher16_hash fletcher16_init(){ void fletcher16_update(uint8_t *aa, size_t len, struct fletcher16_hash *hash){ for(int i = 0; i != len; i++){ - hash->s1 = (hash->s1 + aa[i]) % 255; - hash->s2 = (hash->s2 + hash->s1) % 255; + hash->s1 = (hash->s1 + aa[i]) % 255; + hash->s2 = (hash->s2 + hash->s1) % 255; } } @@ -54,8 +54,8 @@ struct fletcher32_hash fletcher32_init(){ void fletcher32_update(uint8_t *aa, size_t len, struct fletcher32_hash *hash){ for(int i = 0; i != len; i++){ - hash->s1 = (hash->s1 + aa[i]) % 65535; - hash->s2 = (hash->s2 + hash->s1) % 65535; + hash->s1 = (hash->s1 + aa[i]) % 65535; + hash->s2 = (hash->s2 + hash->s1) % 65535; } } @@ -108,7 +108,7 @@ int l_fletcher32(lua_State* L){ if(lua_gettop(L) == 0) return l_fletcher32_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[32]; uint32_t u = fletcher32(a, len); @@ -122,7 +122,7 @@ int l_fletcher16(lua_State* L){ if(lua_gettop(L) == 0) return l_fletcher16_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[16]; uint16_t u = fletcher16(a, len); @@ -136,7 +136,7 @@ int l_fletcher8(lua_State* L){ if(lua_gettop(L) == 0) return l_fletcher8_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[8]; uint8_t u = fletcher8(a, len); diff --git a/src/hash/fletcher.h b/src/hash/fletcher.h index d66692f..cae169f 100644 --- a/src/hash/fletcher.h +++ b/src/hash/fletcher.h @@ -2,15 +2,15 @@ #include struct fletcher8_hash { - uint8_t s1, s2; + uint8_t s1, s2; }; struct fletcher16_hash { - uint16_t s1, s2; + uint16_t s1, s2; }; struct fletcher32_hash { - uint32_t s1, s2; + uint32_t s1, s2; }; uint8_t fletcher8(uint8_t*,size_t); @@ -41,4 +41,4 @@ int l_fletcher16_final(lua_State*); int l_fletcher8(lua_State*); int l_fletcher8_init(lua_State*); int l_fletcher8_update(lua_State*); -int l_fletcher8_final(lua_State*); \ No newline at end of file +int l_fletcher8_final(lua_State*); diff --git a/src/hash/fnv.c b/src/hash/fnv.c index 34623b7..19fc0a5 100644 --- a/src/hash/fnv.c +++ b/src/hash/fnv.c @@ -51,14 +51,14 @@ lua_common_hash_init_ni(fnv_1, fnv_0, fnv_1_init(v_0), fnv_1_free_l); lua_common_hash_init_ni(fnv_1, fnv_a, fnv_1_init(v_a), fnv_1_free_l); #define aaa(v)\ -int l_fnv_##v##_final(lua_State* L){\ - struct fnv_1_hash* a = (struct fnv_1_hash*)lua_touserdata(L, 1);\ - uint64_t u = fnv_1_final(a);\ - char digest[64];\ - sprintf(digest,"%16"PRIx64,u);\ - lua_pushstring(L, digest);\ - return 1;\ -} + int l_fnv_##v##_final(lua_State* L){\ + struct fnv_1_hash* a = (struct fnv_1_hash*)lua_touserdata(L, 1);\ + uint64_t u = fnv_1_final(a);\ + char digest[64];\ + sprintf(digest,"%16"PRIx64,u);\ + lua_pushstring(L, digest);\ + return 1;\ + } aaa(0); aaa(1); diff --git a/src/hash/fnv.h b/src/hash/fnv.h index a4299c9..5d4491a 100644 --- a/src/hash/fnv.h +++ b/src/hash/fnv.h @@ -2,7 +2,7 @@ #include "stdint.h" enum fnv_version { - v_1, v_a, v_0 + v_1, v_a, v_0 }; struct fnv_1_hash { diff --git a/src/hash/md5.c b/src/hash/md5.c index f98e590..d1d78fd 100644 --- a/src/hash/md5.c +++ b/src/hash/md5.c @@ -4,20 +4,20 @@ #include static const uint32_t K[] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, - 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, - 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, - 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, - 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, - 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, - 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, - 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, - 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, - 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; + 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, + 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, + 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, + 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, + 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, + 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, + 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, + 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, + 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}; static const uint32_t s[] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, - 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, - 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, - 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; + 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, + 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, + 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21}; #define bs 64 @@ -49,7 +49,7 @@ void md5_round(struct md5_hash* hash){ for(int i = 0; i < 64; i++){ uint32_t F, g; - + if(i < 16){ F = (B & C) | ((~B) & D); g = i; @@ -63,7 +63,7 @@ void md5_round(struct md5_hash* hash){ F = C ^ (B | (~D)); g = (7*i) % 16; } - + F = F + A + K[i] + M[g]; @@ -93,7 +93,7 @@ void md5_update(uint8_t* input, size_t len, struct md5_hash* hash){ for(; total_add >= bs;){ memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen); - + total_add -= bs; read += bs; hash->bufflen = 0; @@ -125,7 +125,7 @@ void md5_final(struct md5_hash* hash, char out_stream[64]){ uint32_t lhhh = 8*hash->total; memcpy(hash->buffer + 56, &lhhh, 4); - + md5_round(hash); sprintf(out_stream,"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", @@ -145,7 +145,7 @@ lua_common_hash_clone_oargs(md5, md5, l_md5_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_ni(md5, md5, md5_init_l(L), l_md5_free); lua_common_hash_update(md5, md5); diff --git a/src/hash/murmur.c b/src/hash/murmur.c index d191d75..c7ad143 100644 --- a/src/hash/murmur.c +++ b/src/hash/murmur.c @@ -3,70 +3,70 @@ #include uint32_t murmur1_32(uint8_t* in, size_t len, uint32_t seed){ - uint32_t m = 0xc6a4a793; - uint32_t hash = seed ^ (len * m); - - for(;len >= 4; len-=4){ - hash+=*(uint32_t*)in; - hash*=m; - hash^=hash >> 16; - - in+=4; - } - - switch(len){ - case 3: - hash+=in[2]<<16; - case 2: - hash+=in[1]<<8; - case 1: - hash+=in[0]; - hash*=m; - hash^=hash>>16; - break; - } + uint32_t m = 0xc6a4a793; + uint32_t hash = seed ^ (len * m); + for(;len >= 4; len-=4){ + hash+=*(uint32_t*)in; hash*=m; - hash^=hash>>10; - hash*=m; - hash^=hash>>17; - - return hash; + hash^=hash >> 16; + + in+=4; + } + + switch(len){ + case 3: + hash+=in[2]<<16; + case 2: + hash+=in[1]<<8; + case 1: + hash+=in[0]; + hash*=m; + hash^=hash>>16; + break; + } + + hash*=m; + hash^=hash>>10; + hash*=m; + hash^=hash>>17; + + return hash; } uint32_t murmur2_32(uint8_t* in, size_t len, uint32_t seed){ - uint32_t m = 0x5bd1e995; - uint32_t hash = seed ^ len; - - for(;len >= 4; len-=4){ - uint32_t k = *(uint32_t*)in; - - k*=m; - k^=k>>24; - k*=m; - - hash*=m; - hash^=k; - - in+=4; - } - - switch(len){ - case 3: - hash+=in[2]<<16; - case 2: - hash+=in[1]<<8; - case 1: - hash+=in[0]; - hash*=m; - break; - } - - hash^=hash>>13; - hash*=m; - hash^=hash>>15; + uint32_t m = 0x5bd1e995; + uint32_t hash = seed ^ len; - return hash; + for(;len >= 4; len-=4){ + uint32_t k = *(uint32_t*)in; + + k*=m; + k^=k>>24; + k*=m; + + hash*=m; + hash^=k; + + in+=4; + } + + switch(len){ + case 3: + hash+=in[2]<<16; + case 2: + hash+=in[1]<<8; + case 1: + hash+=in[0]; + hash*=m; + break; + } + + hash^=hash>>13; + hash*=m; + hash^=hash>>15; + + return hash; } int l_murmur1_32(lua_State* L){ diff --git a/src/hash/pearson.c b/src/hash/pearson.c index d6a5d1b..74546a4 100644 --- a/src/hash/pearson.c +++ b/src/hash/pearson.c @@ -45,7 +45,7 @@ uint8_t pearson(uint8_t* aa, size_t len){ int l_setpearson(lua_State* L){ luaL_checktype(L, 1, LUA_TTABLE); size_t len = lua_objlen(L,1); - + if(len != 256) { p_error("new table must have a length of 256"); exit(0); @@ -55,7 +55,7 @@ int l_setpearson(lua_State* L){ lua_pushinteger(L,i+1); lua_gettable(L,1); - + pearson_table[i] = luaL_checknumber(L, -1); lua_pop(L,1); } @@ -81,7 +81,7 @@ int l_pearson(lua_State* L){ char digest[8]; uint8_t u = pearson(a, len); - + sprintf(digest,"%x",u); lua_pushstring(L, digest); diff --git a/src/hash/pearson.h b/src/hash/pearson.h index 4cdfda9..c10fea4 100644 --- a/src/hash/pearson.h +++ b/src/hash/pearson.h @@ -2,7 +2,7 @@ #include struct pearson_hash { - uint8_t ret; + uint8_t ret; }; struct pearson_hash pearson_init(); diff --git a/src/hash/pjw.c b/src/hash/pjw.c index 188030f..93fc7d4 100644 --- a/src/hash/pjw.c +++ b/src/hash/pjw.c @@ -4,7 +4,7 @@ #include struct pjw_hash pjw_init(){ - return (struct pjw_hash){.hash = 0, .high = 0}; + return (struct pjw_hash){.hash = 0, .high = 0}; } int pjw_free_l(lua_State* L){ @@ -12,22 +12,22 @@ int pjw_free_l(lua_State* L){ } void pjw_update(uint8_t* in, size_t len, struct pjw_hash* hash){ - for(int i = 0; i != len; i++){ - hash->hash = (hash->hash << 4) + *in++; - if((hash->high = (hash->hash & 0xf0000000))) - hash->hash ^= hash->high >> 24; - hash->hash &= ~hash->high; - } + for(int i = 0; i != len; i++){ + hash->hash = (hash->hash << 4) + *in++; + if((hash->high = (hash->hash & 0xf0000000))) + hash->hash ^= hash->high >> 24; + hash->hash &= ~hash->high; + } } uint32_t pjw_final(struct pjw_hash* hash){ - return hash->hash; + return hash->hash; } uint32_t pjw(uint8_t* in, size_t len){ - struct pjw_hash a = pjw_init(); - pjw_update(in, len, &a); - return pjw_final(&a); + struct pjw_hash a = pjw_init(); + pjw_update(in, len, &a); + return pjw_final(&a); } common_hash_clone(pjw); @@ -43,14 +43,14 @@ int l_pjw_final(lua_State* L){ } int l_pjw(lua_State* L){ - if(lua_gettop(L) == 0) return l_pjw_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + if(lua_gettop(L) == 0) return l_pjw_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[32]; + char digest[32]; - uint32_t u = pjw(a, len); - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; + uint32_t u = pjw(a, len); + sprintf(digest,"%08x",u); + lua_pushstring(L, digest); + return 1; } diff --git a/src/hash/pjw.h b/src/hash/pjw.h index a4e36e7..4ac45ce 100644 --- a/src/hash/pjw.h +++ b/src/hash/pjw.h @@ -2,7 +2,7 @@ #include struct pjw_hash { - uint32_t hash, high; + uint32_t hash, high; }; struct pjw_hash pjw_init(); diff --git a/src/hash/sdbm.h b/src/hash/sdbm.h index a937c92..8d79511 100644 --- a/src/hash/sdbm.h +++ b/src/hash/sdbm.h @@ -2,7 +2,7 @@ #include struct sdbm_hash { - uint64_t hash; + uint64_t hash; }; struct sdbm_hash sdbm_init(); diff --git a/src/hash/sha01.c b/src/hash/sha01.c index 4d05e7e..aa403a2 100644 --- a/src/hash/sha01.c +++ b/src/hash/sha01.c @@ -17,10 +17,10 @@ struct sha01_hash { #define sha1_hash sha01_hash struct sha01_hash sha01_init(uint8_t ver){ - struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0, - .total = 0, .bufflen = 0, .version = ver}; - a.buffer = calloc(sizeof * a.buffer, bs); - return a; + struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0, + .total = 0, .bufflen = 0, .version = ver}; + a.buffer = calloc(sizeof * a.buffer, bs); + return a; } int sha01_free_l(lua_State* L){ @@ -30,64 +30,64 @@ int sha01_free_l(lua_State* L){ } struct sha01_hash sha01_init_l(uint8_t ver, lua_State* L){ - struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0, - .total = 0, .bufflen = 0, .version = ver}; - a.buffer = calloc(sizeof * a.buffer, bs); - memset(a.buffer, 0, bs); - return a; + struct sha01_hash a = {.h0 = 0x67452301, .h1 = 0xEFCDAB89, .h2 = 0x98BADCFE, .h3 = 0x10325476, .h4 = 0xC3D2E1F0, + .total = 0, .bufflen = 0, .version = ver}; + a.buffer = calloc(sizeof * a.buffer, bs); + memset(a.buffer, 0, bs); + return a; } void sha01_round(struct sha01_hash* hash){ - int hat = 0; - uint32_t W[80] = {0}; - - for(int i = 0; i != 16; i++){ - int t = 24; - for(;t>=0;){ - W[i] += (((uint32_t)hash->buffer[hat]) << t); - hat++; - t-=8; - } + int hat = 0; + uint32_t W[80] = {0}; + + for(int i = 0; i != 16; i++){ + int t = 24; + for(;t>=0;){ + W[i] += (((uint32_t)hash->buffer[hat]) << t); + hat++; + t-=8; } - for(int i = 16; i != 80; i++) - W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], hash->version); - - uint32_t a = hash->h0; - uint32_t b = hash->h1; - uint32_t c = hash->h2; - uint32_t d = hash->h3; - uint32_t e = hash->h4; - - for(int i = 0; i != 80; i++){ - - uint32_t f,k; - if(0 <= i && i <= 19){ - f = (b & c) | ((~b) & d); - k = 0x5A827999; - } else if(20 <= i && i <= 39){ - f = b ^ c ^ d; - k = 0x6ED9EBA1; - } else if(40 <= i && i <= 59){ - f = (b & c) | (b & d) | (c & d); - k = 0x8F1BBCDC; - } else { - f = b ^ c ^ d; - k = 0xCA62C1D6; - } - - uint32_t temp = rotl32(a, 5) + f + e + k + W[i]; - e = d; - d = c; - c = rotl32(b, 30); - b = a; - a = temp; + } + for(int i = 16; i != 80; i++) + W[i] = rotl32(W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16], hash->version); + + uint32_t a = hash->h0; + uint32_t b = hash->h1; + uint32_t c = hash->h2; + uint32_t d = hash->h3; + uint32_t e = hash->h4; + + for(int i = 0; i != 80; i++){ + + uint32_t f,k; + if(0 <= i && i <= 19){ + f = (b & c) | ((~b) & d); + k = 0x5A827999; + } else if(20 <= i && i <= 39){ + f = b ^ c ^ d; + k = 0x6ED9EBA1; + } else if(40 <= i && i <= 59){ + f = (b & c) | (b & d) | (c & d); + k = 0x8F1BBCDC; + } else { + f = b ^ c ^ d; + k = 0xCA62C1D6; } - - hash->h0 += a; - hash->h1 += b; - hash->h2 += c; - hash->h3 += d; - hash->h4 += e; + + uint32_t temp = rotl32(a, 5) + f + e + k + W[i]; + e = d; + d = c; + c = rotl32(b, 30); + b = a; + a = temp; + } + + hash->h0 += a; + hash->h1 += b; + hash->h2 += c; + hash->h3 += d; + hash->h4 += e; } void sha01_update(uint8_t* input, size_t len, struct sha01_hash* hash){ @@ -133,7 +133,7 @@ void sha01_final(struct sha01_hash* hash, char* out_stream){ size_t lhhh = 8*hash->total; for(int i = 0; i != 8; i++) - hash->buffer[63 - i] = (uint8_t) (lhhh >> (i * 8) & 0xFF); + hash->buffer[63 - i] = (uint8_t) (lhhh >> (i * 8) & 0xFF); sha01_round(hash); sprintf(out_stream,"%02x%02x%02x%02x%02x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4); @@ -143,41 +143,41 @@ void sha01_final(struct sha01_hash* hash, char* out_stream){ } struct sha01_hash sha0_init(){ - return sha01_init(0); + return sha01_init(0); } struct sha01_hash sha1_init(){ - return sha01_init(1); + return sha01_init(1); } void sha0_update(uint8_t* input, size_t len, struct sha01_hash* hash){ - sha01_update(input, len, hash); + sha01_update(input, len, hash); } void sha1_update(uint8_t* input, size_t len, struct sha01_hash* hash){ - sha01_update(input, len, hash); + sha01_update(input, len, hash); } void sha0_final(struct sha01_hash* hash, char* out_stream){ - sha01_final(hash, out_stream); + sha01_final(hash, out_stream); } void sha1_final(struct sha01_hash* hash, char* out_stream){ - sha01_final(hash, out_stream); + sha01_final(hash, out_stream); } void sha0(uint8_t* a, size_t len, char* out_stream){ - struct sha01_hash aa = sha0_init(); - sha0_update(a, len, &aa); - sha0_final(&aa, out_stream); - free(aa.buffer); + struct sha01_hash aa = sha0_init(); + sha0_update(a, len, &aa); + sha0_final(&aa, out_stream); + free(aa.buffer); } void sha1(uint8_t* a, size_t len, char* out_stream){ - struct sha01_hash aa = sha1_init(); - sha1_update(a, len, &aa); - sha1_final(&aa, out_stream); - free(aa.buffer); + struct sha01_hash aa = sha1_init(); + sha1_update(a, len, &aa); + sha1_final(&aa, out_stream); + free(aa.buffer); } //common_hash_clone(sha1); @@ -186,7 +186,7 @@ lua_common_hash_clone_oargs(sha1, sha1, l_sha1_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_ni(sha1, sha1, sha01_init_l(1, L), sha01_free_l); lua_common_hash_update(sha1, sha1); @@ -197,7 +197,7 @@ lua_common_hash_clone_oargs(sha0, sha0, l_sha0_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_ni(sha0, sha0, sha01_init_l(0, L), sha01_free_l); lua_common_hash_update(sha0, sha0); @@ -212,7 +212,7 @@ int l_sha1_final(lua_State* L){ } int l_sha0_final(lua_State* L){ - return l_sha1_final(L); + return l_sha1_final(L); } int l_sha1(lua_State* L){ @@ -232,7 +232,7 @@ int l_sha0(lua_State* L){ if(lua_gettop(L) == 0) return l_sha0_init(L); size_t len = 0; char* a = (char*)luaL_checklstring(L, 1, &len); - + char digest[160]; sha0((uint8_t*)a, len, digest); diff --git a/src/hash/sha01.h b/src/hash/sha01.h index f70a3e0..bb343c2 100644 --- a/src/hash/sha01.h +++ b/src/hash/sha01.h @@ -8,7 +8,7 @@ * @param {char*} output stream * @param {const char*} input bytes * @return {void} -*/ + */ void i_sha01(uint8_t, char*, int, const char*); int l_sha1(lua_State*); diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 3c33948..06841c5 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -6,106 +6,106 @@ #include const uint64_t k[80] = {0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538, - 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe, - 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, - 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, - 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab, - 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725, - 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, - 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, - 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218, - 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, - 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, - 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, - 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c, - 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6, - 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, - 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817}; + 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe, + 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, + 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, + 0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab, + 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725, + 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, + 0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b, + 0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218, + 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, + 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, + 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec, + 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c, + 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6, + 0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, + 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817}; void endian_swap128(__uint128_t *x){ - uint8_t *y = (uint8_t*)x; - for (size_t low = 0, high = sizeof(__uint128_t) - 1; high > low; low++, high--){ - y[low] ^= y[high]; - y[high] ^= y[low]; - y[low] ^= y[high]; - } + uint8_t *y = (uint8_t*)x; + for (size_t low = 0, high = sizeof(__uint128_t) - 1; high > low; low++, high--){ + y[low] ^= y[high]; + y[high] ^= y[low]; + y[low] ^= y[high]; + } } void endian_swap64(uint64_t *x){ - uint8_t *y = (uint8_t*)x; - for (size_t low = 0, high = sizeof(uint64_t) - 1; high > low; low++, high--){ - y[low] ^= y[high]; - y[high] ^= y[low]; - y[low] ^= y[high]; - } + uint8_t *y = (uint8_t*)x; + for (size_t low = 0, high = sizeof(uint64_t) - 1; high > low; low++, high--){ + y[low] ^= y[high]; + y[high] ^= y[low]; + y[low] ^= y[high]; + } } #define bs 128 void sha512_round(struct sha512_hash* hash){ - uint64_t *msg = ((uint64_t*)&hash->buffer[0]); - for(int i = 0; i < 16; i++) - endian_swap64(msg++); - - uint64_t* M = ((uint64_t*)(hash->buffer)); - uint64_t W[80]; - - //i dont really understand this 0->16 part - int z = 0; - uint64_t *m = &M[(z * 16)]; - for(int i = 0; i < 16; ++i){ - W[i] = *m; - m++; - } - - for(int i = 16; i != 80; i++){ - W[i] = (rotr64(W[i - 2],19) ^ rotr64(W[i - 2], 61) ^ (W[i - 2] >> 6)) - + W[i - 7] + (rotr64(W[i - 15],1) ^ rotr64(W[i - 15],8) ^ (W[i - 15] >> 7)) + W[i - 16]; - } - - uint64_t a = hash->h0; - uint64_t b = hash->h1; - uint64_t c = hash->h2; - uint64_t d = hash->h3; - uint64_t e = hash->h4; - uint64_t f = hash->h5; - uint64_t g = hash->h6; - uint64_t h = hash->h7; - - for(int i = 0; i != 80; i++){ - uint64_t S1 = rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41); - uint64_t ch = (e & f) ^ ((~e) & g); - uint64_t temp1 = h + S1 + ch + k[i] + W[i]; - - uint64_t S0 = rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39); - uint64_t maj = (a & b) ^ (a & c) ^ (b & c); - uint64_t temp2 = S0 + maj; - - h = g; - g = f; - f = e; - e = d + temp1; - d = c; - c = b; - b = a; - a = temp1 + temp2; - } - - hash->h0 += a; - hash->h1 += b; - hash->h2 += c; - hash->h3 += d; - hash->h4 += e; - hash->h5 += f; - hash->h6 += g; - hash->h7 += h; + uint64_t *msg = ((uint64_t*)&hash->buffer[0]); + for(int i = 0; i < 16; i++) + endian_swap64(msg++); + + uint64_t* M = ((uint64_t*)(hash->buffer)); + uint64_t W[80]; + + //i dont really understand this 0->16 part + int z = 0; + uint64_t *m = &M[(z * 16)]; + for(int i = 0; i < 16; ++i){ + W[i] = *m; + m++; + } + + for(int i = 16; i != 80; i++){ + W[i] = (rotr64(W[i - 2],19) ^ rotr64(W[i - 2], 61) ^ (W[i - 2] >> 6)) + + W[i - 7] + (rotr64(W[i - 15],1) ^ rotr64(W[i - 15],8) ^ (W[i - 15] >> 7)) + W[i - 16]; + } + + uint64_t a = hash->h0; + uint64_t b = hash->h1; + uint64_t c = hash->h2; + uint64_t d = hash->h3; + uint64_t e = hash->h4; + uint64_t f = hash->h5; + uint64_t g = hash->h6; + uint64_t h = hash->h7; + + for(int i = 0; i != 80; i++){ + uint64_t S1 = rotr64(e, 14) ^ rotr64(e, 18) ^ rotr64(e, 41); + uint64_t ch = (e & f) ^ ((~e) & g); + uint64_t temp1 = h + S1 + ch + k[i] + W[i]; + + uint64_t S0 = rotr64(a, 28) ^ rotr64(a, 34) ^ rotr64(a, 39); + uint64_t maj = (a & b) ^ (a & c) ^ (b & c); + uint64_t temp2 = S0 + maj; + + h = g; + g = f; + f = e; + e = d + temp1; + d = c; + c = b; + b = a; + a = temp1 + temp2; + } + + hash->h0 += a; + hash->h1 += b; + hash->h2 += c; + hash->h3 += d; + hash->h4 += e; + hash->h5 += f; + hash->h6 += g; + hash->h7 += h; } struct sha512_hash sha512_t_init(struct iv sha_iv){ - struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7, - .total = 0, .bufflen = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - return a; + struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7, + .total = 0, .bufflen = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + return a; } int sha512_t_free_l(lua_State* L){ @@ -115,19 +115,19 @@ int sha512_t_free_l(lua_State* L){ } struct sha512_hash sha512_t_init_l(struct iv sha_iv, lua_State* L){ - struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7, - .total = 0, .bufflen = 0}; - a.buffer = calloc((sizeof * a.buffer), bs); - memset(a.buffer, 0, bs); - return a; + struct sha512_hash a = {.h0 = sha_iv.h0, .h1 = sha_iv.h1, .h2 = sha_iv.h2, .h3 = sha_iv.h3, .h4 = sha_iv.h4, .h5 = sha_iv.h5, .h6 = sha_iv.h6, .h7 = sha_iv.h7, + .total = 0, .bufflen = 0}; + a.buffer = calloc((sizeof * a.buffer), bs); + memset(a.buffer, 0, bs); + return a; } struct sha512_hash sha512_init(){ - return sha512_t_init(sha512_iv); + return sha512_t_init(sha512_iv); } struct sha512_hash sha384_init(){ - return sha512_t_init(sha384_iv); + return sha512_t_init(sha384_iv); } char old[512]; @@ -167,11 +167,11 @@ void _sha512_t_final(struct sha512_hash* hash){ sha512_round(hash); memset(hash->buffer, 0, bs); } - + __uint128_t bigL = hash->total*8; endian_swap128(&bigL); memcpy(&hash->buffer[128 - sizeof(__uint128_t)], &bigL, sizeof(__uint128_t)); - + sha512_round(hash); } @@ -186,12 +186,12 @@ void sha512_final(struct sha512_hash* hash, char* out_stream){ sprintf((char*)out_stream, "%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64 , hash->h0, hash->h1, hash->h2, hash->h3, hash->h4, hash->h5, hash->h6, hash->h7); /*sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h1); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h6); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h7);*/ + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h6); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h7);*/ memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); @@ -207,49 +207,49 @@ void sha384_final(struct sha512_hash* hash, char* out_stream){ sprintf((char*)out_stream, "%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64"%016"PRIx64, hash->h0, hash->h1, hash->h2, hash->h3, hash->h4, hash->h5); /*sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h1); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4); - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);*/ + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h2); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h3); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h4); + sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h5);*/ memcpy(hash, &old_hash, sizeof * hash); memcpy(hash->buffer, old, bs); } void sha512(uint8_t* in, size_t len, char* out){ - struct sha512_hash a = sha512_init(); - sha512_update(in, len, &a); - sha512_final(&a, out); - free(a.buffer); + struct sha512_hash a = sha512_init(); + sha512_update(in, len, &a); + sha512_final(&a, out); + free(a.buffer); } void sha384(uint8_t* in, size_t len, char* out){ - struct sha512_hash a = sha384_init(); - sha384_update(in, len, &a); - sha384_final(&a, out); - free(a.buffer); + struct sha512_hash a = sha384_init(); + sha384_update(in, len, &a); + sha384_final(&a, out); + free(a.buffer); } void sha512_t(uint8_t* in, size_t len, int t, char* out){ - struct sha512_hash a = sha512_t_init(sha_iv_gen(t)); - sha512_update(in, len, &a); - sha512_final(&a, out); - out[t/4] = '\0'; - free(a.buffer); + struct sha512_hash a = sha512_t_init(sha_iv_gen(t)); + sha512_update(in, len, &a); + sha512_final(&a, out); + out[t/4] = '\0'; + free(a.buffer); } struct iv sha_iv_gen(int i){ - struct iv oh = {.h0 = sha512_iv.h0 ^ 0xa5a5a5a5a5a5a5a5, .h1 = sha512_iv.h1 ^ 0xa5a5a5a5a5a5a5a5, .h2 = sha512_iv.h2 ^ 0xa5a5a5a5a5a5a5a5, - .h3 = sha512_iv.h3 ^ 0xa5a5a5a5a5a5a5a5, .h4 = sha512_iv.h4 ^ 0xa5a5a5a5a5a5a5a5, .h5 = sha512_iv.h5 ^ 0xa5a5a5a5a5a5a5a5, - .h6 = sha512_iv.h6 ^ 0xa5a5a5a5a5a5a5a5, .h7 = sha512_iv.h7 ^ 0xa5a5a5a5a5a5a5a5}; - - uint8_t in[12]; - sprintf((char*)in, "SHA-512/%i", i); - struct sha512_hash a = sha512_t_init(oh); - sha512_update(in, strlen((char*)in), &a); - _sha512_t_final(&a); - free(a.buffer); - return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7}; + struct iv oh = {.h0 = sha512_iv.h0 ^ 0xa5a5a5a5a5a5a5a5, .h1 = sha512_iv.h1 ^ 0xa5a5a5a5a5a5a5a5, .h2 = sha512_iv.h2 ^ 0xa5a5a5a5a5a5a5a5, + .h3 = sha512_iv.h3 ^ 0xa5a5a5a5a5a5a5a5, .h4 = sha512_iv.h4 ^ 0xa5a5a5a5a5a5a5a5, .h5 = sha512_iv.h5 ^ 0xa5a5a5a5a5a5a5a5, + .h6 = sha512_iv.h6 ^ 0xa5a5a5a5a5a5a5a5, .h7 = sha512_iv.h7 ^ 0xa5a5a5a5a5a5a5a5}; + + uint8_t in[12]; + sprintf((char*)in, "SHA-512/%i", i); + struct sha512_hash a = sha512_t_init(oh); + sha512_update(in, strlen((char*)in), &a); + _sha512_t_final(&a); + free(a.buffer); + return (struct iv){.h0 = a.h0, .h1 = a.h1, .h2 = a.h2, .h3 = a.h3, .h4 = a.h4, .h5 = a.h5, .h6 = a.h6, .h7 = a.h7}; } //common_hash_clone(sha512); @@ -258,7 +258,7 @@ lua_common_hash_clone_oargs(sha512, sha512, l_sha512_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_ni(sha512, sha512, sha512_t_init_l(sha512_iv, L), sha512_t_free_l); lua_common_hash_update(sha512, sha512); @@ -278,7 +278,7 @@ lua_common_hash_clone_oargs(sha384, sha384, l_sha384_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_ni(sha384, sha384, sha512_t_init_l(sha384_iv, L), sha512_t_free_l); lua_common_hash_update(sha384, sha384); @@ -309,12 +309,12 @@ lua_common_hash_meta(sha512_t); int l_sha512_t_init(lua_State* L){ int tt = luaL_checkinteger(L, -1); lua_newtable(L); - + struct sha512_hash* a = (struct sha512_hash*)lua_newuserdata(L, sizeof * a);\ - int ud = lua_gettop(L); + int ud = lua_gettop(L); *a = sha512_t_init_l(sha_iv_gen(tt), L); a->t = tt; - + lua_common_hash_meta_def(sha512_t, sha512_t_free_l); lua_pushvalue(L, ud); @@ -335,26 +335,26 @@ int l_sha512_t_final(lua_State* L){ } int l_sha512(lua_State* L){ - if(lua_gettop(L) == 0) return l_sha512_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[512] = {0}; - - sha512(a, len, digest); - lua_pushstring(L, digest); - return 1; + if(lua_gettop(L) == 0) return l_sha512_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + char digest[512] = {0}; + + sha512(a, len, digest); + lua_pushstring(L, digest); + return 1; } int l_sha384(lua_State* L){ - if(lua_gettop(L) == 0) return l_sha384_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); + if(lua_gettop(L) == 0) return l_sha384_init(L); + size_t len = 0; + uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - char digest[384] = {0}; + char digest[384] = {0}; - sha384(a, len, digest); - lua_pushstring(L, digest); - return 1; + sha384(a, len, digest); + lua_pushstring(L, digest); + return 1; } int l_sha512_t(lua_State* L){ diff --git a/src/hash/sha2-256.h b/src/hash/sha2-256.h index 524693d..3a29b7c 100644 --- a/src/hash/sha2-256.h +++ b/src/hash/sha2-256.h @@ -2,23 +2,23 @@ #include struct iv { - uint64_t h0, h1, h2, h3, h4, h5, h6, h7; + uint64_t h0, h1, h2, h3, h4, h5, h6, h7; }; static const struct iv sha512_iv = {.h0 = 0x6a09e667f3bcc908, .h1 = 0xbb67ae8584caa73b, .h2 = 0x3c6ef372fe94f82b, .h3 = 0xa54ff53a5f1d36f1, - .h4 = 0x510e527fade682d1, .h5 = 0x9b05688c2b3e6c1f, .h6 = 0x1f83d9abfb41bd6b, .h7 = 0x5be0cd19137e2179}; + .h4 = 0x510e527fade682d1, .h5 = 0x9b05688c2b3e6c1f, .h6 = 0x1f83d9abfb41bd6b, .h7 = 0x5be0cd19137e2179}; static const struct iv sha384_iv = {.h0 = 0xcbbb9d5dc1059ed8, .h1 = 0x629a292a367cd507, .h2 = 0x9159015a3070dd17, .h3 = 0x152fecd8f70e5939, - .h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4}; + .h4 = 0x67332667ffc00b31, .h5 = 0x8eb44a8768581511, .h6 = 0xdb0c2e0d64f98fa7, .h7 = 0x47b5481dbefa4fa4}; #define sha384_update sha512_update #define sha384_hash sha512_hash struct sha512_hash { - uint8_t* buffer; - size_t bufflen; - uint64_t total, h0, h1, h2, h3, h4, h5, h6, h7; - uint16_t t; + uint8_t* buffer; + size_t bufflen; + uint64_t total, h0, h1, h2, h3, h4, h5, h6, h7; + uint16_t t; }; struct iv sha_iv_gen(int i); diff --git a/src/hash/sha2xx.c b/src/hash/sha2xx.c index c67e40d..531967e 100644 --- a/src/hash/sha2xx.c +++ b/src/hash/sha2xx.c @@ -6,10 +6,10 @@ #define bs 64 struct sha256_hash sha256_init(){ - struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, - .total = 0, .bufflen = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - return a; + struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, + .total = 0, .bufflen = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + return a; } int sha256_free_l(lua_State* L){ @@ -21,96 +21,96 @@ int sha256_free_l(lua_State* L){ #define sha224_free_l sha256_free_l struct sha256_hash sha256_init_l(lua_State* L){ - struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, - .total = 0, .bufflen = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - memset(a.buffer, 0, bs); - return a; + struct sha256_hash a = {.h0 = 0x6a09e667, .h1 = 0xbb67ae85, .h2 = 0x3c6ef372, .h3 = 0xa54ff53a, .h4 = 0x510e527f, .h5 = 0x9b05688c, .h6 = 0x1f83d9ab, .h7 = 0x5be0cd19, + .total = 0, .bufflen = 0}; + a.buffer = calloc(sizeof * a.buffer, bs); + memset(a.buffer, 0, bs); + return a; } struct sha256_hash sha224_init(){ - struct sha256_hash a = sha256_init(); - a.h0 = 0xc1059ed8; - a.h1 = 0x367cd507; - a.h2 = 0x3070dd17; - a.h3 = 0xf70e5939; - a.h4 = 0xffc00b31; - a.h5 = 0x68581511; - a.h6 = 0x64f98fa7; - a.h7 = 0xbefa4fa4; - return a; + struct sha256_hash a = sha256_init(); + a.h0 = 0xc1059ed8; + a.h1 = 0x367cd507; + a.h2 = 0x3070dd17; + a.h3 = 0xf70e5939; + a.h4 = 0xffc00b31; + a.h5 = 0x68581511; + a.h6 = 0x64f98fa7; + a.h7 = 0xbefa4fa4; + return a; } struct sha256_hash sha224_init_l(lua_State* L){ - struct sha256_hash a = sha256_init_l(L); - a.h0 = 0xc1059ed8; - a.h1 = 0x367cd507; - a.h2 = 0x3070dd17; - a.h3 = 0xf70e5939; - a.h4 = 0xffc00b31; - a.h5 = 0x68581511; - a.h6 = 0x64f98fa7; - a.h7 = 0xbefa4fa4; - return a; + struct sha256_hash a = sha256_init_l(L); + a.h0 = 0xc1059ed8; + a.h1 = 0x367cd507; + a.h2 = 0x3070dd17; + a.h3 = 0xf70e5939; + a.h4 = 0xffc00b31; + a.h5 = 0x68581511; + a.h6 = 0x64f98fa7; + a.h7 = 0xbefa4fa4; + return a; } void sha256_round(struct sha256_hash* hash){ - const uint32_t k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, - 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, - 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, - 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, - 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, - 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, - 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; - uint32_t W[64]; - memset(W, 0, 64 * sizeof (uint32_t)); - - for (int i = 0; i < 16; i++) - W[i] = (hash->buffer[i * 4] << 24) | (hash->buffer[i * 4 + 1] << 16) | (hash->buffer[i * 4 + 2] << 8) | (hash->buffer[i * 4 + 3]); - - for(int i = 16; i != 64; i++){ - uint32_t s0 = rotr32(W[i - 15], 7) ^ rotr32(W[i - 15], 18) ^ (W[i - 15] >> 3); - uint32_t s1 = rotr32(W[i - 2], 17) ^ rotr32(W[i - 2], 19) ^ (W[i - 2] >> 10); - W[i] = W[i - 16] + s0 + W[i - 7] + s1; - } - - uint32_t a = hash->h0; - uint32_t b = hash->h1; - uint32_t c = hash->h2; - uint32_t d = hash->h3; - uint32_t e = hash->h4; - uint32_t f = hash->h5; - uint32_t g = hash->h6; - uint32_t h = hash->h7; - - for(int i = 0; i != 64; i++){ - uint32_t S1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25); - uint32_t ch = (e & f) ^ ((~e) & g); - uint32_t temp1 = h + S1 + ch + k[i] + W[i]; - - uint32_t S0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22); - uint32_t maj = (a & b) ^ (a & c) ^ (b & c); - uint32_t temp2 = S0 + maj; - - h = g; - g = f; - f = e; - e = d + temp1; - d = c; - c = b; - b = a; - a = temp1 + temp2; - } - - hash->h0 += a; - hash->h1 += b; - hash->h2 += c; - hash->h3 += d; - hash->h4 += e; - hash->h5 += f; - hash->h6 += g; - hash->h7 += h; + const uint32_t k[64] = {0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, + 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, + 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, + 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, + 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, + 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, + 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2}; + uint32_t W[64]; + memset(W, 0, 64 * sizeof (uint32_t)); + + for (int i = 0; i < 16; i++) + W[i] = (hash->buffer[i * 4] << 24) | (hash->buffer[i * 4 + 1] << 16) | (hash->buffer[i * 4 + 2] << 8) | (hash->buffer[i * 4 + 3]); + + for(int i = 16; i != 64; i++){ + uint32_t s0 = rotr32(W[i - 15], 7) ^ rotr32(W[i - 15], 18) ^ (W[i - 15] >> 3); + uint32_t s1 = rotr32(W[i - 2], 17) ^ rotr32(W[i - 2], 19) ^ (W[i - 2] >> 10); + W[i] = W[i - 16] + s0 + W[i - 7] + s1; + } + + uint32_t a = hash->h0; + uint32_t b = hash->h1; + uint32_t c = hash->h2; + uint32_t d = hash->h3; + uint32_t e = hash->h4; + uint32_t f = hash->h5; + uint32_t g = hash->h6; + uint32_t h = hash->h7; + + for(int i = 0; i != 64; i++){ + uint32_t S1 = rotr32(e, 6) ^ rotr32(e, 11) ^ rotr32(e, 25); + uint32_t ch = (e & f) ^ ((~e) & g); + uint32_t temp1 = h + S1 + ch + k[i] + W[i]; + + uint32_t S0 = rotr32(a, 2) ^ rotr32(a, 13) ^ rotr32(a, 22); + uint32_t maj = (a & b) ^ (a & c) ^ (b & c); + uint32_t temp2 = S0 + maj; + + h = g; + g = f; + f = e; + e = d + temp1; + d = c; + c = b; + b = a; + a = temp1 + temp2; + } + + hash->h0 += a; + hash->h1 += b; + hash->h2 += c; + hash->h3 += d; + hash->h4 += e; + hash->h5 += f; + hash->h6 += g; + hash->h7 += h; } #define sha224_update sha256_update @@ -149,7 +149,7 @@ void _sha256_final(struct sha256_hash* hash){ sha256_round(hash); memset(hash->buffer, 0, bs); } - + size_t blen = 8*hash->total; for(int i = 0; i != 8; i++) hash->buffer[63 - i] = (uint8_t) (blen >> (i * 8) & 0xFF); @@ -158,43 +158,43 @@ void _sha256_final(struct sha256_hash* hash){ } void sha256_final(struct sha256_hash* hash, char* out){ - uint8_t old[bs]; - struct sha256_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); + uint8_t old[bs]; + struct sha256_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs); - _sha256_final(hash); - sprintf(out, "%x%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6,hash->h7); + _sha256_final(hash); + sprintf(out, "%x%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6,hash->h7); - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs); } void sha224_final(struct sha256_hash* hash, char* out){ - uint8_t old[bs]; - struct sha256_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); + uint8_t old[bs]; + struct sha256_hash old_hash; + memcpy(&old_hash, hash, sizeof * hash); + memcpy(old, hash->buffer, bs); - _sha256_final(hash); - sprintf(out, "%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6); + _sha256_final(hash); + sprintf(out, "%x%x%x%x%x%x%x",hash->h0,hash->h1,hash->h2,hash->h3,hash->h4,hash->h5,hash->h6); - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); + memcpy(hash, &old_hash, sizeof * hash); + memcpy(hash->buffer, old, bs); } void sha256(uint8_t* in, size_t len, char* out){ - struct sha256_hash a = sha256_init(); - sha256_update(in, len, &a); - sha256_final(&a, out); - free(a.buffer); + struct sha256_hash a = sha256_init(); + sha256_update(in, len, &a); + sha256_final(&a, out); + free(a.buffer); } void sha224(uint8_t* in, size_t len, char* out){ - struct sha256_hash a = sha224_init(); - sha224_update(in, len, &a); - sha224_final(&a, out); - free(a.buffer); + struct sha256_hash a = sha224_init(); + sha224_update(in, len, &a); + sha224_final(&a, out); + free(a.buffer); } //common_hash_clone(sha256); @@ -203,7 +203,7 @@ lua_common_hash_clone_oargs(sha256, sha256, l_sha256_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_l(sha256, sha256); lua_common_hash_update(sha256, sha256); @@ -221,7 +221,7 @@ int l_sha256(lua_State* L){ if(lua_gettop(L) == 0) return l_sha256_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[256]; sha256(a, len, digest); @@ -237,7 +237,7 @@ lua_common_hash_clone_oargs(sha224, sha224, l_sha224_init(L), { *b = *a; b->buffer = old; memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); -}); + }); lua_common_hash_init_l(sha224, sha224); lua_common_hash_update(sha224, sha224); @@ -255,7 +255,7 @@ int l_sha224(lua_State* L){ if(lua_gettop(L) == 0) return l_sha224_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[224]; sha224(a, len, digest); diff --git a/src/hash/sha2xx.h b/src/hash/sha2xx.h index cd7dc83..8fe94a7 100644 --- a/src/hash/sha2xx.h +++ b/src/hash/sha2xx.h @@ -1,10 +1,10 @@ #include "../lua.h" struct sha256_hash { - uint8_t* buffer; - size_t bufflen; - uint64_t total; - uint32_t h0, h1, h2, h3, h4, h5, h6, h7; + uint8_t* buffer; + size_t bufflen; + uint64_t total; + uint32_t h0, h1, h2, h3, h4, h5, h6, h7; }; int l_sha256(lua_State*); diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c index 527bcc7..e283949 100644 --- a/src/hash/sysvchecksum.c +++ b/src/hash/sysvchecksum.c @@ -41,12 +41,12 @@ int l_sysvchecksum_final(lua_State* L){ lua_pushstring(L, digest); return 1; } - + int l_sysvchecksum(lua_State* L){ if(lua_gettop(L) == 0) return l_sysvchecksum_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[16]; uint16_t u = sysvchecksum(a, len); diff --git a/src/hash/sysvchecksum.h b/src/hash/sysvchecksum.h index cae322d..8eee0b7 100644 --- a/src/hash/sysvchecksum.h +++ b/src/hash/sysvchecksum.h @@ -2,7 +2,7 @@ #include struct sysvchecksum_hash { - uint32_t check; + uint32_t check; }; struct sysvchecksum_hash sysvchecksum_init(); diff --git a/src/hash/xor.c b/src/hash/xor.c index bca4d5b..3f7903b 100644 --- a/src/hash/xor.c +++ b/src/hash/xor.c @@ -26,22 +26,22 @@ uint8_t xor8(uint8_t* aa, size_t len){ } common_hash_clone(xor8) -common_hash_init_update(xor8); + common_hash_init_update(xor8); -int l_xor8_final(lua_State* L){ - struct xor8_hash* a = (struct xor8_hash*)lua_touserdata(L, 1); - uint8_t u = xor8_final(a); - char digest[8]; - sprintf(digest,"%02x",u); - lua_pushstring(L, digest); - return 1; -} + int l_xor8_final(lua_State* L){ + struct xor8_hash* a = (struct xor8_hash*)lua_touserdata(L, 1); + uint8_t u = xor8_final(a); + char digest[8]; + sprintf(digest,"%02x",u); + lua_pushstring(L, digest); + return 1; + } int l_xor8(lua_State* L){ if(lua_gettop(L) == 0) return l_xor8_init(L); size_t len = 0; uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - + char digest[8]; uint8_t u = xor8(a, len); diff --git a/src/hash/xor.h b/src/hash/xor.h index 6065524..a05de5c 100644 --- a/src/hash/xor.h +++ b/src/hash/xor.h @@ -2,7 +2,7 @@ #include struct xor8_hash { - uint8_t a; + uint8_t a; }; struct xor8_hash xor8_init(); @@ -13,4 +13,4 @@ uint8_t xor8(uint8_t*, size_t); int l_xor8(lua_State*); int l_xor8_init(lua_State*); int l_xor8_update(lua_State*); -int l_xor8_final(lua_State*); \ No newline at end of file +int l_xor8_final(lua_State*); -- cgit v1.2.3