webmcp

view libraries/moonhash/moonhash.c @ 502:fa902b26589f

Removed endianess detection from moonhash_sha.c and created byteorder.h for that purpose instead
author jbe
date Mon Aug 14 12:07:37 2017 +0200 (2017-08-14)
parents b36e366bba2b
children 83b3882dc31b
line source
1 #include <lua.h>
2 #include <lauxlib.h>
3 #include "moonhash_sha3.c"
5 void moonhash_push_hex(lua_State *L, unsigned char *buf, int len) {
6 int i;
7 unsigned char n;
8 char str[2*len+1];
9 for (i=0; i<len; i++) {
10 n = buf[i] >> 4;
11 str[2*i+0] = n + ((n < 10) ? '0' : ('a' - 10));
12 n = buf[i] & 0x0f;
13 str[2*i+1] = n + ((n < 10) ? '0' : ('a' - 10));
14 }
15 str[2*len] = 0;
16 lua_pushstring(L, str);
17 }
19 typedef void (*moonhash_sha3_fptr)(const uint8_t *, uint64_t, uint8_t *);
20 typedef void (*moonhash_shake_fptr)(const uint8_t *, uint64_t, uint8_t *, uint64_t);
22 int moonhash_sha3(lua_State *L, moonhash_sha3_fptr hashfunc, int len) {
23 const char *input;
24 size_t inputlen;
25 unsigned char output[len];
26 input = luaL_checklstring(L, 1, &inputlen);
27 hashfunc((const uint8_t *)input, inputlen, output);
28 moonhash_push_hex(L, output, len);
29 return 1;
30 }
32 int moonhash_sha3_224(lua_State *L) {
33 return moonhash_sha3(L, FIPS202_SHA3_224, 224/8);
34 }
35 int moonhash_sha3_256(lua_State *L) {
36 return moonhash_sha3(L, FIPS202_SHA3_256, 256/8);
37 }
38 int moonhash_sha3_384(lua_State *L) {
39 return moonhash_sha3(L, FIPS202_SHA3_384, 384/8);
40 }
41 int moonhash_sha3_512(lua_State *L) {
42 return moonhash_sha3(L, FIPS202_SHA3_512, 512/8);
43 }
45 int moonhash_shake(lua_State *L, moonhash_shake_fptr shakefunc, int len) {
46 const char *input;
47 size_t inputlen;
48 unsigned char output[len];
49 input = luaL_checklstring(L, 1, &inputlen);
50 shakefunc((const uint8_t *)input, inputlen, output, len);
51 moonhash_push_hex(L, output, len);
52 return 1;
53 }
55 int moonhash_shake128_128(lua_State *L) {
56 return moonhash_shake(L, FIPS202_SHAKE128, 128/8);
57 }
59 static const struct luaL_Reg moonhash_module_functions[] = {
60 {"sha3_224", moonhash_sha3_224},
61 {"sha3_256", moonhash_sha3_256},
62 {"sha3_384", moonhash_sha3_384},
63 {"sha3_512", moonhash_sha3_512},
64 {"shake128_128", moonhash_shake128_128},
65 {NULL, NULL}
66 };
68 int luaopen_moonhash(lua_State *L) {
69 lua_newtable(L);
70 luaL_setfuncs(L, moonhash_module_functions, 0);
71 return 1;
72 }

Impressum / About Us