diff --git a/lib/crypto-platform.js b/lib/crypto-platform.js deleted file mode 100644 index 0cb0ad8..0000000 --- a/lib/crypto-platform.js +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Node.js Bitmessage crypto implementation. - * @module bitmessage/crypto-platform - */ - -"use strict"; - -require("es6-promise").polyfill(); -var crypto = require("crypto"); - -/** - * Calculate SHA-512 hash. - * @param {Buffer} buf - Input data - * @return {Promise.} Resulting hash. - */ -exports.sha512 = function(buf) { - var hash = crypto.createHash("sha512"); - hash.update(buf); - return Promise.resolve(hash.digest()); -}; - -/** - * Calculate SHA-256 hash. - * @param {Buffer} buf - Input data - * @return {Promise.} Resulting hash. - */ -exports.sha256 = function(buf) { - var hash = crypto.createHash("sha256"); - hash.update(buf); - return Promise.resolve(hash.digest()); -}; - -/** - * Calculate RIPEMD-160 hash. - * @param {Buffer} buf - Input data - * @return {Promise.} Resulting hash. - */ -exports.ripemd160 = function(buf) { - var hash = crypto.createHash("ripemd160"); - hash.update(buf); - return Promise.resolve(hash.digest()); -}; - -/** - * Generate cryptographically strong pseudo-random data. - * @param {number} size - Number of bytes - * @return {Buffer} Buffer with random data. - */ -exports.randomBytes = function(size) { - return crypto.randomBytes(size); -}; diff --git a/lib/crypto.js b/lib/crypto.js index 7588efa..79ce268 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -1,25 +1,52 @@ /** - * Isomorphic Bitmessage crypto module. Reexports - * [platform-specific functions]{@link module:bitmessage/crypto-platform} - * and also some common routines. + * Isomorphic Bitmessage crypto module. Reexports platform-dependent + * implementations and and also some common routines. * @module bitmessage/crypto */ "use strict"; var eccrypto = require("eccrypto"); -var cryptoPlatform = require("./crypto-platform"); +var platform = require("./platform"); -Object.keys(cryptoPlatform).forEach(function(key) { - exports[key] = cryptoPlatform[key]; -}); +/** + * Calculate SHA-512 hash. + * @param {Buffer} buf - Input data + * @return {Promise.} Resulting hash. + * @function + */ +exports.sha512 = platform.sha512; + +/** + * Calculate SHA-256 hash. + * @param {Buffer} buf - Input data + * @return {Promise.} Resulting hash. + * @function + */ +exports.sha256 = platform.sha256; + +/** + * Calculate RIPEMD-160 hash. + * @param {Buffer} buf - Input data + * @return {Promise.} Resulting hash. + * @function + */ +exports.ripemd160 = platform.ripemd160; + +/** + * Generate cryptographically strong pseudo-random data. + * @param {number} size - Number of bytes + * @return {Buffer} Buffer with random data. + * @function + */ +exports.randomBytes = platform.randomBytes; /** * Generate new random private key. * @return {Buffer} New private key. */ exports.getPrivate = function() { - return cryptoPlatform.randomBytes(32); + return platform.randomBytes(32); }; /** diff --git a/lib/crypto-platform.browser.js b/lib/platform.browser.js similarity index 95% rename from lib/crypto-platform.browser.js rename to lib/platform.browser.js index c8e521f..7c4b5e1 100644 --- a/lib/crypto-platform.browser.js +++ b/lib/platform.browser.js @@ -1,5 +1,5 @@ /** - * Browser Bitmessage crypto implementation. + * Browser implementation of platform-specific routines. * @see {@link http://www.w3.org/TR/WebCryptoAPI/} * @see {@link http://caniuse.com/#feat=cryptography} * @see {@link https://sites.google.com/a/chromium.org/dev/blink/webcrypto} diff --git a/lib/platform.js b/lib/platform.js new file mode 100644 index 0000000..26884f1 --- /dev/null +++ b/lib/platform.js @@ -0,0 +1,29 @@ +/** + * Node.js implementation of platform-specific routines. + * @see {@link http://nodejs.org/api/crypto.html} + */ + +"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()); +}; + +exports.randomBytes = crypto.randomBytes; diff --git a/package.json b/package.json index dafc693..295a111 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "JavaScript Bitmessage library", "main": "./lib/index.js", "browser": { - "./lib/crypto-platform.js": "./lib/crypto-platform.browser.js" + "./lib/platform.js": "./lib/platform.browser.js" }, "scripts": { "test": "ALL_TESTS=1 mocha && xvfb-run -a karma start && jshint .",