Skip to content

Commit 5d73e6f

Browse files
committed
Initial hefty1 support
1 parent dbc35af commit 5d73e6f

File tree

7 files changed

+555
-1
lines changed

7 files changed

+555
-1
lines changed

Diff for: binding.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"blake.c",
1717
"fugue.c",
1818
"qubit.c",
19+
"hefty1.c",
20+
"sha3/sph_hefty1.c",
1921
"sha3/sph_fugue.c",
2022
"sha3/aes_helper.c",
2123
"sha3/sph_blake.c",

Diff for: hefty1.c

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "hefty1.h"
2+
3+
#include "sha3/sph_hefty1.h"
4+
#include "sha3/sph_keccak.h"
5+
#include "sha3/sph_groestl.h"
6+
#include "sha3/sph_blake.h"
7+
#include "sha256.h"
8+
9+
void hefty1_hash(const char* input, char* output, uint32_t len)
10+
{
11+
HEFTY1_CTX ctx_hefty1;
12+
SHA256_CTX ctx_sha256;
13+
sph_keccak512_context ctx_keccak;
14+
sph_groestl512_context ctx_groestl;
15+
sph_blake512_context ctx_blake;
16+
17+
char* hash32_1 = (char*) malloc(32);
18+
char* hash32_2 = (char*) malloc(32);
19+
char* hash64_3 = (char*) malloc(64);
20+
char* hash64_4 = (char*) malloc(64);
21+
char* hash64_5 = (char*) malloc(64);
22+
23+
HEFTY1_Init(&ctx_hefty1);
24+
HEFTY1_Update(&ctx_hefty1, (const void*) input, len);
25+
HEFTY1_Final((unsigned char*) hash32_1, &ctx_hefty1); // 1
26+
27+
SHA256_Init(&ctx_sha256);
28+
SHA256_Update(&ctx_sha256, (const void*) input, len);
29+
SHA256_Update(&ctx_sha256, (unsigned char*) hash32_1, 32); // 1
30+
SHA256_Final((unsigned char*) hash32_2, &ctx_sha256); // 2
31+
32+
sph_keccak512_init(&ctx_keccak);
33+
sph_keccak512(&ctx_keccak, (const void*) input, len);
34+
sph_keccak512(&ctx_keccak, (unsigned char*) hash32_1, 32); //1
35+
sph_keccak512_close(&ctx_keccak, (void*) hash64_3); // 3
36+
37+
sph_groestl512_init(&ctx_groestl);
38+
sph_groestl512(&ctx_groestl, (const void*) input, len);
39+
sph_groestl512(&ctx_groestl, (unsigned char*) hash32_1, 32); // 1
40+
sph_groestl512_close(&ctx_groestl, (void*) hash64_4); // 4
41+
42+
sph_blake512_init(&ctx_blake);
43+
sph_blake512(&ctx_blake, (const void*) input, len);
44+
sph_blake512(&ctx_blake, (unsigned char*) hash32_1, 32); // 1
45+
sph_blake512_close(&ctx_blake, (void*) hash64_5); // 5
46+
47+
memset(output, 0, 32);
48+
49+
char* hash[4] = { hash32_2, hash64_3, hash64_4, hash64_5 };
50+
51+
uint32_t i;
52+
uint32_t j;
53+
54+
for(i = 0; i < 64; i++) {
55+
for(j = 0; j < 4; j++) {
56+
if((*(hash[j] + i) & 0x80) != 0)
57+
output[((i * 4 + j) - (i * 4 + j) % 8) / 8] |= 1 << (i * 4 + j) % 8;
58+
}
59+
}
60+
61+
free(hash32_1);
62+
free(hash32_2);
63+
free(hash64_3);
64+
free(hash64_4);
65+
free(hash64_5);
66+
}
67+

Diff for: hefty1.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef HEFTY1_H
2+
#define HEFTY1_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stdint.h>
9+
10+
void hefty1_hash(const char* input, char* output, uint32_t len);
11+
12+
#ifdef __cplusplus
13+
}
14+
#endif
15+
16+
#endif

Diff for: multihashing.cc

+25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extern "C" {
1616
#include "blake.h"
1717
#include "fugue.h"
1818
#include "qubit.h"
19+
#include "hefty1.h"
1920
}
2021

2122
using namespace node;
@@ -336,6 +337,29 @@ Handle<Value> qubit(const Arguments& args) {
336337
return scope.Close(buff->handle_);
337338
}
338339

340+
341+
Handle<Value> hefty1(const Arguments& args) {
342+
HandleScope scope;
343+
344+
if (args.Length() < 1)
345+
return except("You must provide one argument.");
346+
347+
Local<Object> target = args[0]->ToObject();
348+
349+
if(!Buffer::HasInstance(target))
350+
return except("Argument should be a buffer object.");
351+
352+
char * input = Buffer::Data(target);
353+
char * output = new char[32];
354+
355+
uint32_t input_len = Buffer::Length(target);
356+
357+
hefty1_hash(input, output, input_len);
358+
359+
Buffer* buff = Buffer::New(output, 32);
360+
return scope.Close(buff->handle_);
361+
}
362+
339363
void init(Handle<Object> exports) {
340364
exports->Set(String::NewSymbol("quark"), FunctionTemplate::New(quark)->GetFunction());
341365
exports->Set(String::NewSymbol("x11"), FunctionTemplate::New(x11)->GetFunction());
@@ -350,6 +374,7 @@ void init(Handle<Object> exports) {
350374
exports->Set(String::NewSymbol("blake"), FunctionTemplate::New(blake)->GetFunction());
351375
exports->Set(String::NewSymbol("fugue"), FunctionTemplate::New(fugue)->GetFunction());
352376
exports->Set(String::NewSymbol("qubit"), FunctionTemplate::New(qubit)->GetFunction());
377+
exports->Set(String::NewSymbol("hefty1"), FunctionTemplate::New(hefty1)->GetFunction());
353378
}
354379

355380
NODE_MODULE(multihashing, init)

Diff for: qubit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void qubit_hash(const char* input, char* output, uint32_t len)
1919

2020
sph_luffa512_init(&ctx_luffa);
2121
sph_luffa512(&ctx_luffa, (const void*) input, len);
22-
sph_luffa512_close(&ctx_luffa, (void*) hash1); //1
22+
sph_luffa512_close(&ctx_luffa, (void*) hash1); // 1
2323

2424
sph_cubehash512_init(&ctx_cubehash);
2525
sph_cubehash512(&ctx_cubehash, (const void*) hash1, 64); // 1

0 commit comments

Comments
 (0)