aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crypto.h3
-rw-r--r--src/hash/adler.c52
-rw-r--r--src/hash/adler.h20
3 files changed, 58 insertions, 17 deletions
diff --git a/src/crypto.h b/src/crypto.h
index 400e503..2d3dcca 100644
--- a/src/crypto.h
+++ b/src/crypto.h
@@ -41,7 +41,7 @@ uint64_t rotr64(uint64_t, uint64_t);
static const luaL_Reg crypto_function_list [] = {
{"sha0",l_sha0}, {"sha1",l_sha1}, {"sha256",l_sha256}, {"sha224",l_sha224},
{"setpearson",l_setpearson}, {"pearson",l_pearson}, {"xxh64",l_xxh64},
- {"xxh32",l_xxh32}, {"adler32",l_adler32}, {"bsdchecksum",l_bsdchecksum},
+ {"xxh32",l_xxh32}, {"bsdchecksum",l_bsdchecksum},
{"crc8",l_crc8}, {"crc16",l_crc16}, {"crc32",l_crc32}, {"fletcher8",l_fletcher8},
{"fletcher16",l_fletcher16}, {"fletcher32",l_fletcher32},
{"sysvchecksum",l_sysvchecksum}, {"xor8",l_xor8}, {"setbuzhash",l_setbuzhash},
@@ -60,6 +60,7 @@ static const luaL_Reg crypto_function_list [] = {
{"blake2b", l_blake2b}, {"blake2s", l_blake2s}, {"blake256", l_blake256},
{"blake224", l_blake224}, {"blake512", l_blake512}, {"blake384", l_blake384},
+ {"adler32",l_adler32}, {"adler32_init",l_adler32_init}, {"adler32_update",l_adler32_update}, {"adler32_final",l_adler32_final},
{"uuencode",l_uuencode},
{"uudecode",l_uudecode},
diff --git a/src/hash/adler.c b/src/hash/adler.c
index 3bd865a..ed7a84e 100644
--- a/src/hash/adler.c
+++ b/src/hash/adler.c
@@ -2,15 +2,50 @@
#include <stdio.h>
#include <stdint.h>
-uint32_t i_adler32(uint8_t *aa, size_t len){
- uint16_t a = 1, b = 0;
+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);
+}
+
+int l_adler32_init(lua_State* L){
+ struct adler32_hash* a = (struct adler32_hash*)lua_newuserdata(L, sizeof * a);
+ *a = adler32_init();
+ return 1;
+}
- for(int i = 0; i != len; i++){
- a += aa[i];
- b += a;
- }
+int l_adler32_update(lua_State* L){
+ struct adler32_hash* a = (struct adler32_hash*)lua_touserdata(L, 1);
+ size_t len = 0;
+ uint8_t* b = (uint8_t*)luaL_checklstring(L, 2, &len);
+
+ adler32_update(b, len, a);
+
+ return 1;
+}
- return b * 65536 + a;
+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){
@@ -19,7 +54,8 @@ int l_adler32(lua_State* L){
char digest[32];
- uint32_t u = i_adler32(a, len);
+ uint32_t u = adler32(a, len);
+
sprintf(digest,"%08x",u);
lua_pushstring(L, digest);
diff --git a/src/hash/adler.h b/src/hash/adler.h
index 3bd8925..29a74f0 100644
--- a/src/hash/adler.h
+++ b/src/hash/adler.h
@@ -1,13 +1,17 @@
#include "../lua.h"
#include <stdint.h>
-/*!
- * \brief calculates a adler hash of (len) bytes
- *
- * @param {uint8_t*} input bytes
- * @param {size_t} input length
- * @return {uint32_t} 32 bit hash
-*/
-uint32_t i_adler32(uint8_t*, size_t);
+struct adler32_hash {
+ uint16_t a;
+ uint16_t b;
+};
+
+struct adler32_hash adler32_init();
+void adler32_update(uint8_t*, size_t, struct adler32_hash*);
+uint32_t adler32_final(struct adler32_hash*);
+uint32_t adler32(uint8_t*, size_t);
int l_adler32(lua_State*);
+int l_adler32_init(lua_State*);
+int l_adler32_update(lua_State*);
+int l_adler32_final(lua_State*);