Merge pull request #19 from ohac/master

Added sha1coin algo
master
Matthew Little 2014-06-19 12:00:00 -06:00
commit cb6fd1f08b
5 changed files with 102 additions and 1 deletions

View File

@ -21,6 +21,7 @@
"x13.c",
"boolberry.cc",
"nist5.c",
"sha1.c",
"sha3/sph_hefty1.c",
"sha3/sph_fugue.c",
"sha3/aes_helper.c",

View File

@ -20,6 +20,7 @@ extern "C" {
#include "cryptonight.h"
#include "x13.h"
#include "nist5.h"
#include "sha1.h"
}
#include "boolberry.h"
@ -505,6 +506,28 @@ Handle<Value> nist5(const Arguments& args) {
return scope.Close(buff->handle_);
}
Handle<Value> sha1(const Arguments& args) {
HandleScope scope;
if (args.Length() < 1)
return except("You must provide one argument.");
Local<Object> target = args[0]->ToObject();
if(!Buffer::HasInstance(target))
return except("Argument should be a buffer object.");
char * input = Buffer::Data(target);
char output[32];
uint32_t input_len = Buffer::Length(target);
sha1_hash(input, output, input_len);
Buffer* buff = Buffer::New(output, 32);
return scope.Close(buff->handle_);
}
void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("quark"), FunctionTemplate::New(quark)->GetFunction());
exports->Set(String::NewSymbol("x11"), FunctionTemplate::New(x11)->GetFunction());
@ -525,6 +548,7 @@ void init(Handle<Object> exports) {
exports->Set(String::NewSymbol("x13"), FunctionTemplate::New(x13)->GetFunction());
exports->Set(String::NewSymbol("boolberry"), FunctionTemplate::New(boolberry)->GetFunction());
exports->Set(String::NewSymbol("nist5"), FunctionTemplate::New(nist5)->GetFunction());
exports->Set(String::NewSymbol("sha1"), FunctionTemplate::New(sha1)->GetFunction());
}
NODE_MODULE(multihashing, init)

View File

@ -25,6 +25,7 @@
"keccak",
"blake",
"shavite",
"fugue"
"fugue",
"sha1"
]
}

59
sha1.c 100644
View File

@ -0,0 +1,59 @@
#include "sha1.h"
#include <string.h>
#include <openssl/sha.h>
inline void encodeb64(const unsigned char* pch, char* buff)
{
const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int mode = 0, left = 0;
const int len = 20;
const unsigned char *pchEnd = pch + len;
while (pch < pchEnd) {
int enc = *(pch++);
if (mode == 0) {
*buff++ = pbase64[enc >> 2];
left = (enc & 3) << 4;
mode = 1;
}
else if (mode == 1) {
*buff++ = pbase64[left | (enc >> 4)];
left = (enc & 15) << 2;
mode = 2;
}
else {
*buff++ = pbase64[left | (enc >> 6)];
*buff++ = pbase64[enc & 63];
mode = 0;
}
}
*buff = pbase64[left];
*(buff + 1) = 0;
}
void sha1_hash(const char* input, char* output, uint32_t len)
{
char str[38] __attribute__((aligned(32))); // 26 + 11 + 1
uint32_t prehash[5] __attribute__((aligned(32)));
uint32_t hash[5] __attribute__((aligned(32))) = { 0 };
int i = 0;
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, (void *)input, len);
SHA1_Final((void *)prehash, &ctx);
encodeb64((const unsigned char *)prehash, str);
memcpy(&str[26], str, 11);
str[37] = 0;
for (i = 0; i < 26; i++) {
SHA1_Init(&ctx);
SHA1_Update(&ctx, (void *)&str[i], 12);
SHA1_Final((void *)prehash, &ctx);
hash[0] ^= prehash[0];
hash[1] ^= prehash[1];
hash[2] ^= prehash[2];
hash[3] ^= prehash[3];
hash[4] ^= prehash[4];
}
memset(output, 0, 32 - 20);
memcpy(&output[32 - 20], hash, 20);
}

16
sha1.h 100644
View File

@ -0,0 +1,16 @@
#ifndef SHA1_H
#define SHA1_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
void sha1_hash(const char* input, char* output, uint32_t len);
#ifdef __cplusplus
}
#endif
#endif