Skip to content

Commit

Permalink
Starting to get somewhere.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Bornstein committed Feb 8, 2012
1 parent 4a7c92d commit 473f4a5
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 11 deletions.
31 changes: 30 additions & 1 deletion lib/rsab.js
Expand Up @@ -10,10 +10,38 @@

var rsabNative = require("../bin/rsabNative");

/*
* Variable definitions
*/

/** encoding constant */
var UTF8 = "utf8";


/*
* Exported bindings
*/

/**
* Create a new public key object, from the given PEM-encoded file. The
* argument may be either a Buffer (whose contents are interpreted as
* ASCII / UTF8) or a string.
*
* Using a Buffer is more efficient. (A string argument is internally
* converted into a buffer.)
*/
function pemToPublicKey(pem) {
if (typeof pem === "string") {
pem = new Buffer(pem, UTF8);
}

var result = new rsabNative.RsaWrap();
// FIXME; result.doSomething()

return result;
}


/*
* Initialization
*/
Expand All @@ -22,5 +50,6 @@ var rsabNative = require("../bin/rsabNative");
require("crypto");

module.exports = {
hello: rsabNative.hello
pemToPublicKey: pemToPublicKey,
n: rsabNative // FIXME: TEMP!!
};
73 changes: 63 additions & 10 deletions src/rsabNative.cc
@@ -1,19 +1,72 @@
// FIXME
// This placeholder is just the "Hello" example from the Node docs.
// Copyright 2012 The Obvious Corporation.

#include <node.h>
#include <v8.h>
#include "rsabNative.h"

#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#include <openssl/hmac.h>
#include <openssl/engine.h>
#include <openssl/rand.h>

using namespace v8;

Handle<Value> Method(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("world"));
}

/**
* Top-level initialization function.
*/
void init(Handle<Object> target) {
target->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Method)->GetFunction());
RsaWrap::InitClass(target);
}

NODE_MODULE(rsabNative, init)

/**
* Initialize the bindings for this class.
*/
void RsaWrap::InitClass(Handle<Object> target) {
Local<String> className = String::NewSymbol("RsaWrap");

// Basic instance setup
Local<FunctionTemplate> tpl = FunctionTemplate::New(New);
tpl->SetClassName(className);
tpl->InstanceTemplate()->SetInternalFieldCount(1); // required by ObjectWrap

// Prototype method bindings
Local<ObjectTemplate> proto = tpl->PrototypeTemplate();
proto->Set(String::NewSymbol("hello"),
FunctionTemplate::New(Hello)->GetFunction());

// Store the constructor in the target bindings.
target->Set(className, Persistent<Function>::New(tpl->GetFunction()));
}

RsaWrap::RsaWrap() {
rsa = NULL;
}

RsaWrap::~RsaWrap() {
if (rsa != NULL) {
RSA_free(rsa);
}
printf("~~~ DESTRUCTOR CALLED\n");
}

Handle<Value> RsaWrap::New(const Arguments& args) {
HandleScope scope;

printf("~~~ ALLOC\n");

RsaWrap *obj = new RsaWrap();
obj->Wrap(args.This());

return args.This();
}

// FIXME: Temporary!
Handle<Value> RsaWrap::Hello(const Arguments& args) {
HandleScope scope;
return scope.Close(String::New("world"));
}
28 changes: 28 additions & 0 deletions src/rsabNative.h
@@ -0,0 +1,28 @@
// Copyright 2012 The Obvious Corporation.

#ifndef RSAB_NATIVE_H
#define RSAB_NATIVE_H

#define BUILDING_NODE_EXTENSION
#include <node.h>
#include <node_object_wrap.h>
#include <v8.h>

#include <openssl/rsa.h>

class RsaWrap : node::ObjectWrap {
public:
static void InitClass(v8::Handle<v8::Object> target);

protected:
RsaWrap();
~RsaWrap();

static v8::Handle<v8::Value> New(const v8::Arguments& args);
static v8::Handle<v8::Value> Hello(const v8::Arguments& args);

private:
RSA *rsa;
};

#endif // def RSAB_NATIV_H

0 comments on commit 473f4a5

Please sign in to comment.