Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
A native implementation of Ed25519(http://ed25519.cr.yp.to/) for node.js

## Installation
npm install ed25519
npm install --registry=https://github.com/classfellow/ed25519.git ed25519

## Node engine
`node-v4`
`node-v6`
`node-v7`

## Usage
For usage details see the example.js file.
Expand Down
51 changes: 24 additions & 27 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -68,33 +68,30 @@
}]
]
}],
# https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL
['OS=="win"', {
'conditions': [
# "openssl_root" is the directory on Windows of the OpenSSL files.
# Check the "target_arch" variable to set good default values for
# both 64-bit and 32-bit builds of the module.
['target_arch=="x64"', {
'variables': {
'openssl_root%': 'C:/OpenSSL-Win64'
},
}, {
'variables': {
'openssl_root%': 'C:/OpenSSL-Win32'
},
}],
],
'libraries': [
'-l<(openssl_root)/lib/libeay32.lib',
],
'include_dirs': [
'<(openssl_root)/include',
],
}]
],
'include_dirs': [
"<!(node -e \"require('nan')\")"
# https://github.com/TooTallNate/node-gyp/wiki/Linking-to-OpenSSL
['OS=="win"', {
'conditions': [
# "openssl_root" is the directory on Windows of the OpenSSL files.
# Check the "target_arch" variable to set good default values for
# both 64-bit and 32-bit builds of the module.
['target_arch=="x64"', {
'variables': {
'openssl_root%': 'C:/OpenSSL-Win64'
},
}, {
'variables': {
'openssl_root%': 'C:/OpenSSL-Win32'
},
}],
],
'libraries': [
'-l<(openssl_root)/lib/libeay32.lib',
],
'include_dirs': [
'<(openssl_root)/include',
],
}]
]
}
]
}
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('bindings')('ed25519');
module.exports = require('./build/Release/ed25519.node');
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"name": "ed25519",
"version": "0.0.4",
"version": "0.0.5",
"description": "An Ed25519 implementation for node.js",
"dependencies": {
"bindings": "^1.2.1",
"nan": "^2.0.9"
},
"main": "index.js",
"scripts": {
Expand Down
49 changes: 23 additions & 26 deletions src/ed25519.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <node.h>
#include <node_buffer.h>

#include <nan.h>
#include <stdlib.h>

#include "ed25519/ed25519.h"

using namespace v8;
Expand All @@ -14,26 +11,26 @@ using namespace node;
* seed: A 32 byte buffer
* returns: an Object with PublicKey and PrivateKey
**/
NAN_METHOD(MakeKeypair) {
Nan::HandleScope scope;
static void MakeKeypair(const v8::FunctionCallbackInfo<v8::Value>& info) {
ISOLATE(info);
if ((info.Length() < 1) || (!Buffer::HasInstance(info[0])) || (Buffer::Length(info[0]->ToObject()) != 32)) {
return Nan::ThrowError("MakeKeypair requires a 32 byte buffer");
TYPEERROR(MakeKeypair requires a 32 byte buffer)
}
const unsigned char* seed = (unsigned char*)Buffer::Data(info[0]->ToObject());

v8::Local<v8::Object> privateKey = Nan::NewBuffer(64).ToLocalChecked();
v8::Local<v8::Object> privateKey = node::Buffer::New(isolate, 64).ToLocalChecked();

unsigned char* privateKeyData = (unsigned char*)Buffer::Data(privateKey);

v8::Local<v8::Object> publicKey = Nan::NewBuffer(32).ToLocalChecked();
v8::Local<v8::Object> publicKey = node::Buffer::New(isolate, 32).ToLocalChecked();
unsigned char* publicKeyData = (unsigned char*)Buffer::Data(publicKey);
for (int i = 0; i < 32; i++)
privateKeyData[i] = seed[i];
crypto_sign_keypair(publicKeyData, privateKeyData);

Local<Object> result = Nan::New<Object>();
result->Set(Nan::New("publicKey").ToLocalChecked(), publicKey);
result->Set(Nan::New("privateKey").ToLocalChecked(), privateKey);
v8::Local<v8::Object> result = v8::Object::New(isolate);
result->Set(v8::String::NewFromUtf8(isolate, "publicKey"), publicKey);
result->Set(v8::String::NewFromUtf8(isolate, "privateKey"), privateKey);
info.GetReturnValue().Set(result);
}

Expand All @@ -46,12 +43,12 @@ NAN_METHOD(MakeKeypair) {
* keyPair: the object from the MakeKeypair function
* returns: the signature as a Buffer
**/
NAN_METHOD(Sign) {
Nan::HandleScope scope;
static void Sign(const v8::FunctionCallbackInfo<v8::Value>& info) {
ISOLATE(info);
unsigned char* privateKey;

if ((info.Length() < 2) || (!Buffer::HasInstance(info[0]->ToObject()))) {
return Nan::ThrowError("Sign requires (Buffer, {Buffer(32 or 64) | keyPair object})");
TYPEERROR(Sign requires (Buffer, {Buffer(32 or 64) | keyPair object}))
}
if ((Buffer::HasInstance(info[1])) && (Buffer::Length(info[1]->ToObject()) == 32)) {
unsigned char* seed = (unsigned char*)Buffer::Data(info[1]->ToObject());
Expand All @@ -65,13 +62,13 @@ NAN_METHOD(Sign) {
} else if ((Buffer::HasInstance(info[1])) && (Buffer::Length(info[1]->ToObject()) == 64)) {
privateKey = (unsigned char*)Buffer::Data(info[1]->ToObject());
} else if ((info[1]->IsObject()) && (!Buffer::HasInstance(info[1]))) {
Local<Value> privateKeyBuffer = info[1]->ToObject()->Get(Nan::New<String>("privateKey").ToLocalChecked())->ToObject();
Local<Value> privateKeyBuffer = info[1]->ToObject()->Get(v8::String::NewFromUtf8(isolate, "privateKey"))->ToObject();
if (!Buffer::HasInstance(privateKeyBuffer)) {
return Nan::ThrowError("Sign requires (Buffer, {Buffer(32 or 64) | keyPair object})");
TYPEERROR(Sign requires (Buffer, {Buffer(32 or 64) | keyPair object}))
}
privateKey = (unsigned char*)Buffer::Data(privateKeyBuffer);
} else {
return Nan::ThrowError("Sign requires (Buffer, {Buffer(32 or 64) | keyPair object})");
TYPEERROR(Sign requires (Buffer, {Buffer(32 or 64) | keyPair object}))
}
Handle<Object> message = info[0]->ToObject();
const unsigned char* messageData = (unsigned char*)Buffer::Data(message);
Expand All @@ -80,7 +77,7 @@ NAN_METHOD(Sign) {
unsigned char *signatureMessageData = (unsigned char*) malloc(sigLen);
crypto_sign(signatureMessageData, &sigLen, messageData, messageLen, privateKey);

v8::Local<v8::Object> signature = Nan::NewBuffer(64).ToLocalChecked();
v8::Local<v8::Object> signature = node::Buffer::New(isolate, 64).ToLocalChecked();
unsigned char* signatureData = (unsigned char*)Buffer::Data(signature);
for (int i = 0; i < 64; i++) {
signatureData[i] = signatureMessageData[i];
Expand All @@ -97,16 +94,17 @@ NAN_METHOD(Sign) {
* publicKey: publicKey to the private key that created the signature
* returns: boolean
**/
NAN_METHOD(Verify) {
static void Verify(const v8::FunctionCallbackInfo<v8::Value>& info) {
ISOLATE(info);
if ((info.Length() < 3) || (!Buffer::HasInstance(info[0]->ToObject())) ||
(!Buffer::HasInstance(info[1]->ToObject())) || (!Buffer::HasInstance(info[2]->ToObject()))) {
return Nan::ThrowError("Verify requires (Buffer, Buffer(64), Buffer(32)");
TYPEERROR(Verify requires Buffer, Buffer(64), Buffer(32))
}
Handle<Object> message = info[0]->ToObject();
Handle<Object> signature = info[1]->ToObject();
Handle<Object> publicKey = info[2]->ToObject();
if ((Buffer::Length(signature) != 64) || (Buffer::Length(publicKey) != 32)) {
return Nan::ThrowError("Verify requires (Buffer, Buffer(64), Buffer(32)");
TYPEERROR(Verify requires (Buffer, Buffer(64), Buffer(32)))
}
unsigned char* messageData = (unsigned char*)Buffer::Data(message);
size_t messageLen = Buffer::Length(message);
Expand All @@ -116,11 +114,10 @@ NAN_METHOD(Verify) {
info.GetReturnValue().Set(crypto_sign_verify(signatureData, messageData, messageLen, publicKeyData) == 0);
}


void InitModule(Handle<Object> exports) {
Nan::SetMethod(exports, "MakeKeypair", MakeKeypair);
Nan::SetMethod(exports, "Sign", Sign);
Nan::SetMethod(exports, "Verify", Verify);
void InitModule(v8::Local<v8::Object> exports) {
NODE_SET_METHOD(exports, "MakeKeypair", MakeKeypair);
NODE_SET_METHOD(exports, "Sign", Sign);
NODE_SET_METHOD(exports, "Verify", Verify);
}

NODE_MODULE(ed25519, InitModule)
5 changes: 5 additions & 0 deletions src/ed25519/ed25519.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

#include <stddef.h>

#define ISOLATE(V) v8::Isolate* isolate = (V).GetIsolate();\
v8::HandleScope scope(isolate);

#define TYPEERROR(E) isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, ("Error: "#E)))); return;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down