<commit_before>/*
 * nodeON-helpers
 * A collection of helper funcs
 * https://github.com/thanpolas/nodeON-helpers
 *
 * Copyright ©2015 <NAME>
 * Licensed under the MIT license.
 */

/**
 * @fileoverview Helpers.
 */

var __ = require('lodash');
var bcrypt = require('bcrypt');
var slug = require('slug');

var helpers = module.exports = {};

var noop = function() {};

/** @type {string} Internal store for salt. */
helpers.__salt = '';

/**
 * Set the Salt for crypto functions.
 *
 * @param {string} salt The salt.
 */
helpers.setSalt = function (salt) {
  helpers.__salt = salt;
};

/**
 * Adds a {@code getInstance} static method that always return the same instance
 * object.
 * @param {!Function} Ctor The constructor for the class to add the static
 *     method to.
 */
helpers.addSingletonGetter = function(Ctor) {
  Ctor.getInstance = function() {
    if (Ctor._instance) {
      return Ctor._instance;
    }
    return Ctor._instance = new Ctor();
  };
};

/**
 * Generate a random string.
 *
 * @param  {number=} optLength How long the string should be, default 32.
 * @return {string} a random string.
 */
helpers.generateRandomString = function(optLength) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';

  var length = optLength || 32;
  var string = '';
  var randomNumber = 0;
  for (var i = 0; i < length; i++) {
    randomNumber = Math.floor(Math.random() * chars.length);
    string += chars.substring(randomNumber, randomNumber + 1);
  }

  return string;
};

/**
 * Generate a random number, returns type string!.
 *
 * @param  {number=} optLength How long, default 20.
 * @return {string} A random number cast to string.
 */
helpers.generateRandomNumber = function(optLength) {
  var nums = '0123456789';
  var numLen = nums.length;
  var length = optLength || 20;

  var string = '';
  var randomNumber = 0;
  for (var i = 0; i < length; i++) {
    randomNumber = Math.floor(Math.random() * numLen);
    string += nums.substring(randomNumber, randomNumber + 1);
  }

  return string;
};
/**
 * Create a salted hash of the provided string.
 *
 * @param {string} src The string we want to hash.
 * @param {Object|Function(Error=, string=)=} optOpts Options or callback.
 * @param {Function(Error=, string=)=} optDone Callback.
 */
helpers.hash = function(src, optOpts, optDone) {

  var opts = {};
  var done = noop;

  if (__.isObject(optOpts)) {
    opts = optOpts;
  }
  if (__.isFunction(optOpts)) {
    done = optOpts;
  }
  if (__.isFunction(optDone)) {
    done = optDone;
  }

  // bcrypt supports strings up to 72 chars long
  // @see http://security.stackexchange.com/questions/39849/does-bcrypt-have-a-maximum-password-length
  // @see https://github.com/ncb000gt/node.bcrypt.js/issues/325
  if (src.length > 71 && !opts.ignoreLimit) {
    var err = new Error('bcrypt does not support strings longer than 72 characters');
    done(err);
    return;
  }

  bcrypt.genSalt(10, function(err, salt) {
    if (err) {
      done(err);
      return;
    }
    bcrypt.hash(src, salt, done);
  });
};

/**
 * Check if a string matches a hash.
 *
 * @param {string} hash The encrypted hash.
 * @param {string} src The plain text string to verify.
 * @param {Object|Function(boolean)=} optOpts Options or callback.
 * @param {Function(boolean)=} optDone callback, first arg will always be bool.
 */
helpers.hashVerify = function(hash, src, optOpts, optDone) {
  var opts = {};
  var done = noop;

  if (__.isObject(optOpts)) {
    opts = optOpts;
  }
  if (__.isFunction(optOpts)) {
    done = optOpts;
  }
  if (__.isFunction(optDone)) {
    done = optDone;
  }

  bcrypt.compare(src, hash, function(err, res) {
    if (err) {
      done(false);
    } else {
      done(res);
    }
  });
};

/**
 * Salt a string.
 *
 * @param  {string} src The string we want to salt.
 * @return {string} The string salted.
 */
helpers.salt = function(src) {
  return src + helpers.__salt;
};


/**<commit_msg>remove crypto methods from helpers<commit_after>/*
 * nodeON-helpers
 * A collection of helper funcs
 * https://github.com/thanpolas/nodeON-helpers
 *
 * Copyright ©2015 <NAME>
 * Licensed under the MIT license.
 */

/**
 * @fileoverview Helpers.
 */

var __ = require('lodash');
var slug = require('slug');

var helpers = module.exports = {};

/** @type {string} Internal store for salt. */
helpers.__salt = '';

/**
 * Set the Salt for crypto functions.
 *
 * @param {string} salt The salt.
 */
helpers.setSalt = function (salt) {
  helpers.__salt = salt;
};

/**
 * Adds a {@code getInstance} static method that always return the same instance
 * object.
 * @param {!Function} Ctor The constructor for the class to add the static
 *     method to.
 */
helpers.addSingletonGetter = function(Ctor) {
  Ctor.getInstance = function() {
    if (Ctor._instance) {
      return Ctor._instance;
    }
    return Ctor._instance = new Ctor();
  };
};

/**
 * Generate a random string.
 *
 * @param  {number=} optLength How long the string should be, default 32.
 * @return {string} a random string.
 */
helpers.generateRandomString = function(optLength) {
  var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';

  var length = optLength || 32;
  var string = '';
  var randomNumber = 0;
  for (var i = 0; i < length; i++) {
    randomNumber = Math.floor(Math.random() * chars.length);
    string += chars.substring(randomNumber, randomNumber + 1);
  }

  return string;
};

/**
 * Generate a random number, returns type string!.
 *
 * @param  {number=} optLength How long, default 20.
 * @return {string} A random number cast to string.
 */
helpers.generateRandomNumber = function(optLength) {
  var nums = '0123456789';
  var numLen = nums.length;
  var length = optLength || 20;

  var string = '';
  var randomNumber = 0;
  for (var i = 0; i < length; i++) {
    randomNumber = Math.floor(Math.random() * numLen);
    string += nums.substring(randomNumber, randomNumber + 1);
  }

  return string;
};

/**
 * Salt a string.
 *
 * @param  {string} src The string we want to salt.
 * @return {string} The string salted.
 */
helpers.salt = function(src) {
  return src + helpers.__salt;
};


/**
{'fraction_non_alphanumeric': 0.14306569343065692, 'fraction_numerical': 0.015409570154095702, 'mean_word_length': 3.2436338609772886, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 6, 'https://': 3, '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': '2075790', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2033, 'n_words': 599}