/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * Returns a random integer between min and max
 */
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

/**
 * Build a digits code
 */
function digitsCode(size) {
  var s = size;
  var code = "";
  while (s > 0) {
    s--;
    code += getRandomInt(0, 9);
  }
  return code;
}

module.exports = {
  digitsCode: digitsCode
};

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var crypto = require("crypto");

/**
 * Build a digits code
 */
function digitsCode(size) {
  var nbBytes = Math.ceil(size/2);
  return parseInt(crypto.randomBytes(nbBytes).toString("hex"), 16)
    .toString().substr(0, size);
}

module.exports = {
  digitsCode: digitsCode
};

Refactor digitCode to use /dev/urandom instead of Math.random. r=Tarek