<commit_before>var crypto = require('crypto');
var bignum = require('bignum');
var Binary = require('binary');
var Put = require('bufferput');
var buffertools = require('buffertools');
// var node_scrypt = require('scryptsy').scrypt;
// add scrypt jane hash nodejs module
var node_scryptJane = require('scrypt-jane-hash').scryptJane;
var sjcl = require('../lib/sjcl');
if (process.browser) {
  var hashjs = require('hash.js');
}

var sha256 = exports.sha256 = function(data) {
  return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
};

var sha512 = exports.sha512 = function(data) {
  if (process.browser) {
    var datahex = data.toString('hex');
    var databits = sjcl.codec.hex.toBits(datahex);
    var hashbits = sjcl.hash.sha512.hash(databits);
    var hashhex = sjcl.codec.hex.fromBits(hashbits);
    var hash = new Buffer(hashhex, 'hex');
    return hash;
  };
  return new Buffer(crypto.createHash('sha512').update(data).digest('binary'), 'binary');
};

var sha512hmac = exports.sha512hmac = function(data, key) {
  if (process.browser) {
    var skey = sjcl.codec.hex.toBits(key.toString('hex'));
    var sdata = sjcl.codec.hex.toBits(data.toString('hex'));
    var hmac = new sjcl.misc.hmac(skey, sjcl.hash.sha512);
    var encrypted = hmac.encrypt(sdata);
    var enchex = sjcl.codec.hex.fromBits(encrypted);
    var encbuf = new Buffer(enchex, 'hex');
    return encbuf;
  };
  var hmac = crypto.createHmac('sha512', key);
  var hash = hmac.update(data).digest();
  return hash;
};

var ripe160 = exports.ripe160 = function(data) {
  if (!Buffer.isBuffer(data)) {
    throw new Error('arg should be a buffer');
  }
  if (process.browser) {
    return new Buffer(hashjs.ripemd160().update(data).digest());
  }
  return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary');
};

var sha1 = exports.sha1 = function(data) {
  return new Buffer(crypto.createHash('sha1').update(data).digest('binary'), 'binary');
};

var twoSha256 = exports.twoSha256 = function(data) {
  return sha256(sha256(data));
};

var sha256ripe160 = exports.sha256ripe160 = function(data) {
  return ripe160(sha256(data));
};

// var scrypt = exports.scrypt = function(data) {
// scrypt(passwd, salt, N, r, p, dkLen)
// return node_scrypt(data, data, 1024, 1, 1, 32);
// };
var scryptJane = exports.scryptJane = function(data) {
  return node_scryptJane(data, data, 1024, 1, 1, 32);
};

/**
 * Format a block hash like the official client does.
 */
var formatHash = exports.formatHash = function(hash) {
  var hashEnd = new Buffer(10);
  hash.copy(hashEnd, 0, 22, 32);
  return buffertools.reverse(hashEnd).toString('hex');
};

/**
 * Display the whole hash, as hex, in correct endian order.
 */
var formatHashFull = exports.formatHashFull = function(hash) {
  var copy = new Buffer(hash.length);
  hash.copy(copy);
  var hex = buffertools.toHex(buffertools.reverse(copy));
  return hex;
};

/**
 * Format a block hash like Block Explorer does.
 *
 * Formats a block hash by removing leading zeros and truncating to 10 characters.<commit_msg>Update util.js<commit_after>var crypto = require('crypto');
var bignum = require('bignum');
var Binary = require('binary');
var Put = require('bufferput');
var buffertools = require('buffertools');
// var node_scrypt = require('scryptsy').scrypt;
// add scrypt jane hash nodejs module
// var node_scryptJane = require('scrypt-jane-hash').scryptJane;
var sjcl = require('../lib/sjcl');
if (process.browser) {
  var hashjs = require('hash.js');
}

var sha256 = exports.sha256 = function(data) {
  return new Buffer(crypto.createHash('sha256').update(data).digest('binary'), 'binary');
};

var sha512 = exports.sha512 = function(data) {
  if (process.browser) {
    var datahex = data.toString('hex');
    var databits = sjcl.codec.hex.toBits(datahex);
    var hashbits = sjcl.hash.sha512.hash(databits);
    var hashhex = sjcl.codec.hex.fromBits(hashbits);
    var hash = new Buffer(hashhex, 'hex');
    return hash;
  };
  return new Buffer(crypto.createHash('sha512').update(data).digest('binary'), 'binary');
};

var sha512hmac = exports.sha512hmac = function(data, key) {
  if (process.browser) {
    var skey = sjcl.codec.hex.toBits(key.toString('hex'));
    var sdata = sjcl.codec.hex.toBits(data.toString('hex'));
    var hmac = new sjcl.misc.hmac(skey, sjcl.hash.sha512);
    var encrypted = hmac.encrypt(sdata);
    var enchex = sjcl.codec.hex.fromBits(encrypted);
    var encbuf = new Buffer(enchex, 'hex');
    return encbuf;
  };
  var hmac = crypto.createHmac('sha512', key);
  var hash = hmac.update(data).digest();
  return hash;
};

var ripe160 = exports.ripe160 = function(data) {
  if (!Buffer.isBuffer(data)) {
    throw new Error('arg should be a buffer');
  }
  if (process.browser) {
    return new Buffer(hashjs.ripemd160().update(data).digest());
  }
  return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary');
};

var sha1 = exports.sha1 = function(data) {
  return new Buffer(crypto.createHash('sha1').update(data).digest('binary'), 'binary');
};

var twoSha256 = exports.twoSha256 = function(data) {
  return sha256(sha256(data));
};

var sha256ripe160 = exports.sha256ripe160 = function(data) {
  return ripe160(sha256(data));
};

// var scrypt = exports.scrypt = function(data) {
// scrypt(passwd, salt, N, r, p, dkLen)
// return node_scrypt(data, data, 1024, 1, 1, 32);
// };
// var scryptJane = exports.scryptJane = function(data) {
//  return node_scryptJane(data, data, 1024, 1, 1, 32);
// };

/**
 * Format a block hash like the official client does.
 */
var formatHash = exports.formatHash = function(hash) {
  var hashEnd = new Buffer(10);
  hash.copy(hashEnd, 0, 22, 32);
  return buffertools.reverse(hashEnd).toString('hex');
};

/**
 * Display the whole hash, as hex, in correct endian order.
 */
var formatHashFull = exports.formatHashFull = function(hash) {
  var copy = new Buffer(hash.length);
  hash.copy(copy);
  var hex = buffertools.toHex(buffertools.reverse(copy));
  return hex;
};

/**
 * Format a block hash like Block Explorer does.
 *
 * Formats a block hash by removing leading zeros and truncating to 10 characters.
{'fraction_non_alphanumeric': 0.1602522639068564, 'fraction_numerical': 0.0333117723156533, 'mean_word_length': 4.350346020761246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5848301', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2080, 'n_words': 500}