diff --git a/lib/crypto.browser.js b/lib/crypto-platform.browser.js similarity index 90% rename from lib/crypto.browser.js rename to lib/crypto-platform.browser.js index 8052a8e..d6bf688 100644 --- a/lib/crypto.browser.js +++ b/lib/crypto-platform.browser.js @@ -1,11 +1,9 @@ /** - * Browser version of the crypto for Bitmessage JS implementation. + * Browser Bitmessage crypto implementation. * * Documentation: * Browsers support: * Blink implementation details: - * - * @module bitmessage/crypto.browser */ "use strict"; diff --git a/lib/crypto-platform.js b/lib/crypto-platform.js new file mode 100644 index 0000000..47eaf49 --- /dev/null +++ b/lib/crypto-platform.js @@ -0,0 +1,27 @@ +/** + * Node.js Bitmessage crypto implementation. + * @module bitmessage/crypto-platform + */ + +"use strict"; + +require("es6-promise").polyfill(); +var crypto = require("crypto"); + +exports.sha512 = function(buf) { + var hash = crypto.createHash("sha512"); + hash.update(buf); + return Promise.resolve(hash.digest()); +}; + +exports.sha256 = function(buf) { + var hash = crypto.createHash("sha256"); + hash.update(buf); + return Promise.resolve(hash.digest()); +}; + +exports.ripemd160 = function(buf) { + var hash = crypto.createHash("ripemd160"); + hash.update(buf); + return Promise.resolve(hash.digest()); +}; diff --git a/lib/crypto.js b/lib/crypto.js index c8b5154..c77f550 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -1,27 +1,12 @@ /** - * Node.js version of the crypto for Bitmessage JS implementation. + * Isomorphic Bitmessage crypto module. Reexport platform-specific + * functions and also export some common routines. * @module bitmessage/crypto */ "use strict"; -require("es6-promise").polyfill(); -var crypto = require("crypto"); - -exports.sha512 = function(buf) { - var hash = crypto.createHash("sha512"); - hash.update(buf); - return Promise.resolve(hash.digest()); -}; - -exports.sha256 = function(buf) { - var hash = crypto.createHash("sha256"); - hash.update(buf); - return Promise.resolve(hash.digest()); -}; - -exports.ripemd160 = function(buf) { - var hash = crypto.createHash("ripemd160"); - hash.update(buf); - return Promise.resolve(hash.digest()); -}; +var cryptoPlatform = require("./crypto-platform"); +Object.keys(cryptoPlatform).forEach(function(key) { + exports[key] = cryptoPlatform[key]; +}); diff --git a/package.json b/package.json index 16547e1..bb13879 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "./lib/index.js", "browser": { "int64-native": "node-int64", - "./lib/crypto.js": "./lib/crypto.browser.js" + "./lib/crypto-platform.js": "./lib/crypto-platform.browser.js" }, "scripts": { "test": "mocha && xvfb-run -a karma start && jshint .",