Skip to content

Commit cb6fd1f

Browse files
committed
Merge pull request zone117x#19 from ohac/master
Added sha1coin algo
2 parents cacdbe2 + 5430b87 commit cb6fd1f

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

binding.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"x13.c",
2222
"boolberry.cc",
2323
"nist5.c",
24+
"sha1.c",
2425
"sha3/sph_hefty1.c",
2526
"sha3/sph_fugue.c",
2627
"sha3/aes_helper.c",

multihashing.cc

+24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extern "C" {
2020
#include "cryptonight.h"
2121
#include "x13.h"
2222
#include "nist5.h"
23+
#include "sha1.h"
2324
}
2425

2526
#include "boolberry.h"
@@ -505,6 +506,28 @@ Handle<Value> nist5(const Arguments& args) {
505506
return scope.Close(buff->handle_);
506507
}
507508

509+
Handle<Value> sha1(const Arguments& args) {
510+
HandleScope scope;
511+
512+
if (args.Length() < 1)
513+
return except("You must provide one argument.");
514+
515+
Local<Object> target = args[0]->ToObject();
516+
517+
if(!Buffer::HasInstance(target))
518+
return except("Argument should be a buffer object.");
519+
520+
char * input = Buffer::Data(target);
521+
char output[32];
522+
523+
uint32_t input_len = Buffer::Length(target);
524+
525+
sha1_hash(input, output, input_len);
526+
527+
Buffer* buff = Buffer::New(output, 32);
528+
return scope.Close(buff->handle_);
529+
}
530+
508531
void init(Handle<Object> exports) {
509532
exports->Set(String::NewSymbol("quark"), FunctionTemplate::New(quark)->GetFunction());
510533
exports->Set(String::NewSymbol("x11"), FunctionTemplate::New(x11)->GetFunction());
@@ -525,6 +548,7 @@ void init(Handle<Object> exports) {
525548
exports->Set(String::NewSymbol("x13"), FunctionTemplate::New(x13)->GetFunction());
526549
exports->Set(String::NewSymbol("boolberry"), FunctionTemplate::New(boolberry)->GetFunction());
527550
exports->Set(String::NewSymbol("nist5"), FunctionTemplate::New(nist5)->GetFunction());
551+
exports->Set(String::NewSymbol("sha1"), FunctionTemplate::New(sha1)->GetFunction());
528552
}
529553

530554
NODE_MODULE(multihashing, init)

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"keccak",
2626
"blake",
2727
"shavite",
28-
"fugue"
28+
"fugue",
29+
"sha1"
2930
]
3031
}

sha1.c

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include "sha1.h"
2+
3+
#include <string.h>
4+
#include <openssl/sha.h>
5+
6+
inline void encodeb64(const unsigned char* pch, char* buff)
7+
{
8+
const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9+
int mode = 0, left = 0;
10+
const int len = 20;
11+
const unsigned char *pchEnd = pch + len;
12+
while (pch < pchEnd) {
13+
int enc = *(pch++);
14+
if (mode == 0) {
15+
*buff++ = pbase64[enc >> 2];
16+
left = (enc & 3) << 4;
17+
mode = 1;
18+
}
19+
else if (mode == 1) {
20+
*buff++ = pbase64[left | (enc >> 4)];
21+
left = (enc & 15) << 2;
22+
mode = 2;
23+
}
24+
else {
25+
*buff++ = pbase64[left | (enc >> 6)];
26+
*buff++ = pbase64[enc & 63];
27+
mode = 0;
28+
}
29+
}
30+
*buff = pbase64[left];
31+
*(buff + 1) = 0;
32+
}
33+
34+
void sha1_hash(const char* input, char* output, uint32_t len)
35+
{
36+
char str[38] __attribute__((aligned(32))); // 26 + 11 + 1
37+
uint32_t prehash[5] __attribute__((aligned(32)));
38+
uint32_t hash[5] __attribute__((aligned(32))) = { 0 };
39+
int i = 0;
40+
SHA_CTX ctx;
41+
SHA1_Init(&ctx);
42+
SHA1_Update(&ctx, (void *)input, len);
43+
SHA1_Final((void *)prehash, &ctx);
44+
encodeb64((const unsigned char *)prehash, str);
45+
memcpy(&str[26], str, 11);
46+
str[37] = 0;
47+
for (i = 0; i < 26; i++) {
48+
SHA1_Init(&ctx);
49+
SHA1_Update(&ctx, (void *)&str[i], 12);
50+
SHA1_Final((void *)prehash, &ctx);
51+
hash[0] ^= prehash[0];
52+
hash[1] ^= prehash[1];
53+
hash[2] ^= prehash[2];
54+
hash[3] ^= prehash[3];
55+
hash[4] ^= prehash[4];
56+
}
57+
memset(output, 0, 32 - 20);
58+
memcpy(&output[32 - 20], hash, 20);
59+
}

sha1.h

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

0 commit comments

Comments
 (0)