From 19129b090306b4f086f996f5727b33f90110f50c Mon Sep 17 00:00:00 2001 From: Kagami Hiiragi Date: Sat, 13 Dec 2014 21:56:14 +0300 Subject: [PATCH] Start with isomorphic sha512 wrapper --- lib/crypto.browser.js | 4 ++++ lib/crypto.js | 16 ++++++++++++++++ index.js => lib/index.js | 0 package.json | 6 +++++- test.js | 13 +++++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 lib/crypto.browser.js create mode 100644 lib/crypto.js rename index.js => lib/index.js (100%) diff --git a/lib/crypto.browser.js b/lib/crypto.browser.js new file mode 100644 index 0000000..3c553ff --- /dev/null +++ b/lib/crypto.browser.js @@ -0,0 +1,4 @@ +/** + * Browser version of the crypto for Bitmessage JS implementation. + * @module bitmessage/lib/crypto.browser + */ diff --git a/lib/crypto.js b/lib/crypto.js new file mode 100644 index 0000000..9ced6f0 --- /dev/null +++ b/lib/crypto.js @@ -0,0 +1,16 @@ +/** + * Node.js version of the crypto for Bitmessage JS implementation. + * Wrap all crypto functions with promises because WebCryptoAPI uses it + * throughout. + * @module bitmessage/lib/crypto + */ + +"use strict"; + +var crypto = require("crypto"); + +exports.sha512 = function(buf) { + var hash = crypto.createHash("sha512"); + hash.update(buf); + return Promise.resolve(hash.digest()); +}; diff --git a/index.js b/lib/index.js similarity index 100% rename from index.js rename to lib/index.js diff --git a/package.json b/package.json index a645de6..0a1331a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,10 @@ "name": "bitmessage", "version": "0.0.0", "description": "JavaScript Bitmessage library", - "main": "index.js", + "main": "./lib/index", + "browser": { + "./lib/crypto": "./lib/crypto.browser" + }, "scripts": { "test": "mocha && jshint ." }, @@ -24,6 +27,7 @@ "homepage": "https://github.com/nekogrid/bitmessage", "devDependencies": { "chai": "*", + "es6-promise": "^2.0.1", "jshint": "*", "mocha": "*" } diff --git a/test.js b/test.js index e69de29..25e63e7 100644 --- a/test.js +++ b/test.js @@ -0,0 +1,13 @@ +var expect = require("chai").expect; + +require("es6-promise").polyfill(); +var crypto = require("./lib/crypto"); + +describe("crypto", function() { + it("should calculate sha512 hash for both node and browserify", function(done) { + crypto.sha512(new Buffer("test")).then(function(res) { + expect(res.toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); + done(); + }); + }); +});