2014-12-26 17:47:25 +01:00
|
|
|
/**
|
|
|
|
* Node.js Bitmessage crypto implementation.
|
|
|
|
* @module bitmessage/crypto-platform
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
require("es6-promise").polyfill();
|
|
|
|
var crypto = require("crypto");
|
|
|
|
|
2014-12-30 18:00:28 +01:00
|
|
|
/**
|
|
|
|
* Calculate SHA-512 hash.
|
|
|
|
* @param {Buffer} buf - Input data
|
|
|
|
* @return {Promise.<Buffer>} Resulting hash.
|
|
|
|
*/
|
2014-12-26 17:47:25 +01:00
|
|
|
exports.sha512 = function(buf) {
|
|
|
|
var hash = crypto.createHash("sha512");
|
|
|
|
hash.update(buf);
|
|
|
|
return Promise.resolve(hash.digest());
|
|
|
|
};
|
|
|
|
|
2014-12-30 18:00:28 +01:00
|
|
|
/**
|
|
|
|
* Calculate SHA-256 hash.
|
|
|
|
* @param {Buffer} buf - Input data
|
|
|
|
* @return {Promise.<Buffer>} Resulting hash.
|
|
|
|
*/
|
2014-12-26 17:47:25 +01:00
|
|
|
exports.sha256 = function(buf) {
|
|
|
|
var hash = crypto.createHash("sha256");
|
|
|
|
hash.update(buf);
|
|
|
|
return Promise.resolve(hash.digest());
|
|
|
|
};
|
|
|
|
|
2014-12-30 18:00:28 +01:00
|
|
|
/**
|
|
|
|
* Calculate RIPEMD-160 hash.
|
|
|
|
* @param {Buffer} buf - Input data
|
|
|
|
* @return {Promise.<Buffer>} Resulting hash.
|
|
|
|
*/
|
2014-12-26 17:47:25 +01:00
|
|
|
exports.ripemd160 = function(buf) {
|
|
|
|
var hash = crypto.createHash("ripemd160");
|
|
|
|
hash.update(buf);
|
|
|
|
return Promise.resolve(hash.digest());
|
|
|
|
};
|
2014-12-26 18:17:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
};
|