diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/crypto.c | 50 | ||||
| -rw-r--r-- | src/crypto.h | 344 | ||||
| -rw-r--r-- | src/hash/adler.c | 104 | ||||
| -rw-r--r-- | src/hash/blake.c | 1268 | ||||
| -rw-r--r-- | src/hash/blake.h | 108 | ||||
| -rw-r--r-- | src/hash/blake2.c | 46 | ||||
| -rw-r--r-- | src/hash/bsdchecksum.c | 108 | ||||
| -rw-r--r-- | src/hash/cityhash.c | 860 | ||||
| -rw-r--r-- | src/hash/crc.c | 320 | ||||
| -rw-r--r-- | src/hash/djb2.c | 100 | ||||
| -rw-r--r-- | src/hash/farmhash.c | 338 | ||||
| -rw-r--r-- | src/hash/fasthash.c | 166 | ||||
| -rw-r--r-- | src/hash/fletcher.c | 286 | ||||
| -rw-r--r-- | src/hash/fnv.c | 202 | ||||
| -rw-r--r-- | src/hash/jenkins.c | 114 | ||||
| -rw-r--r-- | src/hash/loselose.c | 100 | ||||
| -rw-r--r-- | src/hash/md5.c | 334 | ||||
| -rw-r--r-- | src/hash/metrohash.c | 444 | ||||
| -rw-r--r-- | src/hash/pearson.c | 174 | ||||
| -rw-r--r-- | src/hash/pjw.c | 106 | ||||
| -rw-r--r-- | src/hash/sdbm.c | 100 | ||||
| -rw-r--r-- | src/hash/sha01.c | 446 | ||||
| -rw-r--r-- | src/hash/sha2-256.c | 696 | ||||
| -rw-r--r-- | src/hash/sysvchecksum.c | 100 | ||||
| -rw-r--r-- | src/hash/xor.c | 96 | ||||
| -rw-r--r-- | src/hash/xxh.c | 308 | ||||
| -rw-r--r-- | src/util.c | 138 |
27 files changed, 3705 insertions, 3751 deletions
diff --git a/src/crypto.c b/src/crypto.c index 8bb896a..7556b6f 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -1,25 +1,25 @@ -#include "crypto.h" - -uint8_t rotl8(uint8_t y, uint8_t offset){ - return ( y << offset ) | ( y >> (8 - offset)); -} - -uint16_t rotl16(uint16_t y, uint16_t offset){ - return ( y << offset ) | ( y >> (16 - offset)); -} - -unsigned rotl32(unsigned y, unsigned offset){ - return ( y << offset ) | ( y >> (32 - offset)); -} - -unsigned rotr32(unsigned x, unsigned n) { - return (x >> n % 32) | (x << (32-n) % 32); -} - -uint64_t rotl64(uint64_t y, uint64_t offset){ - return ( y << offset ) | ( y >> (64 - offset)); -} - -uint64_t rotr64(uint64_t x, uint64_t n) { - return (x >> n) | (x << (64-n)); -} +#include "crypto.h"
+
+uint8_t rotl8(uint8_t y, uint8_t offset){
+ return ( y << offset ) | ( y >> (8 - offset));
+}
+
+uint16_t rotl16(uint16_t y, uint16_t offset){
+ return ( y << offset ) | ( y >> (16 - offset));
+}
+
+unsigned rotl32(unsigned y, unsigned offset){
+ return ( y << offset ) | ( y >> (32 - offset));
+}
+
+unsigned rotr32(unsigned x, unsigned n) {
+ return (x >> n % 32) | (x << (32-n) % 32);
+}
+
+uint64_t rotl64(uint64_t y, uint64_t offset){
+ return ( y << offset ) | ( y >> (64 - offset));
+}
+
+uint64_t rotr64(uint64_t x, uint64_t n) {
+ return (x >> n) | (x << (64-n));
+}
diff --git a/src/crypto.h b/src/crypto.h index 886aeeb..3fb0e58 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -1,172 +1,172 @@ -#include "lua.h" -#include "util.h" -#include "hash/md5.h" -#include "hash/sha01.h" -#include "hash/sha2xx.h" -#include "hash/pearson.h" -#include "hash/xxh.h" -#include "hash/adler.h" -#include "hash/bsdchecksum.h" -#include "hash/crc.h" -#include "hash/fletcher.h" -#include "hash/sysvchecksum.h" -#include "hash/xor.h" -#include "hash/buzhash.h" -#include "hash/cityhash.h" -#include "hash/djb2.h" -#include "hash/farmhash.h" -#include "hash/fasthash.h" -#include "hash/fnv.h" -#include "hash/jenkins.h" -#include "hash/loselose.h" -#include "hash/metrohash.h" -#include "hash/murmur.h" -#include "hash/pjw.h" -#include "hash/sdbm.h" -#include "hash/sha2-256.h" -#include "hash/spookyhash.h" -#include "hash/blake2.h" -#include "hash/blake.h" - -#include "encode/uuencode.h" -#include "encode/base64.h" -#include "encode/baseN.h" - -uint8_t rotl8(uint8_t, uint8_t); -uint16_t rotl16(uint16_t, uint16_t); -unsigned rotl32(unsigned, unsigned); -unsigned rotr32(unsigned, unsigned); -uint64_t rotl64(uint64_t, uint64_t); -uint64_t rotr64(uint64_t, uint64_t); - -int tp(lua_State*); - -#define common_hash_init_update(hashname) lua_common_hash_init_update(hashname, hashname) -#define lua_common_hash_init_update(hashname, luaname) lua_common_hash_init(hashname, luaname) lua_common_hash_update(hashname, luaname) -#define lua_common_hash_init(hashname, luaname) lua_common_hash_init_ni(hashname, luaname, hashname##_init()) - -#define common_hash_clone(hashname) lua_common_hash_clone(hashname, hashname) -#define lua_common_hash_clone(hashname, luaname) lua_common_hash_clone_oargs(hashname, luaname, l_##luaname##_init(L)) -#define lua_common_hash_clone_oargs(hashname, luaname, oinit)\ -int l_##luaname##_clone(lua_State* L){\ - struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\ - oinit;\ - struct hashname##_hash* b = (struct hashname##_hash*)lua_touserdata(L, -1);\ - *b = *a;\ - return 1;\ -} - -#define lua_common_hash_meta(luaname)\ -int _##luaname##_hash_add(lua_State*L){\ - lua_pushvalue(L, 1);\ - l_##luaname##_clone(L);\ - lua_pushvalue(L, -1);\ - lua_pushvalue(L, 2);\ - return l_##luaname##_update(L);\ - }\ -int _##luaname##_common_hash(lua_State* L){\ - lua_newtable(L);\ - int ti = lua_gettop(L);\ - luaI_tsetcf(L, ti, "update", l_##luaname##_update);\ - luaI_tsetcf(L, ti, "final", l_##luaname##_final);\ - \ - lua_pushvalue(L, 2);\ - lua_gettable(L, ti);\ - return 1;\ - } - -#define lua_common_hash_meta_def(luaname)\ - lua_newtable(L);\ - int mt = lua_gettop(L);\ - luaI_tsetcf(L, mt, "__index", _##luaname##_common_hash);\ - luaI_tsetcf(L, mt, "__add", _##luaname##_hash_add);\ - lua_pushvalue(L, mt);\ - lua_setmetatable(L, ud);\ - -#define lua_common_hash_init_ni(hashname, luaname, initf)\ - lua_common_hash_meta(luaname);\ - int l_##luaname##_init(lua_State* L){\ - \ - struct hashname##_hash* a = (struct hashname##_hash*)lua_newuserdata(L, sizeof * a);\ - int ud = lua_gettop(L);\ - *a = initf;\ - lua_common_hash_meta_def(luaname);\ - lua_pushvalue(L, ud);\ - return 1;\ -} - -#define lua_common_hash_update(hashname, luaname)\ -int l_##luaname##_update(lua_State* L){\ - struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -2);\ - size_t len = 0;\ - uint8_t* b = (uint8_t*)luaL_checklstring(L, -1, &len);\ - \ - hashname##_update(b, len, a);\ - \ - lua_pushvalue(L, -2);\ - return 1;\ -} - -static const luaL_Reg crypto_function_list [] = { - {"setpearson",l_setpearson}, {"xxh64",l_xxh64}, - {"xxh32",l_xxh32}, {"fletcher8",l_fletcher8}, - {"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32}, - {"setbuzhash",l_setbuzhash}, - {"cityhash32", l_cityhash32}, - {"cityhash64", l_cityhash64}, {"cityhash128", l_cityhash128}, - {"farmhash32", l_farmhash32}, {"farmhash64", l_farmhash64}, - {"fasthash32", l_fasthash32}, {"fasthash64", l_fasthash64}, - {"loselose", l_loselose}, - {"metrohash64_v1", l_metrohash64_v1}, {"metrohash64_v2", l_metrohash64_v2}, - {"metrohash128_v1", l_metrohash128_v1}, {"metrohash128_v2", l_metrohash128_v2}, - {"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32}, - {"spookyhash128_v1", l_spookyhash128_v1}, {"spookyhash128_v2", l_spookyhash128_v2}, - {"spookyhash64_v1", l_spookyhash64_v1}, {"spookyhash64_v2", l_spookyhash64_v2}, - {"spookyhash32_v1", l_spookyhash32_v1}, {"spookyhash32_v2", l_spookyhash32_v2}, - {"blake2b", l_blake2b}, - {"blake512", l_blake512}, {"blake384", l_blake384}, - - {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final}, - {"bsdchecksum",l_bsdchecksum}, {"bsdchecksum_init",l_bsdchecksum_init}, {"bsdchecksum_update",l_bsdchecksum_update}, {"bsdchecksum_final",l_bsdchecksum_final}, - {"buzhash8",l_buzhash8}, {"buzhash16",l_buzhash16}, - {"crc8",l_crc8}, {"crc8_init",l_crc8_init}, {"crc8_update",l_crc8_update}, {"crc8_final",l_crc8_final}, - {"crc16",l_crc16}, {"crc16_init",l_crc16_init}, {"crc16_update",l_crc16_update}, {"crc16_final",l_crc16_final}, - {"crc32",l_crc32}, {"crc32_init",l_crc32_init}, {"crc32_update",l_crc32_update}, {"crc32_final",l_crc32_final}, - {"djb2", l_djb2}, {"djb2_init", l_djb2_init}, {"djb2_update", l_djb2_update}, {"djb2_final", l_djb2_final}, - {"fletcher8",l_fletcher8}, {"fletcher8_init",l_fletcher8_init}, {"fletcher8_update",l_fletcher8_update}, {"fletcher8_final",l_fletcher8_final}, - {"fletcher16",l_fletcher16}, {"fletcher16_init",l_fletcher16_init}, {"fletcher16_update",l_fletcher16_update}, {"fletcher16_final",l_fletcher16_final}, - {"fletcher32",l_fletcher32}, {"fletcher32_init",l_fletcher32_init}, {"fletcher32_update",l_fletcher32_update}, {"fletcher32_final",l_fletcher32_final}, - {"fnv_0", l_fnv_0}, {"fnv_0_init", l_fnv_0_init}, {"fnv_0_update", l_fnv_0_update}, {"fnv_0_final", l_fnv_0_final}, - {"fnv_1", l_fnv_1}, {"fnv_1_init", l_fnv_1_init}, {"fnv_1_update", l_fnv_1_update}, {"fnv_1_final", l_fnv_1_final}, - {"fnv_a", l_fnv_a}, {"fnv_a_init", l_fnv_a_init}, {"fnv_a_update", l_fnv_a_update}, {"fnv_a_final", l_fnv_a_final}, - {"oaat", l_oaat}, {"oaat_init", l_oaat_init}, {"oaat_update", l_oaat_update}, {"oaat_final", l_oaat_final}, - {"loselose", l_loselose}, {"loselose_init", l_loselose_init}, {"loselose_update", l_loselose_update}, {"loselose_final", l_loselose_final}, - {"pearson",l_pearson}, {"pearson_init",l_pearson_init}, {"pearson_update",l_pearson_update}, {"pearson_final",l_pearson_final}, - {"pjw", l_pjw}, {"pjw_init", l_pjw_init}, {"pjw_update", l_pjw_update}, {"pjw_final", l_pjw_final}, - {"sdbm", l_sdbm}, {"sdbm_init", l_sdbm_init}, {"sdbm_update", l_sdbm_update}, {"sdbm_final", l_sdbm_final}, - {"sysvchecksum",l_sysvchecksum}, {"sysvchecksum_init",l_sysvchecksum_init}, {"sysvchecksum_update",l_sysvchecksum_update}, {"sysvchecksum_final",l_sysvchecksum_final}, - {"xor8",l_xor8}, {"xor8_init",l_xor8_init}, {"xor8_update",l_xor8_update}, {"xor8_final",l_xor8_final}, - {"md5",l_md5}, {"md5_init",l_md5_init}, {"md5_update",l_md5_update}, {"md5_final",l_md5_final}, - {"sha0",l_sha0}, {"sha0_init",l_sha0_init}, {"sha0_update",l_sha0_update}, {"sha0_final",l_sha0_final}, - {"sha1",l_sha1}, {"sha1_init",l_sha1_init}, {"sha1_update",l_sha1_update}, {"sha1_final",l_sha1_final}, - {"sha512", l_sha512}, {"sha512_init", l_sha512_init}, {"sha512_update", l_sha512_update}, {"sha512_final", l_sha512_final}, - {"sha384", l_sha384}, {"sha384_init", l_sha384_init}, {"sha384_update", l_sha384_update}, {"sha384_final", l_sha384_final}, - {"sha512_t", l_sha512_t}, {"sha512_t_init", l_sha512_t_init}, {"sha512_t_update", l_sha512_t_update}, {"sha512_t_final", l_sha512_t_final}, - {"sha256",l_sha256}, {"sha256_init",l_sha256_init}, {"sha256_update",l_sha256_update}, {"sha256_final",l_sha256_final}, - {"sha224",l_sha224}, {"sha224_init",l_sha224_init}, {"sha224_update",l_sha224_update}, {"sha224_final",l_sha224_final}, - {"blake256", l_blake256}, {"blake256_init", l_blake256_init}, {"blake256_update", l_blake256_update}, {"blake256_final", l_blake256_final}, - {"blake224", l_blake224}, {"blake224_init", l_blake224_init}, {"blake224_update", l_blake224_update}, {"blake224_final", l_blake224_final}, - {"blake512", l_blake512}, {"blake512_init", l_blake512_init}, {"blake512_update", l_blake512_update}, {"blake512_final", l_blake512_final}, - {"blake384", l_blake384}, {"blake384_init", l_blake384_init}, {"blake384_update", l_blake384_update}, {"blake384_final", l_blake384_final}, - {"blake2s", l_blake2s}, {"blake2s_init", l_blake2s_init}, {"blake2s_update", l_blake2s_update}, {"blake2s_final", l_blake2s_final}, - {"uuencode",l_uuencode}, - {"uudecode",l_uudecode}, - - {"base64encode",l_base64encode}, - {"base64decode",l_base64decode}, - - {"baseconvert",l_baseconvert}, - - {NULL,NULL} -}; - +#include "lua.h"
+#include "util.h"
+#include "hash/md5.h"
+#include "hash/sha01.h"
+#include "hash/sha2xx.h"
+#include "hash/pearson.h"
+#include "hash/xxh.h"
+#include "hash/adler.h"
+#include "hash/bsdchecksum.h"
+#include "hash/crc.h"
+#include "hash/fletcher.h"
+#include "hash/sysvchecksum.h"
+#include "hash/xor.h"
+#include "hash/buzhash.h"
+#include "hash/cityhash.h"
+#include "hash/djb2.h"
+#include "hash/farmhash.h"
+#include "hash/fasthash.h"
+#include "hash/fnv.h"
+#include "hash/jenkins.h"
+#include "hash/loselose.h"
+#include "hash/metrohash.h"
+#include "hash/murmur.h"
+#include "hash/pjw.h"
+#include "hash/sdbm.h"
+#include "hash/sha2-256.h"
+#include "hash/spookyhash.h"
+#include "hash/blake2.h"
+#include "hash/blake.h"
+
+#include "encode/uuencode.h"
+#include "encode/base64.h"
+#include "encode/baseN.h"
+
+uint8_t rotl8(uint8_t, uint8_t);
+uint16_t rotl16(uint16_t, uint16_t);
+unsigned rotl32(unsigned, unsigned);
+unsigned rotr32(unsigned, unsigned);
+uint64_t rotl64(uint64_t, uint64_t);
+uint64_t rotr64(uint64_t, uint64_t);
+
+int tp(lua_State*);
+
+#define common_hash_init_update(hashname) lua_common_hash_init_update(hashname, hashname)
+#define lua_common_hash_init_update(hashname, luaname) lua_common_hash_init(hashname, luaname) lua_common_hash_update(hashname, luaname)
+#define lua_common_hash_init(hashname, luaname) lua_common_hash_init_ni(hashname, luaname, hashname##_init())
+
+#define common_hash_clone(hashname) lua_common_hash_clone(hashname, hashname)
+#define lua_common_hash_clone(hashname, luaname) lua_common_hash_clone_oargs(hashname, luaname, l_##luaname##_init(L))
+#define lua_common_hash_clone_oargs(hashname, luaname, oinit)\
+int l_##luaname##_clone(lua_State* L){\
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -1);\
+ oinit;\
+ struct hashname##_hash* b = (struct hashname##_hash*)lua_touserdata(L, -1);\
+ *b = *a;\
+ return 1;\
+}
+
+#define lua_common_hash_meta(luaname)\
+int _##luaname##_hash_add(lua_State*L){\
+ lua_pushvalue(L, 1);\
+ l_##luaname##_clone(L);\
+ lua_pushvalue(L, -1);\
+ lua_pushvalue(L, 2);\
+ return l_##luaname##_update(L);\
+ }\
+int _##luaname##_common_hash(lua_State* L){\
+ lua_newtable(L);\
+ int ti = lua_gettop(L);\
+ luaI_tsetcf(L, ti, "update", l_##luaname##_update);\
+ luaI_tsetcf(L, ti, "final", l_##luaname##_final);\
+ \
+ lua_pushvalue(L, 2);\
+ lua_gettable(L, ti);\
+ return 1;\
+ }
+
+#define lua_common_hash_meta_def(luaname)\
+ lua_newtable(L);\
+ int mt = lua_gettop(L);\
+ luaI_tsetcf(L, mt, "__index", _##luaname##_common_hash);\
+ luaI_tsetcf(L, mt, "__add", _##luaname##_hash_add);\
+ lua_pushvalue(L, mt);\
+ lua_setmetatable(L, ud);\
+
+#define lua_common_hash_init_ni(hashname, luaname, initf)\
+ lua_common_hash_meta(luaname);\
+ int l_##luaname##_init(lua_State* L){\
+ \
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_newuserdata(L, sizeof * a);\
+ int ud = lua_gettop(L);\
+ *a = initf;\
+ lua_common_hash_meta_def(luaname);\
+ lua_pushvalue(L, ud);\
+ return 1;\
+}
+
+#define lua_common_hash_update(hashname, luaname)\
+int l_##luaname##_update(lua_State* L){\
+ struct hashname##_hash* a = (struct hashname##_hash*)lua_touserdata(L, -2);\
+ size_t len = 0;\
+ uint8_t* b = (uint8_t*)luaL_checklstring(L, -1, &len);\
+ \
+ hashname##_update(b, len, a);\
+ \
+ lua_pushvalue(L, -2);\
+ return 1;\
+}
+
+static const luaL_Reg crypto_function_list [] = {
+ {"setpearson",l_setpearson}, {"xxh64",l_xxh64},
+ {"xxh32",l_xxh32}, {"fletcher8",l_fletcher8},
+ {"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32},
+ {"setbuzhash",l_setbuzhash},
+ {"cityhash32", l_cityhash32},
+ {"cityhash64", l_cityhash64}, {"cityhash128", l_cityhash128},
+ {"farmhash32", l_farmhash32}, {"farmhash64", l_farmhash64},
+ {"fasthash32", l_fasthash32}, {"fasthash64", l_fasthash64},
+ {"loselose", l_loselose},
+ {"metrohash64_v1", l_metrohash64_v1}, {"metrohash64_v2", l_metrohash64_v2},
+ {"metrohash128_v1", l_metrohash128_v1}, {"metrohash128_v2", l_metrohash128_v2},
+ {"murmur1_32", l_murmur1_32}, {"murmur2_32", l_murmur2_32},
+ {"spookyhash128_v1", l_spookyhash128_v1}, {"spookyhash128_v2", l_spookyhash128_v2},
+ {"spookyhash64_v1", l_spookyhash64_v1}, {"spookyhash64_v2", l_spookyhash64_v2},
+ {"spookyhash32_v1", l_spookyhash32_v1}, {"spookyhash32_v2", l_spookyhash32_v2},
+ {"blake2b", l_blake2b},
+ {"blake512", l_blake512}, {"blake384", l_blake384},
+
+ {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final},
+ {"bsdchecksum",l_bsdchecksum}, {"bsdchecksum_init",l_bsdchecksum_init}, {"bsdchecksum_update",l_bsdchecksum_update}, {"bsdchecksum_final",l_bsdchecksum_final},
+ {"buzhash8",l_buzhash8}, {"buzhash16",l_buzhash16},
+ {"crc8",l_crc8}, {"crc8_init",l_crc8_init}, {"crc8_update",l_crc8_update}, {"crc8_final",l_crc8_final},
+ {"crc16",l_crc16}, {"crc16_init",l_crc16_init}, {"crc16_update",l_crc16_update}, {"crc16_final",l_crc16_final},
+ {"crc32",l_crc32}, {"crc32_init",l_crc32_init}, {"crc32_update",l_crc32_update}, {"crc32_final",l_crc32_final},
+ {"djb2", l_djb2}, {"djb2_init", l_djb2_init}, {"djb2_update", l_djb2_update}, {"djb2_final", l_djb2_final},
+ {"fletcher8",l_fletcher8}, {"fletcher8_init",l_fletcher8_init}, {"fletcher8_update",l_fletcher8_update}, {"fletcher8_final",l_fletcher8_final},
+ {"fletcher16",l_fletcher16}, {"fletcher16_init",l_fletcher16_init}, {"fletcher16_update",l_fletcher16_update}, {"fletcher16_final",l_fletcher16_final},
+ {"fletcher32",l_fletcher32}, {"fletcher32_init",l_fletcher32_init}, {"fletcher32_update",l_fletcher32_update}, {"fletcher32_final",l_fletcher32_final},
+ {"fnv_0", l_fnv_0}, {"fnv_0_init", l_fnv_0_init}, {"fnv_0_update", l_fnv_0_update}, {"fnv_0_final", l_fnv_0_final},
+ {"fnv_1", l_fnv_1}, {"fnv_1_init", l_fnv_1_init}, {"fnv_1_update", l_fnv_1_update}, {"fnv_1_final", l_fnv_1_final},
+ {"fnv_a", l_fnv_a}, {"fnv_a_init", l_fnv_a_init}, {"fnv_a_update", l_fnv_a_update}, {"fnv_a_final", l_fnv_a_final},
+ {"oaat", l_oaat}, {"oaat_init", l_oaat_init}, {"oaat_update", l_oaat_update}, {"oaat_final", l_oaat_final},
+ {"loselose", l_loselose}, {"loselose_init", l_loselose_init}, {"loselose_update", l_loselose_update}, {"loselose_final", l_loselose_final},
+ {"pearson",l_pearson}, {"pearson_init",l_pearson_init}, {"pearson_update",l_pearson_update}, {"pearson_final",l_pearson_final},
+ {"pjw", l_pjw}, {"pjw_init", l_pjw_init}, {"pjw_update", l_pjw_update}, {"pjw_final", l_pjw_final},
+ {"sdbm", l_sdbm}, {"sdbm_init", l_sdbm_init}, {"sdbm_update", l_sdbm_update}, {"sdbm_final", l_sdbm_final},
+ {"sysvchecksum",l_sysvchecksum}, {"sysvchecksum_init",l_sysvchecksum_init}, {"sysvchecksum_update",l_sysvchecksum_update}, {"sysvchecksum_final",l_sysvchecksum_final},
+ {"xor8",l_xor8}, {"xor8_init",l_xor8_init}, {"xor8_update",l_xor8_update}, {"xor8_final",l_xor8_final},
+ {"md5",l_md5}, {"md5_init",l_md5_init}, {"md5_update",l_md5_update}, {"md5_final",l_md5_final},
+ {"sha0",l_sha0}, {"sha0_init",l_sha0_init}, {"sha0_update",l_sha0_update}, {"sha0_final",l_sha0_final},
+ {"sha1",l_sha1}, {"sha1_init",l_sha1_init}, {"sha1_update",l_sha1_update}, {"sha1_final",l_sha1_final},
+ {"sha512", l_sha512}, {"sha512_init", l_sha512_init}, {"sha512_update", l_sha512_update}, {"sha512_final", l_sha512_final},
+ {"sha384", l_sha384}, {"sha384_init", l_sha384_init}, {"sha384_update", l_sha384_update}, {"sha384_final", l_sha384_final},
+ {"sha512_t", l_sha512_t}, {"sha512_t_init", l_sha512_t_init}, {"sha512_t_update", l_sha512_t_update}, {"sha512_t_final", l_sha512_t_final},
+ {"sha256",l_sha256}, {"sha256_init",l_sha256_init}, {"sha256_update",l_sha256_update}, {"sha256_final",l_sha256_final},
+ {"sha224",l_sha224}, {"sha224_init",l_sha224_init}, {"sha224_update",l_sha224_update}, {"sha224_final",l_sha224_final},
+ {"blake256", l_blake256}, {"blake256_init", l_blake256_init}, {"blake256_update", l_blake256_update}, {"blake256_final", l_blake256_final},
+ {"blake224", l_blake224}, {"blake224_init", l_blake224_init}, {"blake224_update", l_blake224_update}, {"blake224_final", l_blake224_final},
+ {"blake512", l_blake512}, {"blake512_init", l_blake512_init}, {"blake512_update", l_blake512_update}, {"blake512_final", l_blake512_final},
+ {"blake384", l_blake384}, {"blake384_init", l_blake384_init}, {"blake384_update", l_blake384_update}, {"blake384_final", l_blake384_final},
+ {"blake2s", l_blake2s}, {"blake2s_init", l_blake2s_init}, {"blake2s_update", l_blake2s_update}, {"blake2s_final", l_blake2s_final},
+ {"uuencode",l_uuencode},
+ {"uudecode",l_uudecode},
+
+ {"base64encode",l_base64encode},
+ {"base64decode",l_base64decode},
+
+ {"baseconvert",l_baseconvert},
+
+ {NULL,NULL}
+};
+
diff --git a/src/hash/adler.c b/src/hash/adler.c index daca0d7..7ee9197 100644 --- a/src/hash/adler.c +++ b/src/hash/adler.c @@ -1,52 +1,52 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct adler32_hash adler32_init(){ - return (struct adler32_hash){.a = 1, .b = 0}; -} - -void adler32_update(uint8_t* aa, size_t len, struct adler32_hash* hash){ - for(int i = 0; i != len; i++){ - hash->a += aa[i]; - hash->b += hash->a; - } -} - -uint32_t adler32_final(struct adler32_hash* hash){ - return hash->b * 65536 + hash->a; -} - -uint32_t adler32(uint8_t* aa, size_t len){ - struct adler32_hash dig = adler32_init(); - adler32_update(aa, len, &dig); - return adler32_final(&dig); -} - -common_hash_clone(adler32); - -common_hash_init_update(adler32); - -int l_adler32_final(lua_State* L){ - struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1); - uint32_t u = adler32_final(a); - char digest[32]; - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} - -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); - - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct adler32_hash adler32_init(){
+ return (struct adler32_hash){.a = 1, .b = 0};
+}
+
+void adler32_update(uint8_t* aa, size_t len, struct adler32_hash* hash){
+ for(int i = 0; i != len; i++){
+ hash->a += aa[i];
+ hash->b += hash->a;
+ }
+}
+
+uint32_t adler32_final(struct adler32_hash* hash){
+ return hash->b * 65536 + hash->a;
+}
+
+uint32_t adler32(uint8_t* aa, size_t len){
+ struct adler32_hash dig = adler32_init();
+ adler32_update(aa, len, &dig);
+ return adler32_final(&dig);
+}
+
+common_hash_clone(adler32);
+
+common_hash_init_update(adler32);
+
+int l_adler32_final(lua_State* L){
+ struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1);
+ uint32_t u = adler32_final(a);
+ char digest[32];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/blake.c b/src/hash/blake.c index 84aa727..c6d73ad 100644 --- a/src/hash/blake.c +++ b/src/hash/blake.c @@ -1,634 +1,634 @@ -#include <string.h> -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <math.h> -#include <stdlib.h> -#include "../crypto.h" -#include "../util.h" - -const uint8_t blake_sigma[][16] = { - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, - {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, - {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, - { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, - { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, - { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, - {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, - {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, - { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, - {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 }, - { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, - {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, - {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, - { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, - { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, - { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 } -}; - -const uint32_t blake_u256[16] = { - 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, - 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, - 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, - 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917 -}; - -const uint64_t blake_u512[16] = { - 0x243f6a8885a308d3ULL, 0x13198a2e03707344ULL, - 0xa4093822299f31d0ULL, 0x082efa98ec4e6c89ULL, - 0x452821e638d01377ULL, 0xbe5466cf34e90c6cULL, - 0xc0ac29b7c97c50ddULL, 0x3f84d5b5b5470917ULL, - 0x9216d5d98979fb1bULL, 0xd1310ba698dfb5acULL, - 0x2ffd72dbd01adfb7ULL, 0xb8e1afed6a267e96ULL, - 0xba7c9045f12c7f99ULL, 0x24a19947b3916cf7ULL, - 0x0801f2e2858efc16ULL, 0x636920d871574e69ULL -}; - -#define blake_round_256(a,b,c,d,e) \ - v[a] += (m[blake_sigma[i][e]] ^ blake_u256[blake_sigma[i][e+1]]) + v[b]; \ - v[d] = rotr32( v[d] ^ v[a],16); \ - v[c] += v[d]; \ - v[b] = rotr32( v[b] ^ v[c],12); \ - v[a] += (m[blake_sigma[i][e+1]] ^ blake_u256[blake_sigma[i][e]])+v[b]; \ - v[d] = rotr32( v[d] ^ v[a], 8); \ - v[c] += v[d]; \ - v[b] = rotr32( v[b] ^ v[c], 7); - -void compress256(uint32_t* hash, char *block, uint64_t compressed){ - uint32_t v[16], m[16], i; - - for(int i = 0; i < 16; i++) m[i] = wtf((block + i * 4)); - - for(int i = 0; i < 8; i++) v[i] = hash[i]; - - for(int i = 0; i != 8; i++) - v[i + 8] = blake_u256[i]; - - v[12] ^= (uint32_t)compressed; - v[13] ^= (uint32_t)compressed; - v[14] ^= compressed >> 32; - v[15] ^= compressed >> 32; - - for(int i = 0; i < 14; i++){ - blake_round_256( 0, 4, 8, 12, 0 ); - blake_round_256( 1, 5, 9, 13, 2 ); - blake_round_256( 2, 6, 10, 14, 4 ); - blake_round_256( 3, 7, 11, 15, 6 ); - - blake_round_256( 0, 5, 10, 15, 8 ); - blake_round_256( 1, 6, 11, 12, 10 ); - blake_round_256( 2, 7, 8, 13, 12 ); - blake_round_256( 3, 4, 9, 14, 14 ); - } - - for(int i = 0; i < 16; i++) hash[i % 8] ^= v[i]; - - //for(int i = 0; i != 8; i++) printf("%lx ", hash[i]); - //printf("\n"); -} - -#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 blake256_init_l(lua_State* L){ - struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs); - a.hash = lua_newuserdata(L, 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 blake224_init_l(lua_State* L){ - struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs); - a.hash = lua_newuserdata(L, 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; -} - -void blake256_round(struct blake256_hash* hash){ - compress256(hash->hash, (char*)hash->buffer, hash->compressed * 8); -} - -#define blake224_update blake256_update -void blake256_update(uint8_t* input, size_t len, struct blake256_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; - } - - 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; - blake256_round(hash); - } - - memset(hash->buffer, 0, bs); - - if(0 != total_add){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } -} - -void _blake256_final(struct blake256_hash* hash, char* out_stream){ - hash->compressed += hash->bufflen; - - hash->buffer[hash->bufflen] = 0x80; - - if(hash->bufflen > 55) { - //too large, needs another buffer - memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen); - blake256_round(hash); - hash->compressed = 0; - memset(hash->buffer, 0, 64); - } - - size_t lhhh = 8*hash->total; - U32TO8_BIG(hash->buffer + bs - 8, 0x0); - U32TO8_BIG(hash->buffer + bs - 4, hash->total << 3); - /*for(int i = 0; i != bs; i++) printf("%x ", hash->buffer[i]); - printf("\n");*/ - blake256_round(hash); -} - -void blake256_final(struct blake256_hash* hash, char* out_stream){ - uint8_t old[bs]; - struct blake256_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); - - if(hash->bufflen == 55) hash->buffer[hash->bufflen] = 0x81; - else hash->buffer[bs - 9] = 0x01; - - _blake256_final(hash, out_stream); - - for(int i = 0; i != 8; i++){ - sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]); - } - - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); -} - -void blake224_final(struct blake256_hash* hash, char* out_stream){ - uint8_t old[bs]; - struct blake256_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); - - if(hash->bufflen == 55) hash->buffer[hash->bufflen] = 0x80; - else hash->buffer[bs - 9] = 0x00; - - _blake256_final(hash, out_stream); - - for(int i = 0; i != 7; i++){ - sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]); - } - - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); -} - -void blake256(char *out, char *in, uint64_t inlen){ - struct blake256_hash a = blake256_init(); - blake256_update((uint8_t*)in, inlen, &a); - blake256_final(&a, out); - free(a.buffer); - free(a.hash); -} - -void blake224(char *out, char *in, uint64_t inlen){ - struct blake224_hash a = blake224_init(); - blake224_update((uint8_t*)in, inlen, &a); - blake224_final(&a, out); - free(a.buffer); - free(a.hash); -} - -#define blake_round_512(a,b,c,d,e) \ - v[a] += (m[blake_sigma[i][e]] ^ blake_u512[blake_sigma[i][e+1]]) + v[b];\ - v[d] = rotr64( v[d] ^ v[a],32); \ - v[c] += v[d]; \ - v[b] = rotr64( v[b] ^ v[c],25); \ - v[a] += (m[blake_sigma[i][e+1]] ^ blake_u512[blake_sigma[i][e]])+v[b]; \ - v[d] = rotr64( v[d] ^ v[a],16); \ - v[c] += v[d]; \ - v[b] = rotr64( v[b] ^ v[c],11); - -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(int i = 0; i < 8; i++) v[i] = hash[i]; - - for(int i = 0; i != 8; i++) - v[i + 8] = blake_u512[i]; - - v[12] ^= compressed; - v[13] ^= compressed; - v[14] ^= 0; - v[15] ^= 0; - - - for(i = 0; i < 16; i++){ - blake_round_512(0, 4, 8, 12, 0); - blake_round_512(1, 5, 9, 13, 2); - blake_round_512(2, 6, 10, 14, 4); - blake_round_512(3, 7, 11, 15, 6); - - blake_round_512(0, 5, 10, 15, 8); - 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]; -} - -//#undef bs -#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 blake512_init_l(lua_State* L){ - struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2); - a.hash = lua_newuserdata(L, 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 blake384_init_l(lua_State* L){ - struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0}; - a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2); - a.hash = lua_newuserdata(L, 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; -} - -void blake512_round(struct blake512_hash* hash){ - compress512(hash->hash, hash->buffer, hash->compressed * 8); -} - -#define blake384_update blake512_update -void blake512_update(uint8_t* input, size_t len, struct blake512_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; - } - - 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 += 128; - blake512_round(hash); - } - - memset(hash->buffer, 0, bs_2); - - if(0 != total_add){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } -} - -void _blake512_final(struct blake512_hash* hash, char* out_stream){ - hash->compressed += hash->bufflen; - - hash->buffer[hash->bufflen] = 0x80; - - if(hash->bufflen > bs_2 - 16) { - //too large, needs another buffer - memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen); - blake512_round(hash); - hash->compressed = 0; - memset(hash->buffer, 0, 64); - } - - size_t lhhh = 8*hash->total; - U64TO8_BIG(hash->buffer + bs_2 - 8, hash->total << 3); - - blake512_round(hash); -} - -void blake512_final(struct blake512_hash* hash, char* out_stream){ - uint8_t old[bs_2]; - struct blake512_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs_2); - - if(hash->bufflen == 111) hash->buffer[hash->bufflen] = 0x81; - else hash->buffer[bs_2 - 17] = 0x01; - - _blake512_final(hash, out_stream); - - for(int i = 0; i != 8; i++){ - sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]); - } - - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs_2); -} - -void blake512(uint8_t* in, size_t len, char* out){ - struct blake512_hash a = blake512_init(); - blake512_update(in, len, &a); - blake512_final(&a, out); - free(a.buffer); - free(a.hash); -} - -void blake384_final(struct blake384_hash* hash, char* out_stream){ - uint8_t old[bs_2]; - struct blake384_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs_2); - - if(hash->bufflen == 111) hash->buffer[hash->bufflen] = 0x80; - else hash->buffer[bs_2 - 17] = 0x00; - - _blake512_final(hash, out_stream); - - for(int i = 0; i != 6; i++){ - sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]); - } - - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs_2); -} - -void blake384(uint8_t* in, size_t len, char* out){ - struct blake384_hash a = blake384_init(); - blake384_update(in, len, &a); - blake384_final(&a, out); - free(a.buffer); - free(a.hash); -} - -int l_blake256_clone(lua_State* L){ - struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, -1); - l_blake256_init(L); - struct blake256_hash* b = (struct blake256_hash*)lua_touserdata(L, -1); - - memcpy(b->hash, a->hash, 8 * sizeof * b->hash); - memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); - b->total = a->total; - b->bufflen = a->bufflen; - b->compressed = a->compressed; - b->total = a->total; - return 1; -} - -common_hash_init_update(blake256); - -int l_blake256_final(lua_State* L){ - struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, 1); - - char digest[257] = {0}; - blake256_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -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); - int argv = lua_gettop(L); - - char digest[257] = {0}; - - blake256(digest, (char*)a, len); - - lua_pushstring(L, digest); - - return 1; -} - -int l_blake224_clone(lua_State* L){ - struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, -1); - l_blake224_init(L); - struct blake224_hash* b = (struct blake224_hash*)lua_touserdata(L, -1); - - memcpy(b->hash, a->hash, 8 * sizeof * b->hash); - memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer); - b->total = a->total; - b->bufflen = a->bufflen; - b->compressed = a->compressed; - b->total = a->total; - return 1; -} - -common_hash_init_update(blake224); - -int l_blake224_final(lua_State* L){ - struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, 1); - - char digest[257] = {0}; - blake224_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -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); - int argv = lua_gettop(L); - - char digest[257] = {0}; - - blake224(digest, (char*)a, len); - - lua_pushstring(L, digest); - - return 1; -} - -int l_blake512_clone(lua_State* L){ - struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, -1); - l_blake512_init(L); - struct blake512_hash* b = (struct blake512_hash*)lua_touserdata(L, -1); - - memcpy(b->hash, a->hash, 8 * sizeof * b->hash); - memcpy(b->buffer, a->buffer, bs_2 * sizeof * b->buffer); - b->total = a->total; - b->bufflen = a->bufflen; - b->compressed = a->compressed; - b->total = a->total; - return 1; -} - -common_hash_init_update(blake512); - -int l_blake512_final(lua_State* L){ - struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, 1); - - char digest[513] = {0}; - blake512_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -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); - int argv = lua_gettop(L); - - char digest[513] = {0}; - //memset(digest, 0, 513); - - //blake512(digest, a, len, b512); - blake512(a, len, digest); - lua_pushstring(L, digest); - - return 1; -} - -int l_blake384_clone(lua_State* L){ - struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, -1); - l_blake384_init(L); - struct blake384_hash* b = (struct blake384_hash*)lua_touserdata(L, -1); - - memcpy(b->hash, a->hash, 8 * sizeof * b->hash); - memcpy(b->buffer, a->buffer, bs_2 * sizeof * b->buffer); - b->total = a->total; - b->bufflen = a->bufflen; - b->compressed = a->compressed; - b->total = a->total; - return 1; -} - -common_hash_init_update(blake384); - -int l_blake384_final(lua_State* L){ - struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, 1); - - char digest[513] = {0}; - blake384_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -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); - int argv = lua_gettop(L); - - char digest[513] = {0}; - - blake384(a, len, digest); - lua_pushstring(L, digest); - - return 1; -} +#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <math.h>
+#include <stdlib.h>
+#include "../crypto.h"
+#include "../util.h"
+
+const uint8_t blake_sigma[][16] = {
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
+ {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
+ {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
+ { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
+ { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
+ { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
+ {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
+ {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
+ { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
+ {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0 },
+ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
+ {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
+ {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
+ { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
+ { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
+ { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }
+};
+
+const uint32_t blake_u256[16] = {
+ 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344,
+ 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
+ 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c,
+ 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917
+};
+
+const uint64_t blake_u512[16] = {
+ 0x243f6a8885a308d3ULL, 0x13198a2e03707344ULL,
+ 0xa4093822299f31d0ULL, 0x082efa98ec4e6c89ULL,
+ 0x452821e638d01377ULL, 0xbe5466cf34e90c6cULL,
+ 0xc0ac29b7c97c50ddULL, 0x3f84d5b5b5470917ULL,
+ 0x9216d5d98979fb1bULL, 0xd1310ba698dfb5acULL,
+ 0x2ffd72dbd01adfb7ULL, 0xb8e1afed6a267e96ULL,
+ 0xba7c9045f12c7f99ULL, 0x24a19947b3916cf7ULL,
+ 0x0801f2e2858efc16ULL, 0x636920d871574e69ULL
+};
+
+#define blake_round_256(a,b,c,d,e) \
+ v[a] += (m[blake_sigma[i][e]] ^ blake_u256[blake_sigma[i][e+1]]) + v[b]; \
+ v[d] = rotr32( v[d] ^ v[a],16); \
+ v[c] += v[d]; \
+ v[b] = rotr32( v[b] ^ v[c],12); \
+ v[a] += (m[blake_sigma[i][e+1]] ^ blake_u256[blake_sigma[i][e]])+v[b]; \
+ v[d] = rotr32( v[d] ^ v[a], 8); \
+ v[c] += v[d]; \
+ v[b] = rotr32( v[b] ^ v[c], 7);
+
+void compress256(uint32_t* hash, char *block, uint64_t compressed){
+ uint32_t v[16], m[16], i;
+
+ for(int i = 0; i < 16; i++) m[i] = wtf((block + i * 4));
+
+ for(int i = 0; i < 8; i++) v[i] = hash[i];
+
+ for(int i = 0; i != 8; i++)
+ v[i + 8] = blake_u256[i];
+
+ v[12] ^= (uint32_t)compressed;
+ v[13] ^= (uint32_t)compressed;
+ v[14] ^= compressed >> 32;
+ v[15] ^= compressed >> 32;
+
+ for(int i = 0; i < 14; i++){
+ blake_round_256( 0, 4, 8, 12, 0 );
+ blake_round_256( 1, 5, 9, 13, 2 );
+ blake_round_256( 2, 6, 10, 14, 4 );
+ blake_round_256( 3, 7, 11, 15, 6 );
+
+ blake_round_256( 0, 5, 10, 15, 8 );
+ blake_round_256( 1, 6, 11, 12, 10 );
+ blake_round_256( 2, 7, 8, 13, 12 );
+ blake_round_256( 3, 4, 9, 14, 14 );
+ }
+
+ for(int i = 0; i < 16; i++) hash[i % 8] ^= v[i];
+
+ //for(int i = 0; i != 8; i++) printf("%lx ", hash[i]);
+ //printf("\n");
+}
+
+#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 blake256_init_l(lua_State* L){
+ struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs);
+ a.hash = lua_newuserdata(L, 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 blake224_init_l(lua_State* L){
+ struct blake256_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs);
+ a.hash = lua_newuserdata(L, 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;
+}
+
+void blake256_round(struct blake256_hash* hash){
+ compress256(hash->hash, (char*)hash->buffer, hash->compressed * 8);
+}
+
+#define blake224_update blake256_update
+void blake256_update(uint8_t* input, size_t len, struct blake256_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;
+ }
+
+ 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;
+ blake256_round(hash);
+ }
+
+ memset(hash->buffer, 0, bs);
+
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
+}
+
+void _blake256_final(struct blake256_hash* hash, char* out_stream){
+ hash->compressed += hash->bufflen;
+
+ hash->buffer[hash->bufflen] = 0x80;
+
+ if(hash->bufflen > 55) {
+ //too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen);
+ blake256_round(hash);
+ hash->compressed = 0;
+ memset(hash->buffer, 0, 64);
+ }
+
+ size_t lhhh = 8*hash->total;
+ U32TO8_BIG(hash->buffer + bs - 8, 0x0);
+ U32TO8_BIG(hash->buffer + bs - 4, hash->total << 3);
+ /*for(int i = 0; i != bs; i++) printf("%x ", hash->buffer[i]);
+ printf("\n");*/
+ blake256_round(hash);
+}
+
+void blake256_final(struct blake256_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct blake256_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
+ if(hash->bufflen == 55) hash->buffer[hash->bufflen] = 0x81;
+ else hash->buffer[bs - 9] = 0x01;
+
+ _blake256_final(hash, out_stream);
+
+ for(int i = 0; i != 8; i++){
+ sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]);
+ }
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
+}
+
+void blake224_final(struct blake256_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct blake256_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
+ if(hash->bufflen == 55) hash->buffer[hash->bufflen] = 0x80;
+ else hash->buffer[bs - 9] = 0x00;
+
+ _blake256_final(hash, out_stream);
+
+ for(int i = 0; i != 7; i++){
+ sprintf(out_stream, "%s%08x",out_stream,(hash->hash)[i]);
+ }
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
+}
+
+void blake256(char *out, char *in, uint64_t inlen){
+ struct blake256_hash a = blake256_init();
+ blake256_update((uint8_t*)in, inlen, &a);
+ blake256_final(&a, out);
+ free(a.buffer);
+ free(a.hash);
+}
+
+void blake224(char *out, char *in, uint64_t inlen){
+ struct blake224_hash a = blake224_init();
+ blake224_update((uint8_t*)in, inlen, &a);
+ blake224_final(&a, out);
+ free(a.buffer);
+ free(a.hash);
+}
+
+#define blake_round_512(a,b,c,d,e) \
+ v[a] += (m[blake_sigma[i][e]] ^ blake_u512[blake_sigma[i][e+1]]) + v[b];\
+ v[d] = rotr64( v[d] ^ v[a],32); \
+ v[c] += v[d]; \
+ v[b] = rotr64( v[b] ^ v[c],25); \
+ v[a] += (m[blake_sigma[i][e+1]] ^ blake_u512[blake_sigma[i][e]])+v[b]; \
+ v[d] = rotr64( v[d] ^ v[a],16); \
+ v[c] += v[d]; \
+ v[b] = rotr64( v[b] ^ v[c],11);
+
+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(int i = 0; i < 8; i++) v[i] = hash[i];
+
+ for(int i = 0; i != 8; i++)
+ v[i + 8] = blake_u512[i];
+
+ v[12] ^= compressed;
+ v[13] ^= compressed;
+ v[14] ^= 0;
+ v[15] ^= 0;
+
+
+ for(i = 0; i < 16; i++){
+ blake_round_512(0, 4, 8, 12, 0);
+ blake_round_512(1, 5, 9, 13, 2);
+ blake_round_512(2, 6, 10, 14, 4);
+ blake_round_512(3, 7, 11, 15, 6);
+
+ blake_round_512(0, 5, 10, 15, 8);
+ 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];
+}
+
+//#undef bs
+#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 blake512_init_l(lua_State* L){
+ struct blake512_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2);
+ a.hash = lua_newuserdata(L, 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 blake384_init_l(lua_State* L){
+ struct blake384_hash a = {.bufflen = 0, .total = 0, .compressed = 0};
+ a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs_2);
+ a.hash = lua_newuserdata(L, 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;
+}
+
+void blake512_round(struct blake512_hash* hash){
+ compress512(hash->hash, hash->buffer, hash->compressed * 8);
+}
+
+#define blake384_update blake512_update
+void blake512_update(uint8_t* input, size_t len, struct blake512_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;
+ }
+
+ 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 += 128;
+ blake512_round(hash);
+ }
+
+ memset(hash->buffer, 0, bs_2);
+
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
+}
+
+void _blake512_final(struct blake512_hash* hash, char* out_stream){
+ hash->compressed += hash->bufflen;
+
+ hash->buffer[hash->bufflen] = 0x80;
+
+ if(hash->bufflen > bs_2 - 16) {
+ //too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen);
+ blake512_round(hash);
+ hash->compressed = 0;
+ memset(hash->buffer, 0, 64);
+ }
+
+ size_t lhhh = 8*hash->total;
+ U64TO8_BIG(hash->buffer + bs_2 - 8, hash->total << 3);
+
+ blake512_round(hash);
+}
+
+void blake512_final(struct blake512_hash* hash, char* out_stream){
+ uint8_t old[bs_2];
+ struct blake512_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs_2);
+
+ if(hash->bufflen == 111) hash->buffer[hash->bufflen] = 0x81;
+ else hash->buffer[bs_2 - 17] = 0x01;
+
+ _blake512_final(hash, out_stream);
+
+ for(int i = 0; i != 8; i++){
+ sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]);
+ }
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs_2);
+}
+
+void blake512(uint8_t* in, size_t len, char* out){
+ struct blake512_hash a = blake512_init();
+ blake512_update(in, len, &a);
+ blake512_final(&a, out);
+ free(a.buffer);
+ free(a.hash);
+}
+
+void blake384_final(struct blake384_hash* hash, char* out_stream){
+ uint8_t old[bs_2];
+ struct blake384_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs_2);
+
+ if(hash->bufflen == 111) hash->buffer[hash->bufflen] = 0x80;
+ else hash->buffer[bs_2 - 17] = 0x00;
+
+ _blake512_final(hash, out_stream);
+
+ for(int i = 0; i != 6; i++){
+ sprintf(out_stream, "%s%016llx",out_stream, (hash->hash)[i]);
+ }
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs_2);
+}
+
+void blake384(uint8_t* in, size_t len, char* out){
+ struct blake384_hash a = blake384_init();
+ blake384_update(in, len, &a);
+ blake384_final(&a, out);
+ free(a.buffer);
+ free(a.hash);
+}
+
+int l_blake256_clone(lua_State* L){
+ struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, -1);
+ l_blake256_init(L);
+ struct blake256_hash* b = (struct blake256_hash*)lua_touserdata(L, -1);
+
+ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+ b->total = a->total;
+ b->bufflen = a->bufflen;
+ b->compressed = a->compressed;
+ b->total = a->total;
+ return 1;
+}
+
+common_hash_init_update(blake256);
+
+int l_blake256_final(lua_State* L){
+ struct blake256_hash* a = (struct blake256_hash*)lua_touserdata(L, 1);
+
+ char digest[257] = {0};
+ blake256_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ int argv = lua_gettop(L);
+
+ char digest[257] = {0};
+
+ blake256(digest, (char*)a, len);
+
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+int l_blake224_clone(lua_State* L){
+ struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, -1);
+ l_blake224_init(L);
+ struct blake224_hash* b = (struct blake224_hash*)lua_touserdata(L, -1);
+
+ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
+ memcpy(b->buffer, a->buffer, bs * sizeof * b->buffer);
+ b->total = a->total;
+ b->bufflen = a->bufflen;
+ b->compressed = a->compressed;
+ b->total = a->total;
+ return 1;
+}
+
+common_hash_init_update(blake224);
+
+int l_blake224_final(lua_State* L){
+ struct blake224_hash* a = (struct blake224_hash*)lua_touserdata(L, 1);
+
+ char digest[257] = {0};
+ blake224_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ int argv = lua_gettop(L);
+
+ char digest[257] = {0};
+
+ blake224(digest, (char*)a, len);
+
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+int l_blake512_clone(lua_State* L){
+ struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, -1);
+ l_blake512_init(L);
+ struct blake512_hash* b = (struct blake512_hash*)lua_touserdata(L, -1);
+
+ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
+ memcpy(b->buffer, a->buffer, bs_2 * sizeof * b->buffer);
+ b->total = a->total;
+ b->bufflen = a->bufflen;
+ b->compressed = a->compressed;
+ b->total = a->total;
+ return 1;
+}
+
+common_hash_init_update(blake512);
+
+int l_blake512_final(lua_State* L){
+ struct blake512_hash* a = (struct blake512_hash*)lua_touserdata(L, 1);
+
+ char digest[513] = {0};
+ blake512_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ int argv = lua_gettop(L);
+
+ char digest[513] = {0};
+ //memset(digest, 0, 513);
+
+ //blake512(digest, a, len, b512);
+ blake512(a, len, digest);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+int l_blake384_clone(lua_State* L){
+ struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, -1);
+ l_blake384_init(L);
+ struct blake384_hash* b = (struct blake384_hash*)lua_touserdata(L, -1);
+
+ memcpy(b->hash, a->hash, 8 * sizeof * b->hash);
+ memcpy(b->buffer, a->buffer, bs_2 * sizeof * b->buffer);
+ b->total = a->total;
+ b->bufflen = a->bufflen;
+ b->compressed = a->compressed;
+ b->total = a->total;
+ return 1;
+}
+
+common_hash_init_update(blake384);
+
+int l_blake384_final(lua_State* L){
+ struct blake384_hash* a = (struct blake384_hash*)lua_touserdata(L, 1);
+
+ char digest[513] = {0};
+ blake384_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ int argv = lua_gettop(L);
+
+ char digest[513] = {0};
+
+ blake384(a, len, digest);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/blake.h b/src/hash/blake.h index e0aac85..07caf3d 100644 --- a/src/hash/blake.h +++ b/src/hash/blake.h @@ -1,55 +1,55 @@ -#include <stdint.h> -#include "../lua.h" - -#define U8TO32_BIG(p) \ - (((uint32_t)((p)[0]) << 24) | ((uint32_t)((p)[1]) << 16) | \ - ((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3]) )) - -#define U32TO8_BIG(p, v) \ - (p)[0] = (uint8_t)((v) >> 24); (p)[1] = (uint8_t)((v) >> 16); \ - (p)[2] = (uint8_t)((v) >> 8); (p)[3] = (uint8_t)((v) ); - -#define U8TO64_BIG(p) \ - (((uint64_t)U8TO32_BIG(p) << 32) | (uint64_t)U8TO32_BIG((p) + 4)) - -#define U64TO8_BIG(p, v) \ - U32TO8_BIG((p), (uint32_t)((v) >> 32)); \ - U32TO8_BIG((p) + 4, (uint32_t)((v) )); - -#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; -}; -#define blake224_hash blake256_hash - -struct blake512_hash { - uint8_t* buffer; - size_t bufflen; - uint64_t total, *hash; - uint64_t compressed; -}; -#define blake384_hash blake512_hash - -int l_blake256(lua_State* L); -int l_blake256_init(lua_State* L); -int l_blake256_update(lua_State* L); -int l_blake256_final(lua_State* L); - -int l_blake224(lua_State* L); -int l_blake224_init(lua_State* L); -int l_blake224_update(lua_State* L); -int l_blake224_final(lua_State* L); - -int l_blake512(lua_State* L); -int l_blake512_init(lua_State* L); -int l_blake512_update(lua_State* L); -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); +#include <stdint.h>
+#include "../lua.h"
+
+#define U8TO32_BIG(p) \
+ (((uint32_t)((p)[0]) << 24) | ((uint32_t)((p)[1]) << 16) | \
+ ((uint32_t)((p)[2]) << 8) | ((uint32_t)((p)[3]) ))
+
+#define U32TO8_BIG(p, v) \
+ (p)[0] = (uint8_t)((v) >> 24); (p)[1] = (uint8_t)((v) >> 16); \
+ (p)[2] = (uint8_t)((v) >> 8); (p)[3] = (uint8_t)((v) );
+
+#define U8TO64_BIG(p) \
+ (((uint64_t)U8TO32_BIG(p) << 32) | (uint64_t)U8TO32_BIG((p) + 4))
+
+#define U64TO8_BIG(p, v) \
+ U32TO8_BIG((p), (uint32_t)((v) >> 32)); \
+ U32TO8_BIG((p) + 4, (uint32_t)((v) ));
+
+#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;
+};
+#define blake224_hash blake256_hash
+
+struct blake512_hash {
+ uint8_t* buffer;
+ size_t bufflen;
+ uint64_t total, *hash;
+ uint64_t compressed;
+};
+#define blake384_hash blake512_hash
+
+int l_blake256(lua_State* L);
+int l_blake256_init(lua_State* L);
+int l_blake256_update(lua_State* L);
+int l_blake256_final(lua_State* L);
+
+int l_blake224(lua_State* L);
+int l_blake224_init(lua_State* L);
+int l_blake224_update(lua_State* L);
+int l_blake224_final(lua_State* L);
+
+int l_blake512(lua_State* L);
+int l_blake512_init(lua_State* L);
+int l_blake512_update(lua_State* L);
+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 diff --git a/src/hash/blake2.c b/src/hash/blake2.c index 3dc44e1..be1cb51 100644 --- a/src/hash/blake2.c +++ b/src/hash/blake2.c @@ -86,9 +86,6 @@ void compress2b(uint64_t* hash, uint8_t* inp, uint64_t compressed, int final){ }
void compress2s(uint32_t* hash, uint8_t* inp, uint32_t compressed, int final){
- printf("block:\n");
- for(int i = 0; i != 64; i++) printf("%x ", inp[i]);
- printf("\n");
uint32_t v[16], s[16], m[16];
#pragma unroll
@@ -304,49 +301,6 @@ void blake2s(uint8_t* inp, int len, char* key, int key_len, int dig_len, char* o free(aa.key);
}
-void _blake2s(char* inp, int inp_len, char* key, int key_len, int dig_len, char* buffer){
- uint32_t hash[8];
-
- uint32_t iv0 = hash[0] = sha512_iv.h0 >> 32;
- uint32_t iv1 = hash[1] = sha512_iv.h1 >> 32;
- uint32_t iv2 = hash[2] = sha512_iv.h2 >> 32;
- uint32_t iv3 = hash[3] = sha512_iv.h3 >> 32;
- uint32_t iv4 = hash[4] = sha512_iv.h4 >> 32;
- uint32_t iv5 = hash[5] = sha512_iv.h5 >> 32;
- uint32_t iv6 = hash[6] = sha512_iv.h6 >> 32;
- uint32_t iv7 = hash[7] = sha512_iv.h7 >> 32;
-
- uint32_t alen = inter(inp_len, 64) + 64;
-
- //add padding
- char* padded = calloc(alen + (64 * (key_len > 0)), sizeof * padded);
- if(key_len > 0){
- memcpy(padded, key, key_len);
- inp_len += 64;
- }
- memcpy(padded + (64 * (key_len > 0)), inp, inp_len - (64 * (key_len > 0)));
-
- hash[0] ^= dig_len;
- hash[0] ^= key_len << 8;
- hash[0] ^= 0x01010000;
-
- uint64_t compressed = 0, bytes_remaining = inp_len;
-
- int i = 0;
- for(;bytes_remaining > 64; i += 2){
- bytes_remaining -= 64;
- compressed += 64;
-
- compress2s(hash, (uint8_t*)padded, compressed, 0);
- padded += 64;
- }
-
- compressed += bytes_remaining;
-
- compress2s(hash, (uint8_t*)padded, compressed, 1);
- for(int i = 0; i != dig_len; i++)sprintf(buffer, "%s%02x", buffer, (((uint8_t*)hash)[i]));
-}
-
int l_blake2b(lua_State* L){
size_t len = 0;
char* a = (char*)luaL_checklstring(L, 1, &len);
diff --git a/src/hash/bsdchecksum.c b/src/hash/bsdchecksum.c index 3fbbe77..ccb1e65 100644 --- a/src/hash/bsdchecksum.c +++ b/src/hash/bsdchecksum.c @@ -1,54 +1,54 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct bsdchecksum_hash bsdchecksum_init(){ - return (struct bsdchecksum_hash){.check = 0x0}; -} - -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; - } -} - -uint16_t bsdchecksum_final(struct bsdchecksum_hash* hash){ - return hash->check; -} - -uint16_t bsdchecksum(uint8_t* a, size_t len){ - struct bsdchecksum_hash b = bsdchecksum_init(); - bsdchecksum_update(a, len, &b); - return bsdchecksum_final(&b); -} - -common_hash_clone(bsdchecksum); - -common_hash_init_update(bsdchecksum); - -int l_bsdchecksum_final(lua_State* L){ - struct bsdchecksum_hash* a = (struct bsdchecksum_hash*)lua_touserdata(L, 1); - uint32_t u = bsdchecksum_final(a); - char digest[32]; - sprintf(digest,"%i",u); - lua_pushstring(L, digest); - return 1; -} - -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); - uint16_t u = bsdchecksum(a, len); - sprintf(digest,"%i",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct bsdchecksum_hash bsdchecksum_init(){
+ return (struct bsdchecksum_hash){.check = 0x0};
+}
+
+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;
+ }
+}
+
+uint16_t bsdchecksum_final(struct bsdchecksum_hash* hash){
+ return hash->check;
+}
+
+uint16_t bsdchecksum(uint8_t* a, size_t len){
+ struct bsdchecksum_hash b = bsdchecksum_init();
+ bsdchecksum_update(a, len, &b);
+ return bsdchecksum_final(&b);
+}
+
+common_hash_clone(bsdchecksum);
+
+common_hash_init_update(bsdchecksum);
+
+int l_bsdchecksum_final(lua_State* L){
+ struct bsdchecksum_hash* a = (struct bsdchecksum_hash*)lua_touserdata(L, 1);
+ uint32_t u = bsdchecksum_final(a);
+ char digest[32];
+ sprintf(digest,"%i",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ uint16_t u = bsdchecksum(a, len);
+ sprintf(digest,"%i",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/cityhash.c b/src/hash/cityhash.c index f32453d..f6f419a 100644 --- a/src/hash/cityhash.c +++ b/src/hash/cityhash.c @@ -1,430 +1,430 @@ -#include <stdio.h> -#include <stdint.h> -#include "cityhash.h" - -uint32_t rot32(uint32_t val, int shift) { - return ((val >> shift) | (val << (32 - shift))); -} - -uint32_t fmix(uint32_t h){ - h ^= h >> 16; - h *= 0x85ebca6b; - h ^= h >> 13; - h *= 0xc2b2ae35; - h ^= h >> 16; - return h; -} - -uint32_t mur(uint32_t a, uint32_t h) { - a *= c1; - a = rot32(a, 17); - a *= c2; - h ^= a; - h = rot32(h, 19); - return h * 5 + 0xe6546b64; -} - -uint32_t hash32len0to4(uint8_t* in, size_t len){ - uint32_t b = 0, c = 9; - for(int i = 0; i != len; i++){ - b = b * c1 + (uint32_t)in[i]; - c ^= b; - } - return fmix(mur(b, mur((uint32_t)len, c))); -} - -uint32_t UNALIGNED_LOAD32(uint8_t *p) { - return *(uint32_t*)p; - /* original google code:p - uint32_t result; - memcpy(&result, p, sizeof(result)); - return result; - */ -} - -uint32_t hash32len5to12(uint8_t* in, size_t len){ - uint32_t a = (uint32_t)(len), b = a * 5, c = 9, d = b; - a += UNALIGNED_LOAD32(in); - b += UNALIGNED_LOAD32(in + len - 4); - c += UNALIGNED_LOAD32(in + ((len >> 1) & 4)); - return fmix(mur(c, mur(b, mur(a, d)))); -} - -uint32_t hash32len13to24(uint8_t* in, size_t len){ - uint32_t a = UNALIGNED_LOAD32(in - 4 + (len >> 1)); - uint32_t b = UNALIGNED_LOAD32(in + 4); - uint32_t c = UNALIGNED_LOAD32(in + len - 8); - uint32_t d = UNALIGNED_LOAD32(in + (len >> 1)); - uint32_t e = UNALIGNED_LOAD32(in); - uint32_t f = UNALIGNED_LOAD32(in + len - 4); - uint32_t h = (uint32_t)len; - - return fmix(mur(f, mur(e, mur(d, mur(c, mur(b, mur(a, h))))))); -} - -uint32_t cityhash32(uint8_t* in, size_t len){ - if(len <= 24){ - if(len <= 12){ - if(len <= 4) return hash32len0to4(in, len); - else return hash32len5to12(in, len); - }else return hash32len13to24(in, len); - } - - uint32_t h = (uint32_t)len, g = c1 * h, f = g; - uint32_t a0 = rot32(UNALIGNED_LOAD32(in + len - 4) * c1, 17) * c2; - uint32_t a1 = rot32(UNALIGNED_LOAD32(in + len - 8) * c1, 17) * c2; - uint32_t a2 = rot32(UNALIGNED_LOAD32(in + len - 16) * c1, 17) * c2; - uint32_t a3 = rot32(UNALIGNED_LOAD32(in + len - 12) * c1, 17) * c2; - uint32_t a4 = rot32(UNALIGNED_LOAD32(in + len - 20) * c1, 17) * c2; - h ^= a0; - h = rot32(h, 19); - h = h * 5 + 0xe6546b64; - h ^= a2; - h = rot32(h, 19); - h = h * 5 + 0xe6546b64; - g ^= a1; - g = rot32(g, 19); - g = g * 5 + 0xe6546b64; - g ^= a3; - g = rot32(g, 19); - g = g * 5 + 0xe6546b64; - f += a4; - f = rot32(f, 19); - f = f * 5 + 0xe6546b64; - - for(int i = (len - 1)/20; i != 0; i--){ - uint32_t a0 = rot32(UNALIGNED_LOAD32(in) * c1, 17) * c2; - uint32_t a1 = UNALIGNED_LOAD32(in + 4); - uint32_t a2 = rot32(UNALIGNED_LOAD32(in + 8) * c1, 17) * c2; - uint32_t a3 = rot32(UNALIGNED_LOAD32(in + 12) * c1, 17) * c2; - uint32_t a4 = UNALIGNED_LOAD32(in + 16); - h ^= a0; - h = rot32(h, 18); - h = h * 5 + 0xe6546b64; - f += a1; - f = rot32(f, 19); - f = f * c1; - g += a2; - g = rot32(g, 18); - g = g * 5 + 0xe6546b64; - h ^= a3 + a1; - h = rot32(h, 19); - h = h * 5 + 0xe6546b64; - g ^= a4; - g = __builtin_bswap32(g) * 5; - h += a4 * 5; - h = __builtin_bswap32(h); - f += a0; - //PERMUTE3(f, h, g); - uint32_t temp = f; - f = h; h = temp; - temp = f; - f = g; g = temp; - // - in += 20; - } - - g = rot32(g, 11) * c1; - g = rot32(g, 17) * c1; - f = rot32(f, 11) * c1; - f = rot32(f, 17) * c1; - h = rot32(h + g, 19); - h = h * 5 + 0xe6546b64; - h = rot32(h, 17) * c1; - h = rot32(h + f, 19); - h = h * 5 + 0xe6546b64; - h = rot32(h, 17) * c1; - - return h; -} - -//64 version - -uint64_t UNALIGNED_LOAD64(uint8_t *p) { - return *(uint64_t*)p; - /* - uint64_t result; - memcpy(&result, p, sizeof(result)); - return result; - //*/ -} - -uint64_t rot64(uint64_t val, int shift) { - return ((val >> shift) | (val << (64 - shift))); -} - -uint64_t hashlen16(uint64_t u, uint64_t v, uint64_t mul) { - uint64_t a = (u ^ v) * mul; - a ^= (a >> 47); - uint64_t b = (v ^ a) * mul; - b ^= (b >> 47); - b *= mul; - return b; -} - -uint64_t shiftmix(uint64_t val) { - return val ^ (val >> 47); -} - -uint64_t hashlen0to16(uint8_t* in, size_t len){ - if(len >= 8){ - uint64_t mul = k2 + len * 2; - uint64_t a = UNALIGNED_LOAD64(in) + k2; - uint64_t b = UNALIGNED_LOAD64(in + len - 8); - uint64_t c = rot64(b, 37) * mul + a; - uint64_t d = (rot64(a, 25) + b) * mul; - return hashlen16(c, d, mul); - } - if(len >= 4){ - uint64_t mul = k2 + len * 2; - uint64_t a = UNALIGNED_LOAD32(in); - return hashlen16(len + (a << 3), UNALIGNED_LOAD32(in + len - 4), mul); - } - if(len > 0){ - uint8_t a = (uint8_t)in[0]; - uint8_t b = (uint8_t)in[len >> 1]; - uint8_t c = (uint8_t)in[len - 1]; - uint32_t y = ((uint32_t)a) + (((uint32_t)b) << 8); - uint32_t z = ((uint32_t)len) + ((uint32_t)(c) << 2); - return shiftmix(y * k2 ^ z * k0) * k2; - } - return k2; -} -uint64_t hashlen17to32(uint8_t* in, size_t len){ - uint64_t mul = k2 + len * 2; - uint64_t a = UNALIGNED_LOAD64(in) * k1; - uint64_t b = UNALIGNED_LOAD64(in + 8); - uint64_t c = UNALIGNED_LOAD64(in + len - 8) * mul; - uint64_t d = UNALIGNED_LOAD64(in + len - 16) * k2; - return hashlen16(rot64(a + b, 43) + rot64(c, 30) + d, - a + rot64(b + k2, 18) + c, mul); -} - -uint64_t hashlen33to64(uint8_t* in, size_t len){ - uint64_t mul = k2 + len * 2; - uint64_t a = UNALIGNED_LOAD64(in) * k2; - uint64_t b = UNALIGNED_LOAD64(in + 8); - uint64_t c = UNALIGNED_LOAD64(in + len - 24); - uint64_t d = UNALIGNED_LOAD64(in + len - 32); - uint64_t e = UNALIGNED_LOAD64(in + 16) * k2; - uint64_t f = UNALIGNED_LOAD64(in + 24) * 9; - uint64_t g = UNALIGNED_LOAD64(in + len - 8); - uint64_t h = UNALIGNED_LOAD64(in + len - 16) * mul; - uint64_t u = rot64(a + g, 43) + (rot64(b, 30) + c) * 9; - uint64_t v = ((a + g) ^ d) + f + 1; - uint64_t w = __builtin_bswap64((u + v) * mul) + h; - uint64_t x = rot64(e + f, 42) + c; - uint64_t y = (__builtin_bswap64((v + w) * mul) + g) * mul; - uint64_t z = e + f + c; - a = __builtin_bswap64((x + z) * mul + y) + b; - b = shiftmix((z + a) * mul + d + h) * mul; - return b + x; -} - -void WeakHashLen32WithSeeds(uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b, uint64_t*p1, int64_t*p2) { - a += w; - b = rot64(b + a + z, 21); - uint64_t c = a; - a += x; - a += y; - b += rot64(a, 44); - *p1 = a + z; - *p2 = b + c; - //return make_pair(a + z, b + c); -} - -void pWeakHashLen32WithSeeds(uint8_t* s, uint64_t a, uint64_t b, uint64_t* p1, int64_t* p2) { - WeakHashLen32WithSeeds(UNALIGNED_LOAD64(s), UNALIGNED_LOAD64(s + 8), - UNALIGNED_LOAD64(s + 16), UNALIGNED_LOAD64(s + 24), a, b, p1, p2); -} - -uint64_t hash128to64(uint64_t f, uint64_t s) { - uint64_t kMul = 0x9ddfea08eb382d69ULL; - uint64_t a = (f ^ s) * kMul; - a ^= (a >> 47); - uint64_t b = (s ^ a) * kMul; - b ^= (b >> 47); - b *= kMul; - return b; -} - -uint64_t HashLen16_2(uint64_t u, uint64_t v) { - return hash128to64(u, v); -} - -uint64_t cityhash64(uint8_t* in, size_t len){ - if(len <= 32){ - if(len <= 16) return hashlen0to16(in, len); - else return hashlen17to32(in, len); - } else if(len <= 64) return hashlen33to64(in, len); - - uint64_t x = UNALIGNED_LOAD64(in + len - 40); - uint64_t y = UNALIGNED_LOAD64(in + len - 16) + UNALIGNED_LOAD64(in + len - 56); - uint64_t z = HashLen16_2(UNALIGNED_LOAD64(in + len - 48) + len, UNALIGNED_LOAD64(in + len - 24)); - uint64_t v1, w1; - int64_t v2, w2; - pWeakHashLen32WithSeeds(in + len - 64, len, z, &v1, &v2); - pWeakHashLen32WithSeeds(in + len - 32, y + k1, x, &w1, &w2); - x = x * k1 + UNALIGNED_LOAD64(in); - - for(int i = (len - 1) /64; i != 0; i--){ - x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1; - y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1; - x ^= w2; - y += v1 + UNALIGNED_LOAD64(in + 40); - z = rot64(z + w1, 33) * k1; - pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2); - pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2); - //std::swap(z, x); - uint64_t temp = z; - z = x; - x = temp; - in += 64; - } - //printf("%llu %llu %llu\n",x,y,z); - return HashLen16_2(HashLen16_2(v1, w1) + shiftmix(y) * k1 + z, - HashLen16_2(v2, w2) + x); -} - -void citymurmur(uint8_t* in, size_t len, uint64_t f, uint64_t s, uint64_t* o1, uint64_t* o2){ - uint64_t a = f; - uint64_t b = s; - uint64_t c = 0; - uint64_t d = 0; - - if (len <= 16) { - a = shiftmix(a * k1) * k1; - c = b * k1 + hashlen0to16(in, len); - d = shiftmix(a + (len >= 8 ? UNALIGNED_LOAD64(in) : c)); - } else { - c = HashLen16_2(UNALIGNED_LOAD64(in + len - 8) + k1, a); - d = HashLen16_2(b + len, c + UNALIGNED_LOAD64(in + len - 16)); - a += d; - - for(; len > 16; len -=16) { - a ^= shiftmix(UNALIGNED_LOAD64(in) * k1) * k1; - a *= k1; - b ^= a; - c ^= shiftmix(UNALIGNED_LOAD64(in + 8) * k1) * k1; - c *= k1; - d ^= c; - s += 16; - len -= 16; - } - } - a = HashLen16_2(a, c); - b = HashLen16_2(d, b); - //return uint128(a ^ b, HashLen16(b, a)); - *o2 = a ^ b; - *o1 = HashLen16_2(b, a); -} - -void cityhash128withseed(uint8_t* in, size_t len, uint64_t f, uint64_t s, uint64_t* o1, uint64_t* o2){ - if(len < 128){ - citymurmur(in, len, k0, k1, o2, o1); - return; - } - - uint64_t v1, w1; - int64_t v2, w2; - uint64_t x = f; - uint64_t y = s; - uint64_t z = len * k1; - - v1 = rot64(y ^ k1, 49) * k1 + UNALIGNED_LOAD64(in); - v2 = rot64(v1, 42) * k1 + UNALIGNED_LOAD64(in + 8); - w1 = rot64(y + z, 35) * k1 + x; - w2 = rot64(x + UNALIGNED_LOAD64(in + 88), 53) * k1; - - for(; len >= 128; len-=128){ - x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1; - y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1; - x ^= w2; - y += v1 + UNALIGNED_LOAD64(in + 40); - z = rot64(z + w1, 33) * k1; - pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2); - pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2); - uint64_t temp = z; - z = x; - x = temp; - in += 64; - // - x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1; - y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1; - x ^= w2; - y += v1 + UNALIGNED_LOAD64(in + 40); - z = rot64(z + w1, 33) * k1; - pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2); - pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2); - temp = z; - z = x; - x = temp; - in += 64; - } - x += rot64(v1 + z, 49) * k0; - y = y * k0 + rot64(w2, 37); - z = z * k0 + rot64(w1, 27); - w1 *= 9; - v1 *= k0; - - for (size_t tail_done = 0; tail_done < len; ) { - tail_done += 32; - y = rot64(x + y, 42) * k0 + v2; - w1 += UNALIGNED_LOAD64(in + len - tail_done + 16); - x = x * k0 + w1; - z += w2 + UNALIGNED_LOAD64(in + len - tail_done); - w2 += v1; - pWeakHashLen32WithSeeds(in + len - tail_done, v1 + z, v2, &v1, &v2); - v1 *= k0; - } - - x = HashLen16_2(x, v1); - y = HashLen16_2(y + z, w1); - //return uint128(HashLen16(x + v.second, w.second) + y, - // HashLen16(x + w.second, y + v.second)); - *o1 = HashLen16_2(x + v2, w2) + y; - *o2 = HashLen16_2(x + w2, y + v2); - -} - -void cityhash128(uint8_t* in, size_t len, uint64_t* f, uint64_t* s){ - if(len >= 16) return cityhash128withseed(in + 16, len - 16,UNALIGNED_LOAD64(in), UNALIGNED_LOAD64(in + 8) + k0, f, s); - return cityhash128withseed(in, len, k0, k1, f, s); -} - -int l_cityhash32(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[32]; - - uint32_t u = cityhash32(a, len); - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_cityhash64(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = cityhash64(a, len); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_cityhash128(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[128]; - - uint64_t u1, u2; - cityhash128(a, len, &u1, &u2); - sprintf(digest,"%08llx%08llx",u1, u2); - lua_pushstring(L, digest); - return 1; -} +#include <stdio.h>
+#include <stdint.h>
+#include "cityhash.h"
+
+uint32_t rot32(uint32_t val, int shift) {
+ return ((val >> shift) | (val << (32 - shift)));
+}
+
+uint32_t fmix(uint32_t h){
+ h ^= h >> 16;
+ h *= 0x85ebca6b;
+ h ^= h >> 13;
+ h *= 0xc2b2ae35;
+ h ^= h >> 16;
+ return h;
+}
+
+uint32_t mur(uint32_t a, uint32_t h) {
+ a *= c1;
+ a = rot32(a, 17);
+ a *= c2;
+ h ^= a;
+ h = rot32(h, 19);
+ return h * 5 + 0xe6546b64;
+}
+
+uint32_t hash32len0to4(uint8_t* in, size_t len){
+ uint32_t b = 0, c = 9;
+ for(int i = 0; i != len; i++){
+ b = b * c1 + (uint32_t)in[i];
+ c ^= b;
+ }
+ return fmix(mur(b, mur((uint32_t)len, c)));
+}
+
+uint32_t UNALIGNED_LOAD32(uint8_t *p) {
+ return *(uint32_t*)p;
+ /* original google code:p
+ uint32_t result;
+ memcpy(&result, p, sizeof(result));
+ return result;
+ */
+}
+
+uint32_t hash32len5to12(uint8_t* in, size_t len){
+ uint32_t a = (uint32_t)(len), b = a * 5, c = 9, d = b;
+ a += UNALIGNED_LOAD32(in);
+ b += UNALIGNED_LOAD32(in + len - 4);
+ c += UNALIGNED_LOAD32(in + ((len >> 1) & 4));
+ return fmix(mur(c, mur(b, mur(a, d))));
+}
+
+uint32_t hash32len13to24(uint8_t* in, size_t len){
+ uint32_t a = UNALIGNED_LOAD32(in - 4 + (len >> 1));
+ uint32_t b = UNALIGNED_LOAD32(in + 4);
+ uint32_t c = UNALIGNED_LOAD32(in + len - 8);
+ uint32_t d = UNALIGNED_LOAD32(in + (len >> 1));
+ uint32_t e = UNALIGNED_LOAD32(in);
+ uint32_t f = UNALIGNED_LOAD32(in + len - 4);
+ uint32_t h = (uint32_t)len;
+
+ return fmix(mur(f, mur(e, mur(d, mur(c, mur(b, mur(a, h)))))));
+}
+
+uint32_t cityhash32(uint8_t* in, size_t len){
+ if(len <= 24){
+ if(len <= 12){
+ if(len <= 4) return hash32len0to4(in, len);
+ else return hash32len5to12(in, len);
+ }else return hash32len13to24(in, len);
+ }
+
+ uint32_t h = (uint32_t)len, g = c1 * h, f = g;
+ uint32_t a0 = rot32(UNALIGNED_LOAD32(in + len - 4) * c1, 17) * c2;
+ uint32_t a1 = rot32(UNALIGNED_LOAD32(in + len - 8) * c1, 17) * c2;
+ uint32_t a2 = rot32(UNALIGNED_LOAD32(in + len - 16) * c1, 17) * c2;
+ uint32_t a3 = rot32(UNALIGNED_LOAD32(in + len - 12) * c1, 17) * c2;
+ uint32_t a4 = rot32(UNALIGNED_LOAD32(in + len - 20) * c1, 17) * c2;
+ h ^= a0;
+ h = rot32(h, 19);
+ h = h * 5 + 0xe6546b64;
+ h ^= a2;
+ h = rot32(h, 19);
+ h = h * 5 + 0xe6546b64;
+ g ^= a1;
+ g = rot32(g, 19);
+ g = g * 5 + 0xe6546b64;
+ g ^= a3;
+ g = rot32(g, 19);
+ g = g * 5 + 0xe6546b64;
+ f += a4;
+ f = rot32(f, 19);
+ f = f * 5 + 0xe6546b64;
+
+ for(int i = (len - 1)/20; i != 0; i--){
+ uint32_t a0 = rot32(UNALIGNED_LOAD32(in) * c1, 17) * c2;
+ uint32_t a1 = UNALIGNED_LOAD32(in + 4);
+ uint32_t a2 = rot32(UNALIGNED_LOAD32(in + 8) * c1, 17) * c2;
+ uint32_t a3 = rot32(UNALIGNED_LOAD32(in + 12) * c1, 17) * c2;
+ uint32_t a4 = UNALIGNED_LOAD32(in + 16);
+ h ^= a0;
+ h = rot32(h, 18);
+ h = h * 5 + 0xe6546b64;
+ f += a1;
+ f = rot32(f, 19);
+ f = f * c1;
+ g += a2;
+ g = rot32(g, 18);
+ g = g * 5 + 0xe6546b64;
+ h ^= a3 + a1;
+ h = rot32(h, 19);
+ h = h * 5 + 0xe6546b64;
+ g ^= a4;
+ g = __builtin_bswap32(g) * 5;
+ h += a4 * 5;
+ h = __builtin_bswap32(h);
+ f += a0;
+ //PERMUTE3(f, h, g);
+ uint32_t temp = f;
+ f = h; h = temp;
+ temp = f;
+ f = g; g = temp;
+ //
+ in += 20;
+ }
+
+ g = rot32(g, 11) * c1;
+ g = rot32(g, 17) * c1;
+ f = rot32(f, 11) * c1;
+ f = rot32(f, 17) * c1;
+ h = rot32(h + g, 19);
+ h = h * 5 + 0xe6546b64;
+ h = rot32(h, 17) * c1;
+ h = rot32(h + f, 19);
+ h = h * 5 + 0xe6546b64;
+ h = rot32(h, 17) * c1;
+
+ return h;
+}
+
+//64 version
+
+uint64_t UNALIGNED_LOAD64(uint8_t *p) {
+ return *(uint64_t*)p;
+ /*
+ uint64_t result;
+ memcpy(&result, p, sizeof(result));
+ return result;
+ //*/
+}
+
+uint64_t rot64(uint64_t val, int shift) {
+ return ((val >> shift) | (val << (64 - shift)));
+}
+
+uint64_t hashlen16(uint64_t u, uint64_t v, uint64_t mul) {
+ uint64_t a = (u ^ v) * mul;
+ a ^= (a >> 47);
+ uint64_t b = (v ^ a) * mul;
+ b ^= (b >> 47);
+ b *= mul;
+ return b;
+}
+
+uint64_t shiftmix(uint64_t val) {
+ return val ^ (val >> 47);
+}
+
+uint64_t hashlen0to16(uint8_t* in, size_t len){
+ if(len >= 8){
+ uint64_t mul = k2 + len * 2;
+ uint64_t a = UNALIGNED_LOAD64(in) + k2;
+ uint64_t b = UNALIGNED_LOAD64(in + len - 8);
+ uint64_t c = rot64(b, 37) * mul + a;
+ uint64_t d = (rot64(a, 25) + b) * mul;
+ return hashlen16(c, d, mul);
+ }
+ if(len >= 4){
+ uint64_t mul = k2 + len * 2;
+ uint64_t a = UNALIGNED_LOAD32(in);
+ return hashlen16(len + (a << 3), UNALIGNED_LOAD32(in + len - 4), mul);
+ }
+ if(len > 0){
+ uint8_t a = (uint8_t)in[0];
+ uint8_t b = (uint8_t)in[len >> 1];
+ uint8_t c = (uint8_t)in[len - 1];
+ uint32_t y = ((uint32_t)a) + (((uint32_t)b) << 8);
+ uint32_t z = ((uint32_t)len) + ((uint32_t)(c) << 2);
+ return shiftmix(y * k2 ^ z * k0) * k2;
+ }
+ return k2;
+}
+uint64_t hashlen17to32(uint8_t* in, size_t len){
+ uint64_t mul = k2 + len * 2;
+ uint64_t a = UNALIGNED_LOAD64(in) * k1;
+ uint64_t b = UNALIGNED_LOAD64(in + 8);
+ uint64_t c = UNALIGNED_LOAD64(in + len - 8) * mul;
+ uint64_t d = UNALIGNED_LOAD64(in + len - 16) * k2;
+ return hashlen16(rot64(a + b, 43) + rot64(c, 30) + d,
+ a + rot64(b + k2, 18) + c, mul);
+}
+
+uint64_t hashlen33to64(uint8_t* in, size_t len){
+ uint64_t mul = k2 + len * 2;
+ uint64_t a = UNALIGNED_LOAD64(in) * k2;
+ uint64_t b = UNALIGNED_LOAD64(in + 8);
+ uint64_t c = UNALIGNED_LOAD64(in + len - 24);
+ uint64_t d = UNALIGNED_LOAD64(in + len - 32);
+ uint64_t e = UNALIGNED_LOAD64(in + 16) * k2;
+ uint64_t f = UNALIGNED_LOAD64(in + 24) * 9;
+ uint64_t g = UNALIGNED_LOAD64(in + len - 8);
+ uint64_t h = UNALIGNED_LOAD64(in + len - 16) * mul;
+ uint64_t u = rot64(a + g, 43) + (rot64(b, 30) + c) * 9;
+ uint64_t v = ((a + g) ^ d) + f + 1;
+ uint64_t w = __builtin_bswap64((u + v) * mul) + h;
+ uint64_t x = rot64(e + f, 42) + c;
+ uint64_t y = (__builtin_bswap64((v + w) * mul) + g) * mul;
+ uint64_t z = e + f + c;
+ a = __builtin_bswap64((x + z) * mul + y) + b;
+ b = shiftmix((z + a) * mul + d + h) * mul;
+ return b + x;
+}
+
+void WeakHashLen32WithSeeds(uint64_t w, uint64_t x, uint64_t y, uint64_t z, uint64_t a, uint64_t b, uint64_t*p1, int64_t*p2) {
+ a += w;
+ b = rot64(b + a + z, 21);
+ uint64_t c = a;
+ a += x;
+ a += y;
+ b += rot64(a, 44);
+ *p1 = a + z;
+ *p2 = b + c;
+ //return make_pair(a + z, b + c);
+}
+
+void pWeakHashLen32WithSeeds(uint8_t* s, uint64_t a, uint64_t b, uint64_t* p1, int64_t* p2) {
+ WeakHashLen32WithSeeds(UNALIGNED_LOAD64(s), UNALIGNED_LOAD64(s + 8),
+ UNALIGNED_LOAD64(s + 16), UNALIGNED_LOAD64(s + 24), a, b, p1, p2);
+}
+
+uint64_t hash128to64(uint64_t f, uint64_t s) {
+ uint64_t kMul = 0x9ddfea08eb382d69ULL;
+ uint64_t a = (f ^ s) * kMul;
+ a ^= (a >> 47);
+ uint64_t b = (s ^ a) * kMul;
+ b ^= (b >> 47);
+ b *= kMul;
+ return b;
+}
+
+uint64_t HashLen16_2(uint64_t u, uint64_t v) {
+ return hash128to64(u, v);
+}
+
+uint64_t cityhash64(uint8_t* in, size_t len){
+ if(len <= 32){
+ if(len <= 16) return hashlen0to16(in, len);
+ else return hashlen17to32(in, len);
+ } else if(len <= 64) return hashlen33to64(in, len);
+
+ uint64_t x = UNALIGNED_LOAD64(in + len - 40);
+ uint64_t y = UNALIGNED_LOAD64(in + len - 16) + UNALIGNED_LOAD64(in + len - 56);
+ uint64_t z = HashLen16_2(UNALIGNED_LOAD64(in + len - 48) + len, UNALIGNED_LOAD64(in + len - 24));
+ uint64_t v1, w1;
+ int64_t v2, w2;
+ pWeakHashLen32WithSeeds(in + len - 64, len, z, &v1, &v2);
+ pWeakHashLen32WithSeeds(in + len - 32, y + k1, x, &w1, &w2);
+ x = x * k1 + UNALIGNED_LOAD64(in);
+
+ for(int i = (len - 1) /64; i != 0; i--){
+ x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1;
+ y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1;
+ x ^= w2;
+ y += v1 + UNALIGNED_LOAD64(in + 40);
+ z = rot64(z + w1, 33) * k1;
+ pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2);
+ pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2);
+ //std::swap(z, x);
+ uint64_t temp = z;
+ z = x;
+ x = temp;
+ in += 64;
+ }
+ //printf("%llu %llu %llu\n",x,y,z);
+ return HashLen16_2(HashLen16_2(v1, w1) + shiftmix(y) * k1 + z,
+ HashLen16_2(v2, w2) + x);
+}
+
+void citymurmur(uint8_t* in, size_t len, uint64_t f, uint64_t s, uint64_t* o1, uint64_t* o2){
+ uint64_t a = f;
+ uint64_t b = s;
+ uint64_t c = 0;
+ uint64_t d = 0;
+
+ if (len <= 16) {
+ a = shiftmix(a * k1) * k1;
+ c = b * k1 + hashlen0to16(in, len);
+ d = shiftmix(a + (len >= 8 ? UNALIGNED_LOAD64(in) : c));
+ } else {
+ c = HashLen16_2(UNALIGNED_LOAD64(in + len - 8) + k1, a);
+ d = HashLen16_2(b + len, c + UNALIGNED_LOAD64(in + len - 16));
+ a += d;
+
+ for(; len > 16; len -=16) {
+ a ^= shiftmix(UNALIGNED_LOAD64(in) * k1) * k1;
+ a *= k1;
+ b ^= a;
+ c ^= shiftmix(UNALIGNED_LOAD64(in + 8) * k1) * k1;
+ c *= k1;
+ d ^= c;
+ s += 16;
+ len -= 16;
+ }
+ }
+ a = HashLen16_2(a, c);
+ b = HashLen16_2(d, b);
+ //return uint128(a ^ b, HashLen16(b, a));
+ *o2 = a ^ b;
+ *o1 = HashLen16_2(b, a);
+}
+
+void cityhash128withseed(uint8_t* in, size_t len, uint64_t f, uint64_t s, uint64_t* o1, uint64_t* o2){
+ if(len < 128){
+ citymurmur(in, len, k0, k1, o2, o1);
+ return;
+ }
+
+ uint64_t v1, w1;
+ int64_t v2, w2;
+ uint64_t x = f;
+ uint64_t y = s;
+ uint64_t z = len * k1;
+
+ v1 = rot64(y ^ k1, 49) * k1 + UNALIGNED_LOAD64(in);
+ v2 = rot64(v1, 42) * k1 + UNALIGNED_LOAD64(in + 8);
+ w1 = rot64(y + z, 35) * k1 + x;
+ w2 = rot64(x + UNALIGNED_LOAD64(in + 88), 53) * k1;
+
+ for(; len >= 128; len-=128){
+ x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1;
+ y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1;
+ x ^= w2;
+ y += v1 + UNALIGNED_LOAD64(in + 40);
+ z = rot64(z + w1, 33) * k1;
+ pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2);
+ pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2);
+ uint64_t temp = z;
+ z = x;
+ x = temp;
+ in += 64;
+ //
+ x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1;
+ y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1;
+ x ^= w2;
+ y += v1 + UNALIGNED_LOAD64(in + 40);
+ z = rot64(z + w1, 33) * k1;
+ pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2);
+ pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2);
+ temp = z;
+ z = x;
+ x = temp;
+ in += 64;
+ }
+ x += rot64(v1 + z, 49) * k0;
+ y = y * k0 + rot64(w2, 37);
+ z = z * k0 + rot64(w1, 27);
+ w1 *= 9;
+ v1 *= k0;
+
+ for (size_t tail_done = 0; tail_done < len; ) {
+ tail_done += 32;
+ y = rot64(x + y, 42) * k0 + v2;
+ w1 += UNALIGNED_LOAD64(in + len - tail_done + 16);
+ x = x * k0 + w1;
+ z += w2 + UNALIGNED_LOAD64(in + len - tail_done);
+ w2 += v1;
+ pWeakHashLen32WithSeeds(in + len - tail_done, v1 + z, v2, &v1, &v2);
+ v1 *= k0;
+ }
+
+ x = HashLen16_2(x, v1);
+ y = HashLen16_2(y + z, w1);
+ //return uint128(HashLen16(x + v.second, w.second) + y,
+ // HashLen16(x + w.second, y + v.second));
+ *o1 = HashLen16_2(x + v2, w2) + y;
+ *o2 = HashLen16_2(x + w2, y + v2);
+
+}
+
+void cityhash128(uint8_t* in, size_t len, uint64_t* f, uint64_t* s){
+ if(len >= 16) return cityhash128withseed(in + 16, len - 16,UNALIGNED_LOAD64(in), UNALIGNED_LOAD64(in + 8) + k0, f, s);
+ return cityhash128withseed(in, len, k0, k1, f, s);
+}
+
+int l_cityhash32(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[32];
+
+ uint32_t u = cityhash32(a, len);
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_cityhash64(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = cityhash64(a, len);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_cityhash128(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[128];
+
+ uint64_t u1, u2;
+ cityhash128(a, len, &u1, &u2);
+ sprintf(digest,"%08llx%08llx",u1, u2);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/crc.c b/src/hash/crc.c index acb6984..650b315 100644 --- a/src/hash/crc.c +++ b/src/hash/crc.c @@ -1,160 +1,160 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> -#include <stdio.h> -#include <stdint.h> - -struct crc32_hash crc32_init(){ - 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; - } - } -} - -uint32_t crc32_final(struct crc32_hash* hash){ - 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 crc16_hash crc16_init(){ - 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); - } - } -} - -uint16_t crc16_final(struct crc16_hash *hash){ - 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 crc8_hash crc8_init(){ - 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; - } - } -} - -uint8_t crc8_final(struct crc8_hash *hash){ - 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); -} - -common_hash_clone(crc32); -common_hash_clone(crc16); -common_hash_clone(crc8); - -common_hash_init_update(crc32); -common_hash_init_update(crc16); -common_hash_init_update(crc8); - -int l_crc8_final(lua_State* L){ - struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, 1); - uint32_t u = crc8_final(a); - char digest[8]; - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_crc16_final(lua_State* L){ - struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, 1); - uint32_t u = crc16_final(a); - char digest[16]; - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_crc32_final(lua_State* L){ - struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, 1); - uint32_t u = crc32_final(a); - char digest[32]; - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} - -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); - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - - return 1; -} - -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); - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - - return 1; -} - -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); - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdint.h>
+
+struct crc32_hash crc32_init(){
+ 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;
+ }
+ }
+}
+
+uint32_t crc32_final(struct crc32_hash* hash){
+ 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 crc16_hash crc16_init(){
+ 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);
+ }
+ }
+}
+
+uint16_t crc16_final(struct crc16_hash *hash){
+ 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 crc8_hash crc8_init(){
+ 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;
+ }
+ }
+}
+
+uint8_t crc8_final(struct crc8_hash *hash){
+ 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);
+}
+
+common_hash_clone(crc32);
+common_hash_clone(crc16);
+common_hash_clone(crc8);
+
+common_hash_init_update(crc32);
+common_hash_init_update(crc16);
+common_hash_init_update(crc8);
+
+int l_crc8_final(lua_State* L){
+ struct crc8_hash* a = (struct crc8_hash*)lua_touserdata(L, 1);
+ uint32_t u = crc8_final(a);
+ char digest[8];
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_crc16_final(lua_State* L){
+ struct crc16_hash* a = (struct crc16_hash*)lua_touserdata(L, 1);
+ uint32_t u = crc16_final(a);
+ char digest[16];
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_crc32_final(lua_State* L){
+ struct crc32_hash* a = (struct crc32_hash*)lua_touserdata(L, 1);
+ uint32_t u = crc32_final(a);
+ char digest[32];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+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);
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+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);
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/djb2.c b/src/hash/djb2.c index ed01345..827d468 100644 --- a/src/hash/djb2.c +++ b/src/hash/djb2.c @@ -1,50 +1,50 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct djb2_hash djb2_init(){ - return (struct djb2_hash){.hash = 5381}; -} - -void djb2_update(uint8_t * in, size_t len, struct djb2_hash * hash){ - for(int i = 0; i != len; i++){ - hash->hash = ((hash->hash << 5) + hash->hash) + (uint32_t)*in; - in++; - } -} - -uint32_t djb2_final(struct djb2_hash * hash){ - return hash->hash; -} - -uint32_t djb2(uint8_t * in, size_t len){ - struct djb2_hash a = djb2_init(); - djb2_update(in, len, &a); - return djb2_final(&a); -} - -common_hash_clone(djb2); - -common_hash_init_update(djb2); - -int l_djb2_final(lua_State* L){ - struct djb2_hash* a = (struct djb2_hash*)lua_touserdata(L, 1); - uint32_t u = djb2_final(a); - char digest[64]; - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} - -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); - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct djb2_hash djb2_init(){
+ return (struct djb2_hash){.hash = 5381};
+}
+
+void djb2_update(uint8_t * in, size_t len, struct djb2_hash * hash){
+ for(int i = 0; i != len; i++){
+ hash->hash = ((hash->hash << 5) + hash->hash) + (uint32_t)*in;
+ in++;
+ }
+}
+
+uint32_t djb2_final(struct djb2_hash * hash){
+ return hash->hash;
+}
+
+uint32_t djb2(uint8_t * in, size_t len){
+ struct djb2_hash a = djb2_init();
+ djb2_update(in, len, &a);
+ return djb2_final(&a);
+}
+
+common_hash_clone(djb2);
+
+common_hash_init_update(djb2);
+
+int l_djb2_final(lua_State* L){
+ struct djb2_hash* a = (struct djb2_hash*)lua_touserdata(L, 1);
+ uint32_t u = djb2_final(a);
+ char digest[64];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/farmhash.c b/src/hash/farmhash.c index 95047ee..462bf4b 100644 --- a/src/hash/farmhash.c +++ b/src/hash/farmhash.c @@ -1,169 +1,169 @@ -#include "../util.h" -#include "../crypto.h" //include city hash too -#include <stdint.h> - -uint32_t farmhash32len13to24(uint8_t* in, size_t len) { - uint32_t seed = 0; - uint32_t a = UNALIGNED_LOAD32(in - 4 + (len >> 1)); - uint32_t b = UNALIGNED_LOAD32(in + 4); - uint32_t c = UNALIGNED_LOAD32(in + len - 8); - uint32_t d = UNALIGNED_LOAD32(in + (len >> 1)); - uint32_t e = UNALIGNED_LOAD32(in); - uint32_t f = UNALIGNED_LOAD32(in + len - 4); - uint32_t h = d * c1 + len + seed; - a = rot32(a, 12) + f; - h = mur(c, h) + a; - a = rot32(a, 3) + c; - h = mur(e, h) + a; - a = rot32(a + f, 12) + d; - h = mur(b ^ seed, h) + a; - return fmix(h); -} - -uint32_t farmhash32(uint8_t* in, size_t len){ - if(len <= 24){ - if(len <= 12){ - if(len <= 4) return hash32len0to4(in, len); - else return hash32len5to12(in, len); - } else return farmhash32len13to24(in, len); - } - - uint32_t h = len, g = c1 * len, f = g; - uint32_t a0 = rot32(UNALIGNED_LOAD32(in + len - 4) * c1, 17) * c2; - uint32_t a1 = rot32(UNALIGNED_LOAD32(in + len - 8) * c1, 17) * c2; - uint32_t a2 = rot32(UNALIGNED_LOAD32(in + len - 16) * c1, 17) * c2; - uint32_t a3 = rot32(UNALIGNED_LOAD32(in + len - 12) * c1, 17) * c2; - uint32_t a4 = rot32(UNALIGNED_LOAD32(in + len - 20) * c1, 17) * c2; - - h ^= a0; - h = rot32(h, 19); - h = h * 5 + 0xe6546b64; - h ^= a2; - h = rot32(h, 19); - h = h * 5 + 0xe6546b64; - g ^= a1; - g = rot32(g, 19); - g = g * 5 + 0xe6546b64; - g ^= a3; - g = rot32(g, 19); - g = g * 5 + 0xe6546b64; - f += a4; - f = rot32(f, 19) + 113; - size_t iters = (len - 1) / 20; - for(int i = (len - 1) / 20; i != 0; i--){ - uint32_t a = UNALIGNED_LOAD32(in); - uint32_t b = UNALIGNED_LOAD32(in + 4); - uint32_t c = UNALIGNED_LOAD32(in + 8); - uint32_t d = UNALIGNED_LOAD32(in + 12); - uint32_t e = UNALIGNED_LOAD32(in + 16); - h += a; - g += b; - f += c; - h = mur(d, h) + e; - g = mur(c, g) + a; - f = mur(b + e * c1, f) + d; - f += g; - g += f; - in += 20; - } - g = rot32(g, 11) * c1; - g = rot32(g, 17) * c1; - f = rot32(f, 11) * c1; - f = rot32(f, 17) * c1; - h = rot32(h + g, 19); - h = h * 5 + 0xe6546b64; - h = rot32(h, 17) * c1; - h = rot32(h + f, 19); - h = h * 5 + 0xe6546b64; - h = rot32(h, 17) * c1; - return h; -} - -uint64_t farmhashlen33to64(uint8_t *s, size_t len) { - uint64_t mul = k2 + len * 2; - uint64_t a = UNALIGNED_LOAD64(s) * k2; - uint64_t b = UNALIGNED_LOAD64(s + 8); - uint64_t c = UNALIGNED_LOAD64(s + len - 8) * mul; - uint64_t d = UNALIGNED_LOAD64(s + len - 16) * k2; - uint64_t y = rot64(a + b, 43) + rot64(c, 30) + d; - uint64_t z = hashlen16(y, a + rot64(b + k2, 18) + c, mul); - uint64_t e = UNALIGNED_LOAD64(s + 16) * mul; - uint64_t f = UNALIGNED_LOAD64(s + 24); - uint64_t g = (y + UNALIGNED_LOAD64(s + len - 32)) * mul; - uint64_t h = (z + UNALIGNED_LOAD64(s + len - 24)) * mul; - return hashlen16(rot64(e + f, 43) + rot64(g, 30) + h, - e + rot64(f + a, 18) + g, mul); -} - -uint64_t farmhash64(uint8_t* in, size_t len){ - if(len <= 32){ - if(len <= 16) return hashlen0to16(in, len); - else return hashlen17to32(in, len); - } else if(len <= 64) return farmhashlen33to64(in, len); - - uint64_t seed = 81; - uint64_t x = seed; - uint64_t y = seed * k1 + 113; - uint64_t z = shiftmix(y * k2 + 113) * k2; - - uint64_t v1 = 0, w1 = 0; - int64_t v2 = 0, w2 = 0; - x = x * k2 + UNALIGNED_LOAD64(in); - uint8_t* end = in + ((len - 1) / 64) * 64; - uint8_t* last64 = end + ((len - 1) & 63) - 63; - //assert(in + len - 64 == last64); - for(; in != end; in += 64) { - x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1; - y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1; - x ^= w2; - y += v1 + UNALIGNED_LOAD64(in + 40); - z = rot64(z + w1, 33) * k1; - pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2); - pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2); - uint64_t temp = z; - z = x; x = temp; - } - uint64_t mul = k1 + ((z & 0xff) << 1); - - in = last64; - w1 += ((len - 1) & 63); - v1 += w1; - w1 += v1; - x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * mul; - y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * mul; - x ^= w2 * 9; - y += v1 * 9 + UNALIGNED_LOAD64(in + 40); - z = rot64(z + w1, 33) * mul; - pWeakHashLen32WithSeeds(in, v2 * mul, x + w1, &v1, &v2); - pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2); - uint64_t temp = z; - z = x; x = temp; - return hashlen16(hashlen16(v1, w1, mul) + shiftmix(y) * k0 + z, - hashlen16(v2, w2, mul) + x, - mul); - return 1; -} - -int l_farmhash32(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[32]; - - uint32_t u = farmhash32(a, len); - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_farmhash64(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = farmhash64(a, len); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h" //include city hash too
+#include <stdint.h>
+
+uint32_t farmhash32len13to24(uint8_t* in, size_t len) {
+ uint32_t seed = 0;
+ uint32_t a = UNALIGNED_LOAD32(in - 4 + (len >> 1));
+ uint32_t b = UNALIGNED_LOAD32(in + 4);
+ uint32_t c = UNALIGNED_LOAD32(in + len - 8);
+ uint32_t d = UNALIGNED_LOAD32(in + (len >> 1));
+ uint32_t e = UNALIGNED_LOAD32(in);
+ uint32_t f = UNALIGNED_LOAD32(in + len - 4);
+ uint32_t h = d * c1 + len + seed;
+ a = rot32(a, 12) + f;
+ h = mur(c, h) + a;
+ a = rot32(a, 3) + c;
+ h = mur(e, h) + a;
+ a = rot32(a + f, 12) + d;
+ h = mur(b ^ seed, h) + a;
+ return fmix(h);
+}
+
+uint32_t farmhash32(uint8_t* in, size_t len){
+ if(len <= 24){
+ if(len <= 12){
+ if(len <= 4) return hash32len0to4(in, len);
+ else return hash32len5to12(in, len);
+ } else return farmhash32len13to24(in, len);
+ }
+
+ uint32_t h = len, g = c1 * len, f = g;
+ uint32_t a0 = rot32(UNALIGNED_LOAD32(in + len - 4) * c1, 17) * c2;
+ uint32_t a1 = rot32(UNALIGNED_LOAD32(in + len - 8) * c1, 17) * c2;
+ uint32_t a2 = rot32(UNALIGNED_LOAD32(in + len - 16) * c1, 17) * c2;
+ uint32_t a3 = rot32(UNALIGNED_LOAD32(in + len - 12) * c1, 17) * c2;
+ uint32_t a4 = rot32(UNALIGNED_LOAD32(in + len - 20) * c1, 17) * c2;
+
+ h ^= a0;
+ h = rot32(h, 19);
+ h = h * 5 + 0xe6546b64;
+ h ^= a2;
+ h = rot32(h, 19);
+ h = h * 5 + 0xe6546b64;
+ g ^= a1;
+ g = rot32(g, 19);
+ g = g * 5 + 0xe6546b64;
+ g ^= a3;
+ g = rot32(g, 19);
+ g = g * 5 + 0xe6546b64;
+ f += a4;
+ f = rot32(f, 19) + 113;
+ size_t iters = (len - 1) / 20;
+ for(int i = (len - 1) / 20; i != 0; i--){
+ uint32_t a = UNALIGNED_LOAD32(in);
+ uint32_t b = UNALIGNED_LOAD32(in + 4);
+ uint32_t c = UNALIGNED_LOAD32(in + 8);
+ uint32_t d = UNALIGNED_LOAD32(in + 12);
+ uint32_t e = UNALIGNED_LOAD32(in + 16);
+ h += a;
+ g += b;
+ f += c;
+ h = mur(d, h) + e;
+ g = mur(c, g) + a;
+ f = mur(b + e * c1, f) + d;
+ f += g;
+ g += f;
+ in += 20;
+ }
+ g = rot32(g, 11) * c1;
+ g = rot32(g, 17) * c1;
+ f = rot32(f, 11) * c1;
+ f = rot32(f, 17) * c1;
+ h = rot32(h + g, 19);
+ h = h * 5 + 0xe6546b64;
+ h = rot32(h, 17) * c1;
+ h = rot32(h + f, 19);
+ h = h * 5 + 0xe6546b64;
+ h = rot32(h, 17) * c1;
+ return h;
+}
+
+uint64_t farmhashlen33to64(uint8_t *s, size_t len) {
+ uint64_t mul = k2 + len * 2;
+ uint64_t a = UNALIGNED_LOAD64(s) * k2;
+ uint64_t b = UNALIGNED_LOAD64(s + 8);
+ uint64_t c = UNALIGNED_LOAD64(s + len - 8) * mul;
+ uint64_t d = UNALIGNED_LOAD64(s + len - 16) * k2;
+ uint64_t y = rot64(a + b, 43) + rot64(c, 30) + d;
+ uint64_t z = hashlen16(y, a + rot64(b + k2, 18) + c, mul);
+ uint64_t e = UNALIGNED_LOAD64(s + 16) * mul;
+ uint64_t f = UNALIGNED_LOAD64(s + 24);
+ uint64_t g = (y + UNALIGNED_LOAD64(s + len - 32)) * mul;
+ uint64_t h = (z + UNALIGNED_LOAD64(s + len - 24)) * mul;
+ return hashlen16(rot64(e + f, 43) + rot64(g, 30) + h,
+ e + rot64(f + a, 18) + g, mul);
+}
+
+uint64_t farmhash64(uint8_t* in, size_t len){
+ if(len <= 32){
+ if(len <= 16) return hashlen0to16(in, len);
+ else return hashlen17to32(in, len);
+ } else if(len <= 64) return farmhashlen33to64(in, len);
+
+ uint64_t seed = 81;
+ uint64_t x = seed;
+ uint64_t y = seed * k1 + 113;
+ uint64_t z = shiftmix(y * k2 + 113) * k2;
+
+ uint64_t v1 = 0, w1 = 0;
+ int64_t v2 = 0, w2 = 0;
+ x = x * k2 + UNALIGNED_LOAD64(in);
+ uint8_t* end = in + ((len - 1) / 64) * 64;
+ uint8_t* last64 = end + ((len - 1) & 63) - 63;
+ //assert(in + len - 64 == last64);
+ for(; in != end; in += 64) {
+ x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * k1;
+ y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * k1;
+ x ^= w2;
+ y += v1 + UNALIGNED_LOAD64(in + 40);
+ z = rot64(z + w1, 33) * k1;
+ pWeakHashLen32WithSeeds(in, v2 * k1, x + w1, &v1, &v2);
+ pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2);
+ uint64_t temp = z;
+ z = x; x = temp;
+ }
+ uint64_t mul = k1 + ((z & 0xff) << 1);
+
+ in = last64;
+ w1 += ((len - 1) & 63);
+ v1 += w1;
+ w1 += v1;
+ x = rot64(x + y + v1 + UNALIGNED_LOAD64(in + 8), 37) * mul;
+ y = rot64(y + v2 + UNALIGNED_LOAD64(in + 48), 42) * mul;
+ x ^= w2 * 9;
+ y += v1 * 9 + UNALIGNED_LOAD64(in + 40);
+ z = rot64(z + w1, 33) * mul;
+ pWeakHashLen32WithSeeds(in, v2 * mul, x + w1, &v1, &v2);
+ pWeakHashLen32WithSeeds(in + 32, z + w2, y + UNALIGNED_LOAD64(in + 16), &w1, &w2);
+ uint64_t temp = z;
+ z = x; x = temp;
+ return hashlen16(hashlen16(v1, w1, mul) + shiftmix(y) * k0 + z,
+ hashlen16(v2, w2, mul) + x,
+ mul);
+ return 1;
+}
+
+int l_farmhash32(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[32];
+
+ uint32_t u = farmhash32(a, len);
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_farmhash64(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = farmhash64(a, len);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/fasthash.c b/src/hash/fasthash.c index 993a22a..b27b6b9 100644 --- a/src/hash/fasthash.c +++ b/src/hash/fasthash.c @@ -1,83 +1,83 @@ -#include "../util.h" -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> -//almost entirely taken from https://github.com/ztanml/fast-hash/blob/master/fasthash.c - -#define mix(h) ({ \ - (h) ^= (h) >> 23; \ - (h) *= 0x2127599bf4325c37ULL; \ - (h) ^= (h) >> 47; }) - -uint64_t fasthash64(uint8_t* in, size_t len, uint64_t seed){ - uint64_t m = 0x880355f21e6d1965ULL; - uint64_t hash = seed ^ (len * m); - uint64_t* data = (uint64_t*)in; - uint64_t v; - for(;len >= 8; len-=8){ - v=*data++; - hash^=mix(v); - hash*=m; - - in+=4; - } - - uint8_t* data2 = (uint8_t*)data; - v=0; - - switch (len & 7) { - case 7: - v ^= (uint64_t)data2[6] << 48; - case 6: - v ^= (uint64_t)data2[5] << 40; - case 5: - v ^= (uint64_t)data2[4] << 32; - case 4: - v ^= (uint64_t)data2[3] << 24; - case 3: - v ^= (uint64_t)data2[2] << 16; - case 2: - v ^= (uint64_t)data2[1] << 8; - case 1: - v ^= (uint64_t)data2[0]; - hash ^= mix(v); - hash *= m; - } - - return mix(hash); -} - -uint32_t fasthash32(uint8_t *buf, size_t len, uint32_t seed){ - uint64_t hash = fasthash64(buf, len, seed); - return hash - (hash >> 32); -} - -int l_fasthash64(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - uint64_t seed = 0; - if(argv > 1) seed = luaL_checkinteger(L, 2); - - char digest[64]; - - uint64_t u = fasthash64(a, len, seed); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_fasthash32(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - uint32_t seed = 0; - if(argv > 1) seed = luaL_checkinteger(L, 2); - - char digest[32]; - - uint32_t u = fasthash32(a, len, seed); - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+//almost entirely taken from https://github.com/ztanml/fast-hash/blob/master/fasthash.c
+
+#define mix(h) ({ \
+ (h) ^= (h) >> 23; \
+ (h) *= 0x2127599bf4325c37ULL; \
+ (h) ^= (h) >> 47; })
+
+uint64_t fasthash64(uint8_t* in, size_t len, uint64_t seed){
+ uint64_t m = 0x880355f21e6d1965ULL;
+ uint64_t hash = seed ^ (len * m);
+ uint64_t* data = (uint64_t*)in;
+ uint64_t v;
+ for(;len >= 8; len-=8){
+ v=*data++;
+ hash^=mix(v);
+ hash*=m;
+
+ in+=4;
+ }
+
+ uint8_t* data2 = (uint8_t*)data;
+ v=0;
+
+ switch (len & 7) {
+ case 7:
+ v ^= (uint64_t)data2[6] << 48;
+ case 6:
+ v ^= (uint64_t)data2[5] << 40;
+ case 5:
+ v ^= (uint64_t)data2[4] << 32;
+ case 4:
+ v ^= (uint64_t)data2[3] << 24;
+ case 3:
+ v ^= (uint64_t)data2[2] << 16;
+ case 2:
+ v ^= (uint64_t)data2[1] << 8;
+ case 1:
+ v ^= (uint64_t)data2[0];
+ hash ^= mix(v);
+ hash *= m;
+ }
+
+ return mix(hash);
+}
+
+uint32_t fasthash32(uint8_t *buf, size_t len, uint32_t seed){
+ uint64_t hash = fasthash64(buf, len, seed);
+ return hash - (hash >> 32);
+}
+
+int l_fasthash64(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+ uint64_t seed = 0;
+ if(argv > 1) seed = luaL_checkinteger(L, 2);
+
+ char digest[64];
+
+ uint64_t u = fasthash64(a, len, seed);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_fasthash32(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+ uint32_t seed = 0;
+ if(argv > 1) seed = luaL_checkinteger(L, 2);
+
+ char digest[32];
+
+ uint32_t u = fasthash32(a, len, seed);
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/fletcher.c b/src/hash/fletcher.c index be618bf..52008d8 100644 --- a/src/hash/fletcher.c +++ b/src/hash/fletcher.c @@ -1,143 +1,143 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct fletcher8_hash fletcher8_init(){ - return (struct fletcher8_hash){.s1 = 0, .s2 = 0}; -} - -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; - } -} - -uint8_t fletcher8_final(struct fletcher8_hash *hash){ - return (hash->s2 << 4) | hash->s1; -} - -uint8_t fletcher8(uint8_t *aa, size_t len){ - struct fletcher8_hash a = fletcher8_init(); - fletcher8_update(aa, len, &a); - return fletcher8_final(&a); -} - -struct fletcher16_hash fletcher16_init(){ - return (struct fletcher16_hash){.s1 = 0, .s2 = 0}; -} - -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; - } -} - -uint16_t fletcher16_final(struct fletcher16_hash *hash){ - return (hash->s2 << 8) | hash->s1; -} - -uint16_t fletcher16(uint8_t *aa, size_t len){ - struct fletcher16_hash a = fletcher16_init(); - fletcher16_update(aa, len, &a); - return fletcher16_final(&a); -} - -struct fletcher32_hash fletcher32_init(){ - return (struct fletcher32_hash){.s1 = 0, .s2 = 0}; -} - -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; - } -} - -uint32_t fletcher32_final(struct fletcher32_hash *hash){ - return (hash->s2 << 16) | hash->s1; -} - -uint32_t fletcher32(uint8_t *aa, size_t len){ - struct fletcher32_hash a = fletcher32_init(); - fletcher32_update(aa, len, &a); - return fletcher32_final(&a); -} - -common_hash_clone(fletcher32); -common_hash_clone(fletcher16); -common_hash_clone(fletcher8); - -common_hash_init_update(fletcher8); -common_hash_init_update(fletcher16); -common_hash_init_update(fletcher32); - -int l_fletcher8_final(lua_State* L){ - struct fletcher8_hash* a = (struct fletcher8_hash*)lua_touserdata(L, 1); - uint8_t u = fletcher8_final(a); - char digest[8]; - sprintf(digest,"%02x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_fletcher16_final(lua_State* L){ - struct fletcher16_hash* a = (struct fletcher16_hash*)lua_touserdata(L, 1); - uint16_t u = fletcher16_final(a); - char digest[16]; - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_fletcher32_final(lua_State* L){ - struct fletcher32_hash* a = (struct fletcher32_hash*)lua_touserdata(L, 1); - uint32_t u = fletcher32_final(a); - char digest[32]; - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} - -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); - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - - return 1; -} - -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); - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - - return 1; -} - -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); - sprintf(digest,"%02x",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct fletcher8_hash fletcher8_init(){
+ return (struct fletcher8_hash){.s1 = 0, .s2 = 0};
+}
+
+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;
+ }
+}
+
+uint8_t fletcher8_final(struct fletcher8_hash *hash){
+ return (hash->s2 << 4) | hash->s1;
+}
+
+uint8_t fletcher8(uint8_t *aa, size_t len){
+ struct fletcher8_hash a = fletcher8_init();
+ fletcher8_update(aa, len, &a);
+ return fletcher8_final(&a);
+}
+
+struct fletcher16_hash fletcher16_init(){
+ return (struct fletcher16_hash){.s1 = 0, .s2 = 0};
+}
+
+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;
+ }
+}
+
+uint16_t fletcher16_final(struct fletcher16_hash *hash){
+ return (hash->s2 << 8) | hash->s1;
+}
+
+uint16_t fletcher16(uint8_t *aa, size_t len){
+ struct fletcher16_hash a = fletcher16_init();
+ fletcher16_update(aa, len, &a);
+ return fletcher16_final(&a);
+}
+
+struct fletcher32_hash fletcher32_init(){
+ return (struct fletcher32_hash){.s1 = 0, .s2 = 0};
+}
+
+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;
+ }
+}
+
+uint32_t fletcher32_final(struct fletcher32_hash *hash){
+ return (hash->s2 << 16) | hash->s1;
+}
+
+uint32_t fletcher32(uint8_t *aa, size_t len){
+ struct fletcher32_hash a = fletcher32_init();
+ fletcher32_update(aa, len, &a);
+ return fletcher32_final(&a);
+}
+
+common_hash_clone(fletcher32);
+common_hash_clone(fletcher16);
+common_hash_clone(fletcher8);
+
+common_hash_init_update(fletcher8);
+common_hash_init_update(fletcher16);
+common_hash_init_update(fletcher32);
+
+int l_fletcher8_final(lua_State* L){
+ struct fletcher8_hash* a = (struct fletcher8_hash*)lua_touserdata(L, 1);
+ uint8_t u = fletcher8_final(a);
+ char digest[8];
+ sprintf(digest,"%02x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_fletcher16_final(lua_State* L){
+ struct fletcher16_hash* a = (struct fletcher16_hash*)lua_touserdata(L, 1);
+ uint16_t u = fletcher16_final(a);
+ char digest[16];
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_fletcher32_final(lua_State* L){
+ struct fletcher32_hash* a = (struct fletcher32_hash*)lua_touserdata(L, 1);
+ uint32_t u = fletcher32_final(a);
+ char digest[32];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+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);
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+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);
+ sprintf(digest,"%02x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/fnv.c b/src/hash/fnv.c index 2991ca8..6214ea5 100644 --- a/src/hash/fnv.c +++ b/src/hash/fnv.c @@ -1,101 +1,101 @@ -#include "../util.h" -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct fnv_1_hash fnv_1_init(enum fnv_version A){ - return (struct fnv_1_hash){.A = A, .hash = (A != v_0) * 0xcbf29ce484222325}; -} - -void fnv_1_update(uint8_t* in, size_t len, struct fnv_1_hash* hash){ - uint64_t prime = 0x100000001b3; - - for(int i = 0; i != len; i++){ - switch(hash->A){ - case v_1: - case v_0: - hash->hash *= prime; - hash->hash ^= in[i]; - break; - case v_a: - hash->hash ^= in[i]; - hash->hash *= prime; - break; - } - } -} - -uint64_t fnv_1_final(struct fnv_1_hash* hash){ - return hash->hash; -} - -uint64_t fnv_1(uint8_t* in, size_t len, enum fnv_version A){ - struct fnv_1_hash a = fnv_1_init(A); - fnv_1_update(in, len, &a); - return fnv_1_final(&a); -} - -lua_common_hash_clone(fnv_1, fnv_0); -lua_common_hash_clone(fnv_1, fnv_1); -lua_common_hash_clone(fnv_1, fnv_a); - -lua_common_hash_update(fnv_1, fnv_1); -lua_common_hash_update(fnv_1, fnv_0); -lua_common_hash_update(fnv_1, fnv_a); - -lua_common_hash_init_ni(fnv_1, fnv_1, fnv_1_init(v_1)); -lua_common_hash_init_ni(fnv_1, fnv_0, fnv_1_init(v_0)); -lua_common_hash_init_ni(fnv_1, fnv_a, fnv_1_init(v_a)); - -#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,"%16llx",u);\ - lua_pushstring(L, digest);\ - return 1;\ -} - -aaa(0); -aaa(1); -aaa(a); - -int l_fnv_0(lua_State* L){ - if(lua_gettop(L) == 0) return l_fnv_0_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = fnv_1(a, len, v_0); - sprintf(digest,"%16llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_fnv_1(lua_State* L){ - if(lua_gettop(L) == 0) return l_fnv_1_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = fnv_1(a, len, v_1); - sprintf(digest,"%16llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_fnv_a(lua_State* L){ - if(lua_gettop(L) == 0) return l_fnv_a_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = fnv_1(a, len, v_a); - sprintf(digest,"%16llx",u); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct fnv_1_hash fnv_1_init(enum fnv_version A){
+ return (struct fnv_1_hash){.A = A, .hash = (A != v_0) * 0xcbf29ce484222325};
+}
+
+void fnv_1_update(uint8_t* in, size_t len, struct fnv_1_hash* hash){
+ uint64_t prime = 0x100000001b3;
+
+ for(int i = 0; i != len; i++){
+ switch(hash->A){
+ case v_1:
+ case v_0:
+ hash->hash *= prime;
+ hash->hash ^= in[i];
+ break;
+ case v_a:
+ hash->hash ^= in[i];
+ hash->hash *= prime;
+ break;
+ }
+ }
+}
+
+uint64_t fnv_1_final(struct fnv_1_hash* hash){
+ return hash->hash;
+}
+
+uint64_t fnv_1(uint8_t* in, size_t len, enum fnv_version A){
+ struct fnv_1_hash a = fnv_1_init(A);
+ fnv_1_update(in, len, &a);
+ return fnv_1_final(&a);
+}
+
+lua_common_hash_clone(fnv_1, fnv_0);
+lua_common_hash_clone(fnv_1, fnv_1);
+lua_common_hash_clone(fnv_1, fnv_a);
+
+lua_common_hash_update(fnv_1, fnv_1);
+lua_common_hash_update(fnv_1, fnv_0);
+lua_common_hash_update(fnv_1, fnv_a);
+
+lua_common_hash_init_ni(fnv_1, fnv_1, fnv_1_init(v_1));
+lua_common_hash_init_ni(fnv_1, fnv_0, fnv_1_init(v_0));
+lua_common_hash_init_ni(fnv_1, fnv_a, fnv_1_init(v_a));
+
+#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,"%16llx",u);\
+ lua_pushstring(L, digest);\
+ return 1;\
+}
+
+aaa(0);
+aaa(1);
+aaa(a);
+
+int l_fnv_0(lua_State* L){
+ if(lua_gettop(L) == 0) return l_fnv_0_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = fnv_1(a, len, v_0);
+ sprintf(digest,"%16llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_fnv_1(lua_State* L){
+ if(lua_gettop(L) == 0) return l_fnv_1_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = fnv_1(a, len, v_1);
+ sprintf(digest,"%16llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_fnv_a(lua_State* L){
+ if(lua_gettop(L) == 0) return l_fnv_a_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = fnv_1(a, len, v_a);
+ sprintf(digest,"%16llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/jenkins.c b/src/hash/jenkins.c index b75d4e9..6775988 100644 --- a/src/hash/jenkins.c +++ b/src/hash/jenkins.c @@ -1,57 +1,57 @@ -#include "../util.h" -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct jenkins_oaat_hash jenkins_oaat_init(){ - return (struct jenkins_oaat_hash){.hash = 0}; -} - -void jenkins_oaat_update(uint8_t* in, size_t len, struct jenkins_oaat_hash* hash){ - for(int i = 0; i != len;){ - hash->hash += in[i++]; - hash->hash += hash->hash << 10; - hash->hash ^= hash->hash >> 6; - } -} - -uint32_t jenkins_oaat_final(struct jenkins_oaat_hash* hash){ - uint32_t h = hash->hash; - h += h << 3; - h ^= h >> 11; - h += h << 15; - - return h; -} - - -uint32_t jenkins_oaat(uint8_t* in, size_t len){ - struct jenkins_oaat_hash a = jenkins_oaat_init(); - jenkins_oaat_update(in, len, &a); - return jenkins_oaat_final(&a); -} - -lua_common_hash_clone(jenkins_oaat, oaat); -lua_common_hash_init_update(jenkins_oaat, oaat); - -int l_oaat_final(lua_State* L){ - struct jenkins_oaat_hash* a = (struct jenkins_oaat_hash*)lua_touserdata(L, 1); - uint32_t u = jenkins_oaat_final(a); - char digest[64]; - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_oaat(lua_State* L){ - if(lua_gettop(L) == 0) return l_oaat_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint32_t u = jenkins_oaat(a, len); - sprintf(digest,"%04x",u); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct jenkins_oaat_hash jenkins_oaat_init(){
+ return (struct jenkins_oaat_hash){.hash = 0};
+}
+
+void jenkins_oaat_update(uint8_t* in, size_t len, struct jenkins_oaat_hash* hash){
+ for(int i = 0; i != len;){
+ hash->hash += in[i++];
+ hash->hash += hash->hash << 10;
+ hash->hash ^= hash->hash >> 6;
+ }
+}
+
+uint32_t jenkins_oaat_final(struct jenkins_oaat_hash* hash){
+ uint32_t h = hash->hash;
+ h += h << 3;
+ h ^= h >> 11;
+ h += h << 15;
+
+ return h;
+}
+
+
+uint32_t jenkins_oaat(uint8_t* in, size_t len){
+ struct jenkins_oaat_hash a = jenkins_oaat_init();
+ jenkins_oaat_update(in, len, &a);
+ return jenkins_oaat_final(&a);
+}
+
+lua_common_hash_clone(jenkins_oaat, oaat);
+lua_common_hash_init_update(jenkins_oaat, oaat);
+
+int l_oaat_final(lua_State* L){
+ struct jenkins_oaat_hash* a = (struct jenkins_oaat_hash*)lua_touserdata(L, 1);
+ uint32_t u = jenkins_oaat_final(a);
+ char digest[64];
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_oaat(lua_State* L){
+ if(lua_gettop(L) == 0) return l_oaat_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint32_t u = jenkins_oaat(a, len);
+ sprintf(digest,"%04x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/loselose.c b/src/hash/loselose.c index bfdc017..9724e0c 100644 --- a/src/hash/loselose.c +++ b/src/hash/loselose.c @@ -1,50 +1,50 @@ -#include "../util.h" -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct loselose_hash loselose_init(){ - return (struct loselose_hash){.hash = 0}; -} - -void loselose_update(uint8_t* in, size_t len, struct loselose_hash* hash){ - for(int i = 0; i != len; i++){ - hash->hash += (uint64_t)*in; - in++; - } -} - -uint64_t loselose_final(struct loselose_hash* hash){ - return hash->hash; -} - -uint64_t loselose(uint8_t* in, size_t len){ - struct loselose_hash a = loselose_init(); - loselose_update(in, len, &a); - return loselose_final(&a); -} - -common_hash_clone(loselose); -common_hash_init_update(loselose); - -int l_loselose_final(lua_State* L){ - struct loselose_hash* a = (struct loselose_hash*)lua_touserdata(L, 1); - uint64_t u = loselose_final(a); - char digest[64]; - sprintf(digest,"%08lx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_loselose(lua_State* L){ - if(lua_gettop(L) == 0) return l_loselose_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = loselose(a, len); - sprintf(digest,"%08lx",u); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct loselose_hash loselose_init(){
+ return (struct loselose_hash){.hash = 0};
+}
+
+void loselose_update(uint8_t* in, size_t len, struct loselose_hash* hash){
+ for(int i = 0; i != len; i++){
+ hash->hash += (uint64_t)*in;
+ in++;
+ }
+}
+
+uint64_t loselose_final(struct loselose_hash* hash){
+ return hash->hash;
+}
+
+uint64_t loselose(uint8_t* in, size_t len){
+ struct loselose_hash a = loselose_init();
+ loselose_update(in, len, &a);
+ return loselose_final(&a);
+}
+
+common_hash_clone(loselose);
+common_hash_init_update(loselose);
+
+int l_loselose_final(lua_State* L){
+ struct loselose_hash* a = (struct loselose_hash*)lua_touserdata(L, 1);
+ uint64_t u = loselose_final(a);
+ char digest[64];
+ sprintf(digest,"%08lx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_loselose(lua_State* L){
+ if(lua_gettop(L) == 0) return l_loselose_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = loselose(a, len);
+ sprintf(digest,"%08lx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/md5.c b/src/hash/md5.c index 9dd4612..2e6ad98 100644 --- a/src/hash/md5.c +++ b/src/hash/md5.c @@ -1,167 +1,167 @@ -#include "../crypto.h" -#include <stdlib.h> -#include <string.h> -#include <stdint.h> - -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}; - -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}; - -#define bs 64 - -struct md5_hash md5_init(){ - struct md5_hash a = {.a0 = 0x67452301, .b0 = 0xefcdab89, .c0 = 0x98badcfe, .d0 = 0x10325476, .total = 0, .bufflen = 0}; - a.buffer = calloc(sizeof * a.buffer, bs); - return a; -} - - -struct md5_hash md5_init_l(lua_State* L){ - struct md5_hash a = {.a0 = 0x67452301, .b0 = 0xefcdab89, .c0 = 0x98badcfe, .d0 = 0x10325476, .total = 0, .bufflen = 0}; - a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs); - memset(a.buffer, 0, bs); - return a; -} - -void md5_round(struct md5_hash* hash){ - uint32_t* M = (uint32_t *)(hash->buffer); - - uint32_t A = hash->a0; - uint32_t B = hash->b0; - uint32_t C = hash->c0; - uint32_t D = hash->d0; - - for(int i = 0; i < 64; i++){ - uint32_t F, g; - - if(i < 16){ - F = (B & C) | ((~B) & D); - g = i; - } else if(i < 32){ - F = (D & B) | ((~D) & C); - g = (5*i + 1) % 16; - } else if(i < 48){ - F = B ^ C ^ D; - g = (3*i + 5) % 16; - } else { - F = C ^ (B | (~D)); - g = (7*i) % 16; - } - - - - F = F + A + K[i] + M[g]; - - uint32_t temp = D; - D = C; - C = B; - B = B + rotl32(F, s[i]); - A = temp; - } - - hash->a0 += A; - hash->b0 += B; - hash->c0 += C; - hash->d0 += D; -} - -void md5_update(uint8_t* input, size_t len, struct md5_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; - } - - for(; total_add >= bs;){ - memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen); - - total_add -= bs; - read += bs; - hash->bufflen = 0; - md5_round(hash); - } - - memset(hash->buffer, 0, bs); - if(total_add != 0){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } -} - -void md5_final(struct md5_hash* hash, char out_stream[64]){ - uint8_t old[bs]; - struct md5_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); - - hash->buffer[hash->bufflen] = 0x80; - - if(hash->bufflen > 55) { - //too large, needs another buffer - memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen); - md5_round(hash); - memset(hash->buffer, 0, 64); - } - - uint32_t lhhh = 8*hash->total; - memcpy(hash->buffer + 56, &lhhh, sizeof(lhhh)); - 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", - ((uint8_t*)&hash->a0)[0], ((uint8_t*)&hash->a0)[1], ((uint8_t*)&hash->a0)[2], ((uint8_t*)&hash->a0)[3], - ((uint8_t*)&hash->b0)[0], ((uint8_t*)&hash->b0)[1], ((uint8_t*)&hash->b0)[2], ((uint8_t*)&hash->b0)[3], - ((uint8_t*)&hash->c0)[0], ((uint8_t*)&hash->c0)[1], ((uint8_t*)&hash->c0)[2], ((uint8_t*)&hash->c0)[3], - ((uint8_t*)&hash->d0)[0], ((uint8_t*)&hash->d0)[1], ((uint8_t*)&hash->d0)[2], ((uint8_t*)&hash->d0)[3]); - - memcpy(hash->buffer, old, bs); - memcpy(hash, &old_hash, sizeof * hash); - -} - -common_hash_clone(md5); -lua_common_hash_init_ni(md5, md5, md5_init_l(L)); -lua_common_hash_update(md5, md5); -//common_hash_init_update(md5); - -int l_md5_final(lua_State* L){ - struct md5_hash* a = (struct md5_hash*)lua_touserdata(L, 1); - - char digest[128] = {0}; - md5_final(a, digest); - lua_pushstring(L, digest); - return 1; -} - -void md5(uint8_t* input, size_t len, char out_stream[64]){ - struct md5_hash aa = md5_init(); - md5_update(input, len, &aa); - md5_final(&aa, out_stream); - free(aa.buffer); -} - -int l_md5(lua_State* L){ - if(lua_gettop(L) == 0) return l_md5_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[128] = {0}; - md5(a, len, digest); - lua_pushstring(L, digest); - - return 1; -}; +#include "../crypto.h"
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+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};
+
+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};
+
+#define bs 64
+
+struct md5_hash md5_init(){
+ struct md5_hash a = {.a0 = 0x67452301, .b0 = 0xefcdab89, .c0 = 0x98badcfe, .d0 = 0x10325476, .total = 0, .bufflen = 0};
+ a.buffer = calloc(sizeof * a.buffer, bs);
+ return a;
+}
+
+
+struct md5_hash md5_init_l(lua_State* L){
+ struct md5_hash a = {.a0 = 0x67452301, .b0 = 0xefcdab89, .c0 = 0x98badcfe, .d0 = 0x10325476, .total = 0, .bufflen = 0};
+ a.buffer = lua_newuserdata(L, sizeof * a.buffer * bs);
+ memset(a.buffer, 0, bs);
+ return a;
+}
+
+void md5_round(struct md5_hash* hash){
+ uint32_t* M = (uint32_t *)(hash->buffer);
+
+ uint32_t A = hash->a0;
+ uint32_t B = hash->b0;
+ uint32_t C = hash->c0;
+ uint32_t D = hash->d0;
+
+ for(int i = 0; i < 64; i++){
+ uint32_t F, g;
+
+ if(i < 16){
+ F = (B & C) | ((~B) & D);
+ g = i;
+ } else if(i < 32){
+ F = (D & B) | ((~D) & C);
+ g = (5*i + 1) % 16;
+ } else if(i < 48){
+ F = B ^ C ^ D;
+ g = (3*i + 5) % 16;
+ } else {
+ F = C ^ (B | (~D));
+ g = (7*i) % 16;
+ }
+
+
+
+ F = F + A + K[i] + M[g];
+
+ uint32_t temp = D;
+ D = C;
+ C = B;
+ B = B + rotl32(F, s[i]);
+ A = temp;
+ }
+
+ hash->a0 += A;
+ hash->b0 += B;
+ hash->c0 += C;
+ hash->d0 += D;
+}
+
+void md5_update(uint8_t* input, size_t len, struct md5_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;
+ }
+
+ for(; total_add >= bs;){
+ memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
+
+ total_add -= bs;
+ read += bs;
+ hash->bufflen = 0;
+ md5_round(hash);
+ }
+
+ memset(hash->buffer, 0, bs);
+ if(total_add != 0){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
+}
+
+void md5_final(struct md5_hash* hash, char out_stream[64]){
+ uint8_t old[bs];
+ struct md5_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
+ hash->buffer[hash->bufflen] = 0x80;
+
+ if(hash->bufflen > 55) {
+ //too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen);
+ md5_round(hash);
+ memset(hash->buffer, 0, 64);
+ }
+
+ uint32_t lhhh = 8*hash->total;
+ memcpy(hash->buffer + 56, &lhhh, sizeof(lhhh));
+ 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",
+ ((uint8_t*)&hash->a0)[0], ((uint8_t*)&hash->a0)[1], ((uint8_t*)&hash->a0)[2], ((uint8_t*)&hash->a0)[3],
+ ((uint8_t*)&hash->b0)[0], ((uint8_t*)&hash->b0)[1], ((uint8_t*)&hash->b0)[2], ((uint8_t*)&hash->b0)[3],
+ ((uint8_t*)&hash->c0)[0], ((uint8_t*)&hash->c0)[1], ((uint8_t*)&hash->c0)[2], ((uint8_t*)&hash->c0)[3],
+ ((uint8_t*)&hash->d0)[0], ((uint8_t*)&hash->d0)[1], ((uint8_t*)&hash->d0)[2], ((uint8_t*)&hash->d0)[3]);
+
+ memcpy(hash->buffer, old, bs);
+ memcpy(hash, &old_hash, sizeof * hash);
+
+}
+
+common_hash_clone(md5);
+lua_common_hash_init_ni(md5, md5, md5_init_l(L));
+lua_common_hash_update(md5, md5);
+//common_hash_init_update(md5);
+
+int l_md5_final(lua_State* L){
+ struct md5_hash* a = (struct md5_hash*)lua_touserdata(L, 1);
+
+ char digest[128] = {0};
+ md5_final(a, digest);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+void md5(uint8_t* input, size_t len, char out_stream[64]){
+ struct md5_hash aa = md5_init();
+ md5_update(input, len, &aa);
+ md5_final(&aa, out_stream);
+ free(aa.buffer);
+}
+
+int l_md5(lua_State* L){
+ if(lua_gettop(L) == 0) return l_md5_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[128] = {0};
+ md5(a, len, digest);
+ lua_pushstring(L, digest);
+
+ return 1;
+};
diff --git a/src/hash/metrohash.c b/src/hash/metrohash.c index f473b95..e27dfe7 100644 --- a/src/hash/metrohash.c +++ b/src/hash/metrohash.c @@ -1,222 +1,222 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -#define u64(a) (*(uint64_t*)a) -#define u32(a) (*(uint32_t*)a) -#define u16(a) (*(uint16_t*)a) -#define u8(a) (*(uint8_t*)a) - -uint64_t metrohash64(uint8_t* in, size_t len, uint32_t seed, enum metrohash_version v){ - uint64_t k0, k1, k2, k3, inner_r, inner_r2; - if(v == v1){ - k0 = 0xC83A91E1; - k1 = 0x8648DBDB; - k2 = 0x7BDEC03B; - k3 = 0x2F5870A5; - inner_r = 33; - inner_r2 = 33; - } else { - k0 = 0xD6D018F5; - k1 = 0xA2AA033B; - k2 = 0x62992FC1; - k3 = 0x30BC5B29; - inner_r = 30; - inner_r2 = 29; - } - - uint8_t* end = in + len; - uint64_t hash = ((((uint64_t)seed) + k2) * k0) + len; - - if(len >= 32){ - uint64_t v[4]; - v[0] = hash; - v[1] = hash; - v[2] = hash; - v[3] = hash; - - for(; in <= (end - 32);){ - v[0] += u64(in) * k0; in += 8; v[0] = rot64(v[0],29) + v[2]; - v[1] += u64(in) * k1; in += 8; v[1] = rot64(v[1],29) + v[3]; - v[2] += u64(in) * k2; in += 8; v[2] = rot64(v[2],29) + v[0]; - v[3] += u64(in) * k3; in += 8; v[3] = rot64(v[3],29) + v[1]; - } - - v[2] ^= rot64(((v[0] + v[3]) * k0) + v[1], inner_r) * k1; - v[3] ^= rot64(((v[1] + v[2]) * k1) + v[0], inner_r) * k0; - v[0] ^= rot64(((v[0] + v[2]) * k0) + v[3], inner_r) * k1; - v[1] ^= rot64(((v[1] + v[3]) * k1) + v[2], inner_r) * k0; - hash += v[0] ^ v[1]; - } - - if ((end - in) >= 16){ - uint64_t v0 = hash + (u64(in) * (v == v1? k0 : k2)); in += 8; v0 = rot64(v0,inner_r2) * (v == v1? k1 : k3); - uint64_t v1 = hash + (u64(in) * (v == v1? k1 : k2)); in += 8; v1 = rot64(v1,inner_r2) * (v == v1? k2 : k3); - v0 ^= rot64(v0 * k0, (v == v1? 35 : 34)) + v1; - v1 ^= rot64(v1 * k3, (v == v1? 35 : 34)) + v0; - hash += v1; - } - - if ((end - in) >= 8){ - hash += u64(in) * k3; in += 8; - hash ^= rot64(hash, (v == v1? 33 : 36)) * k1; - - } - - if ((end - in) >= 4){ - hash += u32(in) * k3; in += 4; - hash ^= rot64(hash, 15) * k1; - } - - if ((end - in) >= 2){ - hash += u16(in) * k3; in += 2; - hash ^= rot64(hash, (v == v1? 13 : 15)) * k1; - } - - if ((end - in) >= 1){ - hash += u8(in) * k3; - hash ^= rot64(hash, (v == v1? 25 : 23)) * k1; - } - - hash ^= rot64(hash, (v == v1? 33 : 28)); - hash *= k0; - hash ^= rot64(hash, (v == v1? 33 : 29)); - - return hash; -} - -void metrohash128(uint8_t* in, size_t len, uint32_t seed, uint64_t *a, uint64_t *b, enum metrohash_version ver){ - uint64_t k0 = 0xC83A91E1; - uint64_t k1 = 0x8648DBDB; - uint64_t k2 = 0x7BDEC03B; - uint64_t k3 = 0x2F5870A5; - - if(ver == v2){ - k0 = 0xD6D018F5; - k1 = 0xA2AA033B; - k2 = 0x62992FC1; - k3 = 0x30BC5B29; - } - - uint8_t * end = in + len; - - uint64_t v[4]; - - v[0] = ((((uint64_t)seed) - k0) * k3) + len; - v[1] = ((((uint64_t)seed) + k1) * k2) + len; - - if(len >= 32){ - v[2] = ((((uint64_t)seed) + k0) * k2) + len; - v[3] = ((((uint64_t)seed) - k1) * k3) + len; - - for(;in <= end - 32;){ - v[0] += u64(in) * k0; in += 8; v[0] = rot64(v[0],29) + v[2]; - v[1] += u64(in) * k1; in += 8; v[1] = rot64(v[1],29) + v[3]; - v[2] += u64(in) * k2; in += 8; v[2] = rot64(v[2],29) + v[0]; - v[3] += u64(in) * k3; in += 8; v[3] = rot64(v[3],29) + v[1]; - } - - v[2] ^= rot64(((v[0] + v[3]) * k0) + v[1], ver == v1 ? 26 : 33) * k1; - v[3] ^= rot64(((v[1] + v[2]) * k1) + v[0], ver == v1 ? 26 : 33) * k0; - v[0] ^= rot64(((v[0] + v[2]) * k0) + v[3], ver == v1 ? 26 : 33) * k1; - v[1] ^= rot64(((v[1] + v[3]) * k1) + v[2], ver == v1 ? 26 : 33) * k0; - } - - if ((end - in) >= 16){ - v[0] += u64(in) * k2; in += 8; v[0] = rot64(v[0],ver == v1 ? 33 : 29) * k3; - v[1] += u64(in) * k2; in += 8; v[1] = rot64(v[1],ver == v1 ? 33 : 29) * k3; - v[0] ^= rot64((v[0] * k2) + v[1], ver == v1 ? 17 : 29) * k1; - v[1] ^= rot64((v[1] * k3) + v[0], ver == v1 ? 17 : 29) * k0; - } - - if ((end - in) >= 8){ - v[0] += u64(in) * k2; in += 8; v[0] = rot64(v[0],ver == v1 ? 33 : 29) * k3; - v[0] ^= rot64((v[0] * k2) + v[1], ver == v1 ? 20 : 29) * k1; - } - - if ((end - in) >= 4){ - v[1] += u32(in) * k2; in += 4; v[1] = rot64(v[1],ver == v1 ? 33 : 29) * k3; - v[1] ^= rot64((v[1] * k3) + v[0], ver == v1 ? 18 : 25) * k0; - } - - if ((end - in) >= 2){ - v[0] += u16(in) * k2; in += 2; v[0] = rot64(v[0],ver == v1 ? 33 : 29) * k3; - v[0] ^= rot64((v[0] * k2) + v[1], ver == v1 ? 24 : 30) * k1; - } - - if ((end - in) >= 1){ - v[1] += u8(in) * k2; v[1] = rot64(v[1],ver == v1 ? 33 : 29) * k3; - v[1] ^= rot64((v[1] * k3) + v[0], ver == v1 ? 24 : 18) * k0; - } - - v[0] += rot64((v[0] * k0) + v[1], ver == v1 ? 13 : 33); - v[1] += rot64((v[1] * k1) + v[0], ver == v1 ? 37 : 33); - v[0] += rot64((v[0] * k2) + v[1], ver == v1 ? 13 : 33); - v[1] += rot64((v[1] * k3) + v[0], ver == v1 ? 37 : 33); - - //printf("%llx %llx",v[0],v[1]); - *a = v[0]; - *b = v[1]; -} - -int l_metrohash64_v1(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - uint64_t seed = 0; - if(argv > 1) seed = luaL_checkinteger(L, 2); - - char digest[64]; - - uint64_t u = metrohash64(a, len, seed, v1); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_metrohash64_v2(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - uint64_t seed = 0; - if(argv > 1) seed = luaL_checkinteger(L, 2); - - char digest[64]; - - uint64_t u = metrohash64(a, len, seed, v2); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_metrohash128_v1(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - uint64_t seed = 0; - if(argv > 1) seed = luaL_checkinteger(L, 2); - - char digest[64]; - - uint64_t u1, u2; - metrohash128(a, len, seed, &u1, &u2, v1); - sprintf(digest,"%016llx%016llx",u1,u2); - lua_pushstring(L, digest); - return 1; -} - -int l_metrohash128_v2(lua_State* L){ - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - uint64_t seed = 0; - if(argv > 1) seed = luaL_checkinteger(L, 2); - - char digest[64]; - - uint64_t u1, u2; - metrohash128(a, len, seed, &u1, &u2, v2); - sprintf(digest,"%016llx%016llx",u1,u2); - lua_pushstring(L, digest); - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+#define u64(a) (*(uint64_t*)a)
+#define u32(a) (*(uint32_t*)a)
+#define u16(a) (*(uint16_t*)a)
+#define u8(a) (*(uint8_t*)a)
+
+uint64_t metrohash64(uint8_t* in, size_t len, uint32_t seed, enum metrohash_version v){
+ uint64_t k0, k1, k2, k3, inner_r, inner_r2;
+ if(v == v1){
+ k0 = 0xC83A91E1;
+ k1 = 0x8648DBDB;
+ k2 = 0x7BDEC03B;
+ k3 = 0x2F5870A5;
+ inner_r = 33;
+ inner_r2 = 33;
+ } else {
+ k0 = 0xD6D018F5;
+ k1 = 0xA2AA033B;
+ k2 = 0x62992FC1;
+ k3 = 0x30BC5B29;
+ inner_r = 30;
+ inner_r2 = 29;
+ }
+
+ uint8_t* end = in + len;
+ uint64_t hash = ((((uint64_t)seed) + k2) * k0) + len;
+
+ if(len >= 32){
+ uint64_t v[4];
+ v[0] = hash;
+ v[1] = hash;
+ v[2] = hash;
+ v[3] = hash;
+
+ for(; in <= (end - 32);){
+ v[0] += u64(in) * k0; in += 8; v[0] = rot64(v[0],29) + v[2];
+ v[1] += u64(in) * k1; in += 8; v[1] = rot64(v[1],29) + v[3];
+ v[2] += u64(in) * k2; in += 8; v[2] = rot64(v[2],29) + v[0];
+ v[3] += u64(in) * k3; in += 8; v[3] = rot64(v[3],29) + v[1];
+ }
+
+ v[2] ^= rot64(((v[0] + v[3]) * k0) + v[1], inner_r) * k1;
+ v[3] ^= rot64(((v[1] + v[2]) * k1) + v[0], inner_r) * k0;
+ v[0] ^= rot64(((v[0] + v[2]) * k0) + v[3], inner_r) * k1;
+ v[1] ^= rot64(((v[1] + v[3]) * k1) + v[2], inner_r) * k0;
+ hash += v[0] ^ v[1];
+ }
+
+ if ((end - in) >= 16){
+ uint64_t v0 = hash + (u64(in) * (v == v1? k0 : k2)); in += 8; v0 = rot64(v0,inner_r2) * (v == v1? k1 : k3);
+ uint64_t v1 = hash + (u64(in) * (v == v1? k1 : k2)); in += 8; v1 = rot64(v1,inner_r2) * (v == v1? k2 : k3);
+ v0 ^= rot64(v0 * k0, (v == v1? 35 : 34)) + v1;
+ v1 ^= rot64(v1 * k3, (v == v1? 35 : 34)) + v0;
+ hash += v1;
+ }
+
+ if ((end - in) >= 8){
+ hash += u64(in) * k3; in += 8;
+ hash ^= rot64(hash, (v == v1? 33 : 36)) * k1;
+
+ }
+
+ if ((end - in) >= 4){
+ hash += u32(in) * k3; in += 4;
+ hash ^= rot64(hash, 15) * k1;
+ }
+
+ if ((end - in) >= 2){
+ hash += u16(in) * k3; in += 2;
+ hash ^= rot64(hash, (v == v1? 13 : 15)) * k1;
+ }
+
+ if ((end - in) >= 1){
+ hash += u8(in) * k3;
+ hash ^= rot64(hash, (v == v1? 25 : 23)) * k1;
+ }
+
+ hash ^= rot64(hash, (v == v1? 33 : 28));
+ hash *= k0;
+ hash ^= rot64(hash, (v == v1? 33 : 29));
+
+ return hash;
+}
+
+void metrohash128(uint8_t* in, size_t len, uint32_t seed, uint64_t *a, uint64_t *b, enum metrohash_version ver){
+ uint64_t k0 = 0xC83A91E1;
+ uint64_t k1 = 0x8648DBDB;
+ uint64_t k2 = 0x7BDEC03B;
+ uint64_t k3 = 0x2F5870A5;
+
+ if(ver == v2){
+ k0 = 0xD6D018F5;
+ k1 = 0xA2AA033B;
+ k2 = 0x62992FC1;
+ k3 = 0x30BC5B29;
+ }
+
+ uint8_t * end = in + len;
+
+ uint64_t v[4];
+
+ v[0] = ((((uint64_t)seed) - k0) * k3) + len;
+ v[1] = ((((uint64_t)seed) + k1) * k2) + len;
+
+ if(len >= 32){
+ v[2] = ((((uint64_t)seed) + k0) * k2) + len;
+ v[3] = ((((uint64_t)seed) - k1) * k3) + len;
+
+ for(;in <= end - 32;){
+ v[0] += u64(in) * k0; in += 8; v[0] = rot64(v[0],29) + v[2];
+ v[1] += u64(in) * k1; in += 8; v[1] = rot64(v[1],29) + v[3];
+ v[2] += u64(in) * k2; in += 8; v[2] = rot64(v[2],29) + v[0];
+ v[3] += u64(in) * k3; in += 8; v[3] = rot64(v[3],29) + v[1];
+ }
+
+ v[2] ^= rot64(((v[0] + v[3]) * k0) + v[1], ver == v1 ? 26 : 33) * k1;
+ v[3] ^= rot64(((v[1] + v[2]) * k1) + v[0], ver == v1 ? 26 : 33) * k0;
+ v[0] ^= rot64(((v[0] + v[2]) * k0) + v[3], ver == v1 ? 26 : 33) * k1;
+ v[1] ^= rot64(((v[1] + v[3]) * k1) + v[2], ver == v1 ? 26 : 33) * k0;
+ }
+
+ if ((end - in) >= 16){
+ v[0] += u64(in) * k2; in += 8; v[0] = rot64(v[0],ver == v1 ? 33 : 29) * k3;
+ v[1] += u64(in) * k2; in += 8; v[1] = rot64(v[1],ver == v1 ? 33 : 29) * k3;
+ v[0] ^= rot64((v[0] * k2) + v[1], ver == v1 ? 17 : 29) * k1;
+ v[1] ^= rot64((v[1] * k3) + v[0], ver == v1 ? 17 : 29) * k0;
+ }
+
+ if ((end - in) >= 8){
+ v[0] += u64(in) * k2; in += 8; v[0] = rot64(v[0],ver == v1 ? 33 : 29) * k3;
+ v[0] ^= rot64((v[0] * k2) + v[1], ver == v1 ? 20 : 29) * k1;
+ }
+
+ if ((end - in) >= 4){
+ v[1] += u32(in) * k2; in += 4; v[1] = rot64(v[1],ver == v1 ? 33 : 29) * k3;
+ v[1] ^= rot64((v[1] * k3) + v[0], ver == v1 ? 18 : 25) * k0;
+ }
+
+ if ((end - in) >= 2){
+ v[0] += u16(in) * k2; in += 2; v[0] = rot64(v[0],ver == v1 ? 33 : 29) * k3;
+ v[0] ^= rot64((v[0] * k2) + v[1], ver == v1 ? 24 : 30) * k1;
+ }
+
+ if ((end - in) >= 1){
+ v[1] += u8(in) * k2; v[1] = rot64(v[1],ver == v1 ? 33 : 29) * k3;
+ v[1] ^= rot64((v[1] * k3) + v[0], ver == v1 ? 24 : 18) * k0;
+ }
+
+ v[0] += rot64((v[0] * k0) + v[1], ver == v1 ? 13 : 33);
+ v[1] += rot64((v[1] * k1) + v[0], ver == v1 ? 37 : 33);
+ v[0] += rot64((v[0] * k2) + v[1], ver == v1 ? 13 : 33);
+ v[1] += rot64((v[1] * k3) + v[0], ver == v1 ? 37 : 33);
+
+ //printf("%llx %llx",v[0],v[1]);
+ *a = v[0];
+ *b = v[1];
+}
+
+int l_metrohash64_v1(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+ uint64_t seed = 0;
+ if(argv > 1) seed = luaL_checkinteger(L, 2);
+
+ char digest[64];
+
+ uint64_t u = metrohash64(a, len, seed, v1);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_metrohash64_v2(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+ uint64_t seed = 0;
+ if(argv > 1) seed = luaL_checkinteger(L, 2);
+
+ char digest[64];
+
+ uint64_t u = metrohash64(a, len, seed, v2);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_metrohash128_v1(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+ uint64_t seed = 0;
+ if(argv > 1) seed = luaL_checkinteger(L, 2);
+
+ char digest[64];
+
+ uint64_t u1, u2;
+ metrohash128(a, len, seed, &u1, &u2, v1);
+ sprintf(digest,"%016llx%016llx",u1,u2);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_metrohash128_v2(lua_State* L){
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+ uint64_t seed = 0;
+ if(argv > 1) seed = luaL_checkinteger(L, 2);
+
+ char digest[64];
+
+ uint64_t u1, u2;
+ metrohash128(a, len, seed, &u1, &u2, v2);
+ sprintf(digest,"%016llx%016llx",u1,u2);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/pearson.c b/src/hash/pearson.c index d42c9fe..21c0c96 100644 --- a/src/hash/pearson.c +++ b/src/hash/pearson.c @@ -1,87 +1,87 @@ -#include "../util.h" -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> - -static uint8_t pearson_table[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, - 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38, - 39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62, - 63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86, - 87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107, - 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125, - 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, - 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161, - 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, - 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197, - 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215, - 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233, - 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251, - 252,253,254,255}; - -struct pearson_hash pearson_init(){ - return (struct pearson_hash){.ret = 0}; -} - -void pearson_update(uint8_t* aa, size_t len, struct pearson_hash* hash){ - for(int i = 0; i != len; i++) - hash->ret = pearson_table[(uint8_t)(hash->ret^aa[i])]; -} - -uint8_t pearson_final(struct pearson_hash* hash){ - return hash->ret; -} - -uint8_t pearson(uint8_t* aa, size_t len){ - struct pearson_hash a = pearson_init(); - pearson_update(aa, len, &a); - return pearson_final(&a); -} - -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); - } - - double s = 0; - for(int i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - pearson_table[i] = luaL_checknumber(L, -1); - lua_pop(L,1); - } - return 0; -} - -common_hash_clone(pearson); -common_hash_init_update(pearson); - -int l_pearson_final(lua_State* L){ - struct pearson_hash* a = (struct pearson_hash*)lua_touserdata(L, 1); - uint8_t u = pearson_final(a); - char digest[8]; - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - return 1; -} - -int l_pearson(lua_State* L){ - if(lua_gettop(L) == 0) return l_pearson_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[8]; - uint8_t u = pearson(a, len); - - sprintf(digest,"%x",u); - - lua_pushstring(L, digest); - - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+static uint8_t pearson_table[256] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
+ 15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,
+ 39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,
+ 63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,
+ 87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,
+ 108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
+ 126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
+ 144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
+ 162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
+ 180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
+ 198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
+ 216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
+ 234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
+ 252,253,254,255};
+
+struct pearson_hash pearson_init(){
+ return (struct pearson_hash){.ret = 0};
+}
+
+void pearson_update(uint8_t* aa, size_t len, struct pearson_hash* hash){
+ for(int i = 0; i != len; i++)
+ hash->ret = pearson_table[(uint8_t)(hash->ret^aa[i])];
+}
+
+uint8_t pearson_final(struct pearson_hash* hash){
+ return hash->ret;
+}
+
+uint8_t pearson(uint8_t* aa, size_t len){
+ struct pearson_hash a = pearson_init();
+ pearson_update(aa, len, &a);
+ return pearson_final(&a);
+}
+
+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);
+ }
+
+ double s = 0;
+ for(int i = 0; i <= len-1; i++){
+
+ lua_pushinteger(L,i+1);
+ lua_gettable(L,1);
+
+ pearson_table[i] = luaL_checknumber(L, -1);
+ lua_pop(L,1);
+ }
+ return 0;
+}
+
+common_hash_clone(pearson);
+common_hash_init_update(pearson);
+
+int l_pearson_final(lua_State* L){
+ struct pearson_hash* a = (struct pearson_hash*)lua_touserdata(L, 1);
+ uint8_t u = pearson_final(a);
+ char digest[8];
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_pearson(lua_State* L){
+ if(lua_gettop(L) == 0) return l_pearson_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[8];
+ uint8_t u = pearson(a, len);
+
+ sprintf(digest,"%x",u);
+
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/pjw.c b/src/hash/pjw.c index ca3341a..c96354d 100644 --- a/src/hash/pjw.c +++ b/src/hash/pjw.c @@ -1,53 +1,53 @@ -#include "../util.h" -#include "../crypto.h" - -#include <stdio.h> -#include <stdint.h> - -struct pjw_hash pjw_init(){ - return (struct pjw_hash){.hash = 0, .high = 0}; -} - -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; - } -} - -uint32_t pjw_final(struct pjw_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); -} - -common_hash_clone(pjw); -common_hash_init_update(pjw); - -int l_pjw_final(lua_State* L){ - struct pjw_hash* a = (struct pjw_hash*)lua_touserdata(L, 1); - uint32_t u = pjw_final(a); - char digest[32]; - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} - -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); - - char digest[32]; - - uint32_t u = pjw(a, len); - sprintf(digest,"%08x",u); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+
+#include <stdio.h>
+#include <stdint.h>
+
+struct pjw_hash pjw_init(){
+ return (struct pjw_hash){.hash = 0, .high = 0};
+}
+
+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;
+ }
+}
+
+uint32_t pjw_final(struct pjw_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);
+}
+
+common_hash_clone(pjw);
+common_hash_init_update(pjw);
+
+int l_pjw_final(lua_State* L){
+ struct pjw_hash* a = (struct pjw_hash*)lua_touserdata(L, 1);
+ uint32_t u = pjw_final(a);
+ char digest[32];
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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);
+
+ char digest[32];
+
+ uint32_t u = pjw(a, len);
+ sprintf(digest,"%08x",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/sdbm.c b/src/hash/sdbm.c index 9f9d08f..a2f1acc 100644 --- a/src/hash/sdbm.c +++ b/src/hash/sdbm.c @@ -1,50 +1,50 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct sdbm_hash sdbm_init(){ - return (struct sdbm_hash){.hash = 0}; -} - -void sdbm_update(uint8_t* in, size_t len, struct sdbm_hash* hash){ - for(int i = 0; i != len; i++){ - hash->hash = (uint64_t)*in + (hash->hash << 6) + (hash->hash << 16) - hash->hash; - in++; - } -} - -uint64_t sdbm_final(struct sdbm_hash* hash){ - return hash->hash; -} - -uint64_t sdbm(uint8_t* in, size_t len){ - struct sdbm_hash a = sdbm_init(); - sdbm_update(in, len, &a); - return sdbm_final(&a); -} - -common_hash_clone(sdbm); -common_hash_init_update(sdbm); - -int l_sdbm_final(lua_State* L){ - struct sdbm_hash* a = (struct sdbm_hash*)lua_touserdata(L, 1); - uint64_t u = sdbm_final(a); - char digest[64]; - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} - -int l_sdbm(lua_State* L){ - if(lua_gettop(L) == 0) return l_sdbm_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - - char digest[64]; - - uint64_t u = sdbm(a, len); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - return 1; -} - +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct sdbm_hash sdbm_init(){
+ return (struct sdbm_hash){.hash = 0};
+}
+
+void sdbm_update(uint8_t* in, size_t len, struct sdbm_hash* hash){
+ for(int i = 0; i != len; i++){
+ hash->hash = (uint64_t)*in + (hash->hash << 6) + (hash->hash << 16) - hash->hash;
+ in++;
+ }
+}
+
+uint64_t sdbm_final(struct sdbm_hash* hash){
+ return hash->hash;
+}
+
+uint64_t sdbm(uint8_t* in, size_t len){
+ struct sdbm_hash a = sdbm_init();
+ sdbm_update(in, len, &a);
+ return sdbm_final(&a);
+}
+
+common_hash_clone(sdbm);
+common_hash_init_update(sdbm);
+
+int l_sdbm_final(lua_State* L){
+ struct sdbm_hash* a = (struct sdbm_hash*)lua_touserdata(L, 1);
+ uint64_t u = sdbm_final(a);
+ char digest[64];
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_sdbm(lua_State* L){
+ if(lua_gettop(L) == 0) return l_sdbm_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+
+ char digest[64];
+
+ uint64_t u = sdbm(a, len);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
diff --git a/src/hash/sha01.c b/src/hash/sha01.c index d382465..9ba3e6c 100644 --- a/src/hash/sha01.c +++ b/src/hash/sha01.c @@ -1,223 +1,223 @@ -#include "../crypto.h" -#include <stdlib.h> -#include <string.h> -#include <stdint.h> - -#define bs 64 - -struct sha01_hash { - uint8_t* buffer; - uint32_t h0, h1, h2, h3, h4; - size_t bufflen; - size_t total; - uint8_t version; -}; - -#define sha0_hash 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 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 = lua_newuserdata(L, 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(int x = 0;t>=0; x++){ - 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; - } - - 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){ - 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; - sha01_round(hash); - } - - memset(hash->buffer, 0, bs); - - if(0 != total_add){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } -} - -void sha01_final(struct sha01_hash* hash, char* out_stream){ - uint8_t old[bs]; - struct sha01_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); - - hash->buffer[hash->bufflen] = 0x80; - - if(hash->bufflen > 55) { - //too large, needs another buffer - memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen); - sha01_round(hash); - memset(hash->buffer, 0, 64); - } - - size_t lhhh = 8*hash->total; - for(int i = 0; i != 8; i++) - 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); - - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); -} - -struct sha01_hash sha0_init(){ - return sha01_init(0); -} - -struct sha01_hash sha1_init(){ - return sha01_init(1); -} - -void sha0_update(uint8_t* input, size_t len, struct sha01_hash* hash){ - sha01_update(input, len, hash); -} - -void sha1_update(uint8_t* input, size_t len, struct sha01_hash* hash){ - sha01_update(input, len, hash); -} - -void sha0_final(struct sha01_hash* hash, char* out_stream){ - sha01_final(hash, out_stream); -} - -void sha1_final(struct sha01_hash* hash, char* 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); -} - -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); -} - -common_hash_clone(sha1); -lua_common_hash_init_ni(sha1, sha1, sha01_init_l(1, L)); -lua_common_hash_update(sha1, sha1); - -common_hash_clone(sha0); -lua_common_hash_init_ni(sha0, sha0, sha01_init_l(0, L)); -lua_common_hash_update(sha0, sha0); - -int l_sha1_final(lua_State* L){ - struct sha01_hash* a = (struct sha01_hash*)lua_touserdata(L, 1); - - char digest[160]; - sha1_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -int l_sha0_final(lua_State* L){ - return l_sha1_final(L); -} - -int l_sha1(lua_State* L){ - if(lua_gettop(L) == 0) return l_sha1_init(L); - size_t len = 0; - char* a = (char*)luaL_checklstring(L, 1, &len); - - char digest[160]; - - sha1((uint8_t*)a, len, digest); - lua_pushstring(L, digest); - - return 1; -}; - -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); - lua_pushstring(L, digest); - - return 1; -}; +#include "../crypto.h"
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#define bs 64
+
+struct sha01_hash {
+ uint8_t* buffer;
+ uint32_t h0, h1, h2, h3, h4;
+ size_t bufflen;
+ size_t total;
+ uint8_t version;
+};
+
+#define sha0_hash 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 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 = lua_newuserdata(L, 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(int x = 0;t>=0; x++){
+ 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;
+ }
+
+ 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){
+ 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;
+ sha01_round(hash);
+ }
+
+ memset(hash->buffer, 0, bs);
+
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
+}
+
+void sha01_final(struct sha01_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct sha01_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
+ hash->buffer[hash->bufflen] = 0x80;
+
+ if(hash->bufflen > 55) {
+ //too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, 64 - hash->bufflen);
+ sha01_round(hash);
+ memset(hash->buffer, 0, 64);
+ }
+
+ size_t lhhh = 8*hash->total;
+ for(int i = 0; i != 8; i++)
+ 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);
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
+}
+
+struct sha01_hash sha0_init(){
+ return sha01_init(0);
+}
+
+struct sha01_hash sha1_init(){
+ return sha01_init(1);
+}
+
+void sha0_update(uint8_t* input, size_t len, struct sha01_hash* hash){
+ sha01_update(input, len, hash);
+}
+
+void sha1_update(uint8_t* input, size_t len, struct sha01_hash* hash){
+ sha01_update(input, len, hash);
+}
+
+void sha0_final(struct sha01_hash* hash, char* out_stream){
+ sha01_final(hash, out_stream);
+}
+
+void sha1_final(struct sha01_hash* hash, char* 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);
+}
+
+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);
+}
+
+common_hash_clone(sha1);
+lua_common_hash_init_ni(sha1, sha1, sha01_init_l(1, L));
+lua_common_hash_update(sha1, sha1);
+
+common_hash_clone(sha0);
+lua_common_hash_init_ni(sha0, sha0, sha01_init_l(0, L));
+lua_common_hash_update(sha0, sha0);
+
+int l_sha1_final(lua_State* L){
+ struct sha01_hash* a = (struct sha01_hash*)lua_touserdata(L, 1);
+
+ char digest[160];
+ sha1_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_sha0_final(lua_State* L){
+ return l_sha1_final(L);
+}
+
+int l_sha1(lua_State* L){
+ if(lua_gettop(L) == 0) return l_sha1_init(L);
+ size_t len = 0;
+ char* a = (char*)luaL_checklstring(L, 1, &len);
+
+ char digest[160];
+
+ sha1((uint8_t*)a, len, digest);
+ lua_pushstring(L, digest);
+
+ return 1;
+};
+
+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);
+ lua_pushstring(L, digest);
+
+ return 1;
+};
diff --git a/src/hash/sha2-256.c b/src/hash/sha2-256.c index 6086fd7..7e37da9 100644 --- a/src/hash/sha2-256.c +++ b/src/hash/sha2-256.c @@ -1,348 +1,348 @@ -#include "../util.h" -#include "../crypto.h" -#include <stdlib.h> -#include <string.h> -#include <stdint.h> -#include <stdio.h> - -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}; - -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]; - } -} - -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]; - } -} - -#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; -} - -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 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 = lua_newuserdata(L, sizeof * a.buffer * bs); - memset(a.buffer, 0, bs); - return a; -} - -struct sha512_hash sha512_init(){ - return sha512_t_init(sha512_iv); -} - -struct sha512_hash sha384_init(){ - return sha512_t_init(sha384_iv); -} - -void sha512_update(uint8_t* input, size_t len, struct sha512_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; - } - - for(; total_add >= bs;){ - memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen); - total_add -= bs; - hash->bufflen = 0; - read += bs; - sha512_round(hash); - } - - memset(hash->buffer, 0, bs); - - if(0 != total_add){ - memcpy(hash->buffer, input + read, total_add); - hash->bufflen = total_add; - } -} - -void _sha512_t_final(struct sha512_hash* hash){ - hash->buffer[hash->bufflen] = 0x80; - - if(hash->bufflen > bs - 16) { - //too large, needs another buffer - memset(hash->buffer + hash->bufflen + 1, 0, bs - hash->bufflen); - 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); -} - -void sha512_final(struct sha512_hash* hash, char* out_stream){ - uint8_t old[bs]; - struct sha512_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); - - _sha512_t_final(hash); - - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h0); - 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); - - memcpy(hash, &old_hash, sizeof * hash); - memcpy(hash->buffer, old, bs); -} - -void sha384_final(struct sha512_hash* hash, char* out_stream){ - uint8_t old[bs]; - struct sha512_hash old_hash; - memcpy(&old_hash, hash, sizeof * hash); - memcpy(old, hash->buffer, bs); - _sha512_t_final(hash); - - - sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h0); - 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); - - 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); -} - -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); -} - -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 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 nh[512] = {0}; - 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); -lua_common_hash_init_ni(sha512, sha512, sha512_t_init_l(sha512_iv, L)); -lua_common_hash_update(sha512, sha512); - -int l_sha512_final(lua_State* L){ - struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1); - - char digest[512] = {0}; - sha512_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -common_hash_clone(sha384); -lua_common_hash_init_ni(sha384, sha384, sha512_t_init_l(sha384_iv, L)); -lua_common_hash_update(sha384, sha384); - -int l_sha384_final(lua_State* L){ - struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1); - - char digest[384] = {0}; - sha384_final(a, digest); - - lua_pushstring(L, digest); - return 1; -} - -int l_sha512_t_clone(lua_State* L){ - struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1); - lua_pushinteger(L, a->t); - l_sha512_t_init(L); - struct sha512_hash* b = (struct sha512_hash*)lua_touserdata(L, -1); - *b = *a; - return 1; -} - -lua_common_hash_meta(sha512_t); -int l_sha512_t_init(lua_State* L){ - int tt = luaL_checkinteger(L, -1); - lua_newtable(L); - int t = lua_gettop(L); - - struct sha512_hash* a = (struct sha512_hash*)lua_newuserdata(L, sizeof * a);\ - 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); - - lua_pushvalue(L, ud); - return 1; -} - -lua_common_hash_update(sha512, sha512_t); - -int l_sha512_t_final(lua_State* L){ - struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1); - - char digest[512] = {0}; - sha512_final(a, digest); - digest[a->t/4] = '\0'; - - lua_pushstring(L, digest); - return 1; -} - -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; -} - -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); - - char digest[384] = {0}; - - sha384(a, len, digest); - lua_pushstring(L, digest); - return 1; -} - -int l_sha512_t(lua_State* L){ - if(lua_gettop(L) == 1) return l_sha512_t_init(L); - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - uint64_t t = luaL_checkinteger(L, 2); - - char digest[512] = {0}; - - sha512_t(a, len, t, digest); - lua_pushstring(L, digest); - return 1; -} +#include "../util.h"
+#include "../crypto.h"
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdio.h>
+
+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};
+
+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];
+ }
+}
+
+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];
+ }
+}
+
+#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;
+}
+
+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 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 = lua_newuserdata(L, sizeof * a.buffer * bs);
+ memset(a.buffer, 0, bs);
+ return a;
+}
+
+struct sha512_hash sha512_init(){
+ return sha512_t_init(sha512_iv);
+}
+
+struct sha512_hash sha384_init(){
+ return sha512_t_init(sha384_iv);
+}
+
+void sha512_update(uint8_t* input, size_t len, struct sha512_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;
+ }
+
+ for(; total_add >= bs;){
+ memcpy(hash->buffer + hash->bufflen, input + read, bs - hash->bufflen);
+ total_add -= bs;
+ hash->bufflen = 0;
+ read += bs;
+ sha512_round(hash);
+ }
+
+ memset(hash->buffer, 0, bs);
+
+ if(0 != total_add){
+ memcpy(hash->buffer, input + read, total_add);
+ hash->bufflen = total_add;
+ }
+}
+
+void _sha512_t_final(struct sha512_hash* hash){
+ hash->buffer[hash->bufflen] = 0x80;
+
+ if(hash->bufflen > bs - 16) {
+ //too large, needs another buffer
+ memset(hash->buffer + hash->bufflen + 1, 0, bs - hash->bufflen);
+ 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);
+}
+
+void sha512_final(struct sha512_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct sha512_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+
+ _sha512_t_final(hash);
+
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h0);
+ 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);
+
+ memcpy(hash, &old_hash, sizeof * hash);
+ memcpy(hash->buffer, old, bs);
+}
+
+void sha384_final(struct sha512_hash* hash, char* out_stream){
+ uint8_t old[bs];
+ struct sha512_hash old_hash;
+ memcpy(&old_hash, hash, sizeof * hash);
+ memcpy(old, hash->buffer, bs);
+ _sha512_t_final(hash);
+
+
+ sprintf((char*)out_stream, "%s%016llx", out_stream, hash->h0);
+ 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);
+
+ 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);
+}
+
+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);
+}
+
+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 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 nh[512] = {0};
+ 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);
+lua_common_hash_init_ni(sha512, sha512, sha512_t_init_l(sha512_iv, L));
+lua_common_hash_update(sha512, sha512);
+
+int l_sha512_final(lua_State* L){
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1);
+
+ char digest[512] = {0};
+ sha512_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+common_hash_clone(sha384);
+lua_common_hash_init_ni(sha384, sha384, sha512_t_init_l(sha384_iv, L));
+lua_common_hash_update(sha384, sha384);
+
+int l_sha384_final(lua_State* L){
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1);
+
+ char digest[384] = {0};
+ sha384_final(a, digest);
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_sha512_t_clone(lua_State* L){
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, -1);
+ lua_pushinteger(L, a->t);
+ l_sha512_t_init(L);
+ struct sha512_hash* b = (struct sha512_hash*)lua_touserdata(L, -1);
+ *b = *a;
+ return 1;
+}
+
+lua_common_hash_meta(sha512_t);
+int l_sha512_t_init(lua_State* L){
+ int tt = luaL_checkinteger(L, -1);
+ lua_newtable(L);
+ int t = lua_gettop(L);
+
+ struct sha512_hash* a = (struct sha512_hash*)lua_newuserdata(L, sizeof * a);\
+ 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);
+
+ lua_pushvalue(L, ud);
+ return 1;
+}
+
+lua_common_hash_update(sha512, sha512_t);
+
+int l_sha512_t_final(lua_State* L){
+ struct sha512_hash* a = (struct sha512_hash*)lua_touserdata(L, 1);
+
+ char digest[512] = {0};
+ sha512_final(a, digest);
+ digest[a->t/4] = '\0';
+
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+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;
+}
+
+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);
+
+ char digest[384] = {0};
+
+ sha384(a, len, digest);
+ lua_pushstring(L, digest);
+ return 1;
+}
+
+int l_sha512_t(lua_State* L){
+ if(lua_gettop(L) == 1) return l_sha512_t_init(L);
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ uint64_t t = luaL_checkinteger(L, 2);
+
+ char digest[512] = {0};
+
+ sha512_t(a, len, t, digest);
+ lua_pushstring(L, digest);
+ return 1;
+}
diff --git a/src/hash/sysvchecksum.c b/src/hash/sysvchecksum.c index 221a6dd..661ee8d 100644 --- a/src/hash/sysvchecksum.c +++ b/src/hash/sysvchecksum.c @@ -1,50 +1,50 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> -#include <math.h> - -struct sysvchecksum_hash sysvchecksum_init(){ - return (struct sysvchecksum_hash){.check = 0}; -} - -void sysvchecksum_update(uint8_t* aa, size_t len, struct sysvchecksum_hash* hash){ - for(int i = 0; i != len; i++) - hash->check += aa[i]; -} - -uint32_t sysvchecksum_final(struct sysvchecksum_hash* hash){ - uint32_t r = hash->check % (int)pow(2,16) + (hash->check % (int)pow(2,32)) / (int)pow(2,16); - return (r % (int)pow(2,16)) + r / (int)pow(2,16); -} - -uint32_t sysvchecksum(uint8_t* aa, size_t len){ - struct sysvchecksum_hash a = sysvchecksum_init(); - sysvchecksum_update(aa, len, &a); - return sysvchecksum_final(&a); -} - -common_hash_clone(sysvchecksum); -common_hash_init_update(sysvchecksum); - -int l_sysvchecksum_final(lua_State* L){ - struct sysvchecksum_hash* a = (struct sysvchecksum_hash*)lua_touserdata(L, 1); - uint32_t u = sysvchecksum_final(a); - char digest[32]; - sprintf(digest,"%x",u); - 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[32]; - - uint32_t u = sysvchecksum(a, len); - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+#include <math.h>
+
+struct sysvchecksum_hash sysvchecksum_init(){
+ return (struct sysvchecksum_hash){.check = 0};
+}
+
+void sysvchecksum_update(uint8_t* aa, size_t len, struct sysvchecksum_hash* hash){
+ for(int i = 0; i != len; i++)
+ hash->check += aa[i];
+}
+
+uint32_t sysvchecksum_final(struct sysvchecksum_hash* hash){
+ uint32_t r = hash->check % (int)pow(2,16) + (hash->check % (int)pow(2,32)) / (int)pow(2,16);
+ return (r % (int)pow(2,16)) + r / (int)pow(2,16);
+}
+
+uint32_t sysvchecksum(uint8_t* aa, size_t len){
+ struct sysvchecksum_hash a = sysvchecksum_init();
+ sysvchecksum_update(aa, len, &a);
+ return sysvchecksum_final(&a);
+}
+
+common_hash_clone(sysvchecksum);
+common_hash_init_update(sysvchecksum);
+
+int l_sysvchecksum_final(lua_State* L){
+ struct sysvchecksum_hash* a = (struct sysvchecksum_hash*)lua_touserdata(L, 1);
+ uint32_t u = sysvchecksum_final(a);
+ char digest[32];
+ sprintf(digest,"%x",u);
+ 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[32];
+
+ uint32_t u = sysvchecksum(a, len);
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/xor.c b/src/hash/xor.c index 205408f..fab17d7 100644 --- a/src/hash/xor.c +++ b/src/hash/xor.c @@ -1,48 +1,48 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -struct xor8_hash xor8_init(){ - return (struct xor8_hash){.a = 0}; -} - -void xor8_update(uint8_t* aa, size_t len, struct xor8_hash* hash){ - for(int i = 0; i != len; i++) - hash->a += aa[i] & 0xff; -} - -uint8_t xor8_final(struct xor8_hash* hash){ - return ((hash->a ^ 0xff) + 1) & 0xff; -} - -uint8_t xor8(uint8_t* aa, size_t len){ - struct xor8_hash a = xor8_init(); - xor8_update(aa, len, &a); - return xor8_final(&a); -} - -common_hash_clone(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(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); - sprintf(digest,"%02x",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+struct xor8_hash xor8_init(){
+ return (struct xor8_hash){.a = 0};
+}
+
+void xor8_update(uint8_t* aa, size_t len, struct xor8_hash* hash){
+ for(int i = 0; i != len; i++)
+ hash->a += aa[i] & 0xff;
+}
+
+uint8_t xor8_final(struct xor8_hash* hash){
+ return ((hash->a ^ 0xff) + 1) & 0xff;
+}
+
+uint8_t xor8(uint8_t* aa, size_t len){
+ struct xor8_hash a = xor8_init();
+ xor8_update(aa, len, &a);
+ return xor8_final(&a);
+}
+
+common_hash_clone(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(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);
+ sprintf(digest,"%02x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
diff --git a/src/hash/xxh.c b/src/hash/xxh.c index deb9709..052db3f 100644 --- a/src/hash/xxh.c +++ b/src/hash/xxh.c @@ -1,154 +1,154 @@ -#include "../crypto.h" -#include <stdio.h> -#include <stdint.h> - -#define max_buffer_size32 16 -#define max_buffer_size64 32 -static inline uint32_t rol32(uint32_t x, unsigned char bits){ - return (x << bits) | (x >> (32 - bits)); -} -static inline uint64_t rol64(uint64_t x, unsigned char bits){ - return (x << bits) | (x >> (64 - bits)); -} - -uint32_t i_xxhash32(uint8_t *data, uint32_t seed, size_t len){ - const uint32_t prime1 = 2654435761U; - const uint32_t prime2 = 2246822519U; - const uint32_t prime3 = 3266489917U; - const uint32_t prime4 = 668265263U; - const uint32_t prime5 = 374761393U; - - uint32_t state[4]; - uint32_t result = len; - - uint8_t* stop = data + len; - uint8_t* stop_block = stop - max_buffer_size32; - - state[0] = seed + prime1 + prime2; - state[1] = seed + prime2; - state[2] = seed; - state[3] = seed - prime1; - - for(;data <= stop_block;){ - state[0] = rol32(state[0] + data[0] * prime2, 13) * prime1; - state[1] = rol32(state[1] + data[1] * prime2, 13) * prime1; - state[2] = rol32(state[2] + data[2] * prime2, 13) * prime1; - state[3] = rol32(state[3] + data[3] * prime2, 13) * prime1; - - data += 16; - } - - if(len >= max_buffer_size32){ - result += rol32(state[0], 1) + rol32(state[1], 7) + - rol32(state[2], 12) + rol32(state[3], 18); - } else result += state[2] + prime5; - - for (; data + 4 <= stop; data += 4) - result = rol32(result + *(uint32_t*)data * prime3, 17) * prime4; - - for(;data != stop;) - result = rol32(result + (*data++) * prime5, 11) * prime1; - - result ^= result >> 15; - result *= prime2; - result ^= result >> 13; - result *= prime3; - result ^= result >> 16; - - return result; -} - -#define pr64(u1,u2) rol64((u1) + (u2) * prime2, 31) * prime1 -uint64_t i_xxhash64(uint8_t *data, uint64_t seed, uint64_t len){ - const uint64_t prime1 = 11400714785074694791ULL; - const uint64_t prime2 = 14029467366897019727ULL; - const uint64_t prime3 = 1609587929392839161ULL; - const uint64_t prime4 = 9650029242287828579ULL; - const uint64_t prime5 = 2870177450012600261ULL; - - uint64_t state[4]; - uint64_t result; - - uint8_t* stop = data + len; - uint8_t* stop_block = stop - max_buffer_size64; - - state[0] = seed + prime1 + prime2; - state[1] = seed + prime2; - state[2] = seed; - state[3] = seed - prime1; - - for(;data <= stop_block;){ - state[0] = pr64(state[0], data[0]); - state[1] = pr64(state[1], data[1]); - state[2] = pr64(state[2], data[2]); - state[3] = pr64(state[3], data[3]); - - data += 32; - } - - if(len >= max_buffer_size64){ - result = rol64(state[0], 1) + rol64(state[1], 7) + - rol64(state[2], 12) + rol64(state[3], 18); - result = (result ^ pr64(0, state[0])) * prime1 + prime4; - result = (result ^ pr64(0, state[1])) * prime1 + prime4; - result = (result ^ pr64(0, state[2])) * prime1 + prime4; - result = (result ^ pr64(0, state[3])) * prime1 + prime4; - } else { - result = state[2] + prime5; - } - result += len; - - for (; data + 8 <= stop; data += 8) - result = rol64(result ^ pr64(0, *(uint64_t*)data), 27) * prime1 + prime4; - - if (data + 4 <= stop){ - result = rol64(result ^ (*(uint32_t*)data) * prime1, 23) * prime2 + prime3; - data += 4; - } - - for(;data != stop;) - result = rol64(result ^ (*data++) * prime5, 11) * prime1; - - result ^= result >> 33; - result *= prime2; - result ^= result >> 29; - result *= prime3; - result ^= result >> 32; - return result; -} - -int l_xxh64(lua_State* L){ - - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - - uint64_t seed = 0; - if(argv>1) seed = luaL_checkinteger(L, 2); - - char digest[64]; - - uint64_t u = i_xxhash64(a, seed, len); - sprintf(digest,"%016llx",u); - lua_pushstring(L, digest); - - return 1; -} - -int l_xxh32(lua_State* L){ - - size_t len = 0; - uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len); - int argv = lua_gettop(L); - - uint32_t seed = 0; - if(argv>1) seed = luaL_checkinteger(L, 2); - - char digest[32]; - - uint32_t u = i_xxhash32(a, seed, len); - sprintf(digest,"%x",u); - lua_pushstring(L, digest); - - return 1; -} +#include "../crypto.h"
+#include <stdio.h>
+#include <stdint.h>
+
+#define max_buffer_size32 16
+#define max_buffer_size64 32
+static inline uint32_t rol32(uint32_t x, unsigned char bits){
+ return (x << bits) | (x >> (32 - bits));
+}
+static inline uint64_t rol64(uint64_t x, unsigned char bits){
+ return (x << bits) | (x >> (64 - bits));
+}
+
+uint32_t i_xxhash32(uint8_t *data, uint32_t seed, size_t len){
+ const uint32_t prime1 = 2654435761U;
+ const uint32_t prime2 = 2246822519U;
+ const uint32_t prime3 = 3266489917U;
+ const uint32_t prime4 = 668265263U;
+ const uint32_t prime5 = 374761393U;
+
+ uint32_t state[4];
+ uint32_t result = len;
+
+ uint8_t* stop = data + len;
+ uint8_t* stop_block = stop - max_buffer_size32;
+
+ state[0] = seed + prime1 + prime2;
+ state[1] = seed + prime2;
+ state[2] = seed;
+ state[3] = seed - prime1;
+
+ for(;data <= stop_block;){
+ state[0] = rol32(state[0] + data[0] * prime2, 13) * prime1;
+ state[1] = rol32(state[1] + data[1] * prime2, 13) * prime1;
+ state[2] = rol32(state[2] + data[2] * prime2, 13) * prime1;
+ state[3] = rol32(state[3] + data[3] * prime2, 13) * prime1;
+
+ data += 16;
+ }
+
+ if(len >= max_buffer_size32){
+ result += rol32(state[0], 1) + rol32(state[1], 7) +
+ rol32(state[2], 12) + rol32(state[3], 18);
+ } else result += state[2] + prime5;
+
+ for (; data + 4 <= stop; data += 4)
+ result = rol32(result + *(uint32_t*)data * prime3, 17) * prime4;
+
+ for(;data != stop;)
+ result = rol32(result + (*data++) * prime5, 11) * prime1;
+
+ result ^= result >> 15;
+ result *= prime2;
+ result ^= result >> 13;
+ result *= prime3;
+ result ^= result >> 16;
+
+ return result;
+}
+
+#define pr64(u1,u2) rol64((u1) + (u2) * prime2, 31) * prime1
+uint64_t i_xxhash64(uint8_t *data, uint64_t seed, uint64_t len){
+ const uint64_t prime1 = 11400714785074694791ULL;
+ const uint64_t prime2 = 14029467366897019727ULL;
+ const uint64_t prime3 = 1609587929392839161ULL;
+ const uint64_t prime4 = 9650029242287828579ULL;
+ const uint64_t prime5 = 2870177450012600261ULL;
+
+ uint64_t state[4];
+ uint64_t result;
+
+ uint8_t* stop = data + len;
+ uint8_t* stop_block = stop - max_buffer_size64;
+
+ state[0] = seed + prime1 + prime2;
+ state[1] = seed + prime2;
+ state[2] = seed;
+ state[3] = seed - prime1;
+
+ for(;data <= stop_block;){
+ state[0] = pr64(state[0], data[0]);
+ state[1] = pr64(state[1], data[1]);
+ state[2] = pr64(state[2], data[2]);
+ state[3] = pr64(state[3], data[3]);
+
+ data += 32;
+ }
+
+ if(len >= max_buffer_size64){
+ result = rol64(state[0], 1) + rol64(state[1], 7) +
+ rol64(state[2], 12) + rol64(state[3], 18);
+ result = (result ^ pr64(0, state[0])) * prime1 + prime4;
+ result = (result ^ pr64(0, state[1])) * prime1 + prime4;
+ result = (result ^ pr64(0, state[2])) * prime1 + prime4;
+ result = (result ^ pr64(0, state[3])) * prime1 + prime4;
+ } else {
+ result = state[2] + prime5;
+ }
+ result += len;
+
+ for (; data + 8 <= stop; data += 8)
+ result = rol64(result ^ pr64(0, *(uint64_t*)data), 27) * prime1 + prime4;
+
+ if (data + 4 <= stop){
+ result = rol64(result ^ (*(uint32_t*)data) * prime1, 23) * prime2 + prime3;
+ data += 4;
+ }
+
+ for(;data != stop;)
+ result = rol64(result ^ (*data++) * prime5, 11) * prime1;
+
+ result ^= result >> 33;
+ result *= prime2;
+ result ^= result >> 29;
+ result *= prime3;
+ result ^= result >> 32;
+ return result;
+}
+
+int l_xxh64(lua_State* L){
+
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+
+ uint64_t seed = 0;
+ if(argv>1) seed = luaL_checkinteger(L, 2);
+
+ char digest[64];
+
+ uint64_t u = i_xxhash64(a, seed, len);
+ sprintf(digest,"%016llx",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
+
+int l_xxh32(lua_State* L){
+
+ size_t len = 0;
+ uint8_t* a = (uint8_t*)luaL_checklstring(L, 1, &len);
+ int argv = lua_gettop(L);
+
+ uint32_t seed = 0;
+ if(argv>1) seed = luaL_checkinteger(L, 2);
+
+ char digest[32];
+
+ uint32_t u = i_xxhash32(a, seed, len);
+ sprintf(digest,"%x",u);
+ lua_pushstring(L, digest);
+
+ return 1;
+}
@@ -1,69 +1,69 @@ -#include "util.h" -#include <stdio.h> -#include <stdlib.h> -#include <pthread.h> -#include "lua.h" -#include "net.h" - -int gen_parse(char* inp, int len, parray_t** _table){ - str* current = str_init(""), *last = NULL; - int state = 0; - - parray_t* table = *_table; - for(int i = 0; i < len; i++){ - - if(state != 1 && inp[i] == ';'){ - parray_set(table, last->c, (void*)current); - str_free(last); - last = NULL; - current = str_init(""); - state = 0; - } else if(state != 1 && inp[i] == '='){ - last = current; - current = str_init(""); - if(inp[i+1] == '"'){ - state = 1; - i++; - } - } else if(state == 1 && inp[i] == '"'){ - state = 0; - } else if(current->c[0] != '\0' || inp[i] != ' ') str_pushl(current, inp + i, 1); - } - - if(last != NULL){ - parray_set(table, last->c, (void*)current); - str_free(last); - } - *_table = table; - return 1; -} - -char* strnstr(const char *s1, const char *s2, size_t n) { - // simplistic algorithm with O(n2) worst case, stolen from stack overflow - size_t i, len; - char c = *s2; - - if(c == '\0') - return (char *)s1; - - for(len = strlen(s2); len <= n; n--, s1++){ - if(*s1 == c){ - for(i = 1;; i++){ - if(i == len) return (char *)s1; - if(s1[i] != s2[i]) break; - } - } - } - return NULL; -} - -void _p_fatal(const char* m, int line, const char* file, const char* function){ - fprintf(stderr, "%s[fatal] %s \n" - "\tthread: %zu/%zu\n" - "\tat: %s:%s(%i) %s\n",color_red, m, pthread_self(), threads, file, function, line, color_reset); - exit(EXIT_FAILURE); -} - -void p_error(const char* m){ - fprintf(stderr, "%s[error]%s %s\n",color_red, color_reset, m); -} +#include "util.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <pthread.h>
+#include "lua.h"
+#include "net.h"
+
+int gen_parse(char* inp, int len, parray_t** _table){
+ str* current = str_init(""), *last = NULL;
+ int state = 0;
+
+ parray_t* table = *_table;
+ for(int i = 0; i < len; i++){
+
+ if(state != 1 && inp[i] == ';'){
+ parray_set(table, last->c, (void*)current);
+ str_free(last);
+ last = NULL;
+ current = str_init("");
+ state = 0;
+ } else if(state != 1 && inp[i] == '='){
+ last = current;
+ current = str_init("");
+ if(inp[i+1] == '"'){
+ state = 1;
+ i++;
+ }
+ } else if(state == 1 && inp[i] == '"'){
+ state = 0;
+ } else if(current->c[0] != '\0' || inp[i] != ' ') str_pushl(current, inp + i, 1);
+ }
+
+ if(last != NULL){
+ parray_set(table, last->c, (void*)current);
+ str_free(last);
+ }
+ *_table = table;
+ return 1;
+}
+
+char* strnstr(const char *s1, const char *s2, size_t n) {
+ // simplistic algorithm with O(n2) worst case, stolen from stack overflow
+ size_t i, len;
+ char c = *s2;
+
+ if(c == '\0')
+ return (char *)s1;
+
+ for(len = strlen(s2); len <= n; n--, s1++){
+ if(*s1 == c){
+ for(i = 1;; i++){
+ if(i == len) return (char *)s1;
+ if(s1[i] != s2[i]) break;
+ }
+ }
+ }
+ return NULL;
+}
+
+void _p_fatal(const char* m, int line, const char* file, const char* function){
+ fprintf(stderr, "%s[fatal] %s \n"
+ "\tthread: %zu/%zu\n"
+ "\tat: %s:%s(%i) %s\n",color_red, m, pthread_self(), threads, file, function, line, color_reset);
+ exit(EXIT_FAILURE);
+}
+
+void p_error(const char* m){
+ fprintf(stderr, "%s[error]%s %s\n",color_red, color_reset, m);
+}
|
