miketout
6 years ago
9 changed files with 206 additions and 1747 deletions
@ -1,26 +1,3 @@ |
|||
node-crypto |
|||
=========== |
|||
|
|||
A wrapper around openssl for node.js, currently exposing Hashing, Signing |
|||
and Verifying methods. |
|||
|
|||
Version 0.0.5 : test.js now uses mjsunit |
|||
|
|||
Version 0.0.4 : Added package.json and updated tests to latest node version. |
|||
|
|||
Version 0.0.3 : Added cipher / decipher, thanks to Frans-Willem Hardijzer. |
|||
|
|||
To install, ensure that you have openssl installed, and run: |
|||
|
|||
node-waf configure |
|||
node-waf build |
|||
|
|||
This will put the crypto.node binary module in build/default. |
|||
|
|||
The hashing, signing and verifying methods can work with binary, hex or |
|||
base64 encoded strings. |
|||
|
|||
The encrypt / decrypt methods work with binary, hex or base64 encodings, |
|||
with streaming. |
|||
|
|||
See test.js for example usage. |
|||
verushash-node |
|||
----------------------- |
|||
Implementation of the VerushHash hash algorithm as a node.js module |
|||
|
File diff suppressed because it is too large
@ -1,11 +1,11 @@ |
|||
{ "name" : "crypto" |
|||
, "author" : "Rhys <rhys@wave.to>" |
|||
{ "name" : "verushash" |
|||
, "author" : "miketout" |
|||
, "version" : "0.0.5" |
|||
, "scripts" : |
|||
{ "preinstall" : "node-waf configure" |
|||
, "install" : "node-waf build" |
|||
, "test" : "node test.js" |
|||
} |
|||
, "main" : "build/default/crypto" |
|||
, "main" : "build/default/verushash" |
|||
, "engines" : [ "node" ] |
|||
} |
|||
|
@ -1,20 +0,0 @@ |
|||
-----BEGIN CERTIFICATE----- |
|||
MIIDXDCCAsWgAwIBAgIJAKL0UG+mRkSPMA0GCSqGSIb3DQEBBQUAMH0xCzAJBgNV |
|||
BAYTAlVLMRQwEgYDVQQIEwtBY2tuYWNrIEx0ZDETMBEGA1UEBxMKUmh5cyBKb25l |
|||
czEQMA4GA1UEChMHbm9kZS5qczEdMBsGA1UECxMUVGVzdCBUTFMgQ2VydGlmaWNh |
|||
dGUxEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0wOTExMTEwO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IICXQIBAAKBgQDx3wdzpq2rvwm3Ucun1qAD/ClB+wW+RhR1nVix286QvaNqePAd |
|||
CAwwLL82NqXcVQRbQ4s95splQnwvjgkFdKVXFTjPKKJI5aV3wSRN61EBVPdYpCre |
|||
535yfG/uDysZFCnVQdnCZ1tnXAR8BirxCNjHqbVyIyBGjsNoNCEPb2R35QIDAQAB |
|||
AoGBAJNem9C4ftrFNGtQ2DB0Udz7uDuucepkErUy4MbFsc947GfENjDKJXr42Kx0 |
|||
kYx09ImS1vUpeKpH3xiuhwqe7tm4FsCBg4TYqQle14oxxm7TNeBwwGC3OB7hiokb |
|||
aAjbPZ1hAuNs6ms3Ybvvj6Lmxzx42m8O5DXCG2/f+KMvaNUhAkEA/ekrOsWkNoW9 |
|||
2n3m+msdVuxeek4B87EoTOtzCXb1dybIZUVv4J48VAiM43hhZHWZck2boD/hhwjC |
|||
M5NWd4oY6QJBAPPcgBVNdNZSZ8hR4ogI4nzwWrQhl9MRbqqtfOn2TK/tjMv10ALg |
|||
lPmn3SaPSNRPKD2hoLbFuHFERlcS79pbCZ0CQQChX3PuIna/gDitiJ8oQLOg7xEM |
|||
wk9TRiDK4kl2lnhjhe6PDpaQN4E4F0cTuwqLAoLHtrNWIcOAQvzKMrYdu1MhAkBm |
|||
Et3qDMnjDAs05lGT72QeN90/mPAcASf5eTTYGahv21cb6IBxM+AnwAPpqAAsHhYR |
|||
9h13Y7uYbaOjvuF23LRhAkBoI9eaSMn+l81WXOVUHnzh3ZwB4GuTyxMXXNOhuiFd |
|||
0z4LKAMh99Z4xQmqSoEkXsfM4KPpfhYjF/bwIcP5gOei |
|||
-----END RSA PRIVATE KEY----- |
@ -0,0 +1,178 @@ |
|||
#include <node.h> |
|||
#include <node_events.h> |
|||
#include <assert.h> |
|||
#include <string.h> |
|||
|
|||
using namespace v8; |
|||
using namespace node; |
|||
|
|||
void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest, int* md_hex_len) { |
|||
*md_hex_len = (2*(md_len)); |
|||
*md_hexdigest = (char *) malloc(*md_hex_len + 1); |
|||
for(int i = 0; i < md_len; i++) { |
|||
sprintf((char *)(*md_hexdigest + (i*2)), "%02x", md_value[i]); |
|||
} |
|||
} |
|||
|
|||
#define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) : ((c) - 'a' + 10)) |
|||
void hex_decode(unsigned char *input, int length, char** buf64, int* buf64_len) { |
|||
*buf64_len = (length/2); |
|||
*buf64 = (char*) malloc(length/2 + 1); |
|||
char *b = *buf64; |
|||
for(int i = 0; i < length-1; i+=2) { |
|||
b[i/2] = (hex2i(input[i])<<4) | (hex2i(input[i+1])); |
|||
} |
|||
} |
|||
|
|||
class Hash : public ObjectWrap { |
|||
public: |
|||
static void |
|||
Initialize (v8::Handle<v8::Object> target) |
|||
{ |
|||
HandleScope scope; |
|||
|
|||
Local<FunctionTemplate> t = FunctionTemplate::New(New); |
|||
|
|||
t->InstanceTemplate()->SetInternalFieldCount(1); |
|||
|
|||
NODE_SET_PROTOTYPE_METHOD(t, "init", HashInit); |
|||
NODE_SET_PROTOTYPE_METHOD(t, "update", HashUpdate); |
|||
NODE_SET_PROTOTYPE_METHOD(t, "digest", HashDigest); |
|||
|
|||
target->Set(String::NewSymbol("Hash"), t->GetFunction()); |
|||
} |
|||
|
|||
bool HashInit (const char* hashType) |
|||
{ |
|||
initialised = true; |
|||
return true; |
|||
|
|||
} |
|||
|
|||
int HashUpdate(char* data, int len) { |
|||
if (!initialised) |
|||
return 0; |
|||
// put Hash here
|
|||
return 1; |
|||
} |
|||
|
|||
int HashDigest(unsigned char** md_value, unsigned int *md_len) { |
|||
if (!initialised) |
|||
return 0; |
|||
// finalize and return value here
|
|||
return 1; |
|||
} |
|||
|
|||
|
|||
protected: |
|||
|
|||
static Handle<Value> |
|||
New (const Arguments& args) |
|||
{ |
|||
HandleScope scope; |
|||
|
|||
Hash *hash = new Hash(); |
|||
hash->Wrap(args.This()); |
|||
return args.This(); |
|||
} |
|||
|
|||
static Handle<Value> |
|||
HashInit(const Arguments& args) { |
|||
Hash *hash = ObjectWrap::Unwrap<Hash>(args.This()); |
|||
|
|||
HandleScope scope; |
|||
|
|||
if (args.Length() == 0 || !args[0]->IsString()) { |
|||
return ThrowException(String::New("Must give hashtype string as argument")); |
|||
} |
|||
|
|||
String::Utf8Value hashType(args[0]->ToString()); |
|||
|
|||
bool r = hash->HashInit(*hashType); |
|||
|
|||
return args.This(); |
|||
} |
|||
|
|||
static Handle<Value> |
|||
HashUpdate(const Arguments& args) { |
|||
Hash *hash = ObjectWrap::Unwrap<Hash>(args.This()); |
|||
|
|||
HandleScope scope; |
|||
|
|||
enum encoding enc = ParseEncoding(args[1]); |
|||
ssize_t len = DecodeBytes(args[0], enc); |
|||
|
|||
if (len < 0) { |
|||
Local<Value> exception = Exception::TypeError(String::New("Bad argument")); |
|||
return ThrowException(exception); |
|||
} |
|||
|
|||
char* buf = new char[len]; |
|||
ssize_t written = DecodeWrite(buf, len, args[0], enc); |
|||
assert(written == len); |
|||
|
|||
int r = hash->HashUpdate(buf, len); |
|||
|
|||
return args.This(); |
|||
} |
|||
|
|||
static Handle<Value> |
|||
HashDigest(const Arguments& args) { |
|||
Hash *hash = ObjectWrap::Unwrap<Hash>(args.This()); |
|||
|
|||
HandleScope scope; |
|||
|
|||
unsigned char* md_value; |
|||
unsigned int md_len; |
|||
char* md_hexdigest; |
|||
int md_hex_len; |
|||
Local<Value> outString ; |
|||
|
|||
int r = hash->HashDigest(&md_value, &md_len); |
|||
|
|||
if (md_len == 0 || r == 0) { |
|||
return scope.Close(String::New("")); |
|||
} |
|||
|
|||
if (args.Length() == 0 || !args[0]->IsString()) { |
|||
// Binary
|
|||
outString = Encode(md_value, md_len, BINARY); |
|||
} else { |
|||
String::Utf8Value encoding(args[0]->ToString()); |
|||
if (strcasecmp(*encoding, "hex") == 0) { |
|||
// Hex encoding
|
|||
hex_encode(md_value, md_len, &md_hexdigest, &md_hex_len); |
|||
outString = Encode(md_hexdigest, md_hex_len, BINARY); |
|||
free(md_hexdigest); |
|||
} else if (strcasecmp(*encoding, "binary") == 0) { |
|||
outString = Encode(md_value, md_len, BINARY); |
|||
} else { |
|||
fprintf(stderr, "verushash-node : Hash .digest encoding " |
|||
"can be binary or hex\n"); |
|||
} |
|||
} |
|||
free(md_value); |
|||
return scope.Close(outString); |
|||
|
|||
} |
|||
|
|||
Hash () : ObjectWrap (), vh() |
|||
{ |
|||
initialised = false; |
|||
} |
|||
|
|||
~Hash () |
|||
{ |
|||
} |
|||
|
|||
private: |
|||
CVerusHash vh; |
|||
bool initialised; |
|||
}; |
|||
|
|||
extern "C" void |
|||
init (Handle<Object> target) |
|||
{ |
|||
HandleScope scope; |
|||
Hash::Initialize(target); |
|||
} |
Loading…
Reference in new issue