Reorganize platform-specific routines

This commit is contained in:
Kagami Hiiragi 2015-01-03 13:29:22 +03:00
parent 7048464691
commit f6165d891b
5 changed files with 66 additions and 61 deletions

View File

@ -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.<Buffer>} 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.<Buffer>} 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.<Buffer>} 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);
};

View File

@ -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.<Buffer>} Resulting hash.
* @function
*/
exports.sha512 = platform.sha512;
/**
* Calculate SHA-256 hash.
* @param {Buffer} buf - Input data
* @return {Promise.<Buffer>} Resulting hash.
* @function
*/
exports.sha256 = platform.sha256;
/**
* Calculate RIPEMD-160 hash.
* @param {Buffer} buf - Input data
* @return {Promise.<Buffer>} 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);
};
/**

View File

@ -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}

29
lib/platform.js Normal file
View File

@ -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;

View File

@ -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 .",