index.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. // Load modules
  3. const Crypto = require('crypto');
  4. const Boom = require('boom');
  5. // Declare internals
  6. const internals = {};
  7. // Generate a cryptographically strong pseudo-random data
  8. exports.randomString = function (size) {
  9. const buffer = exports.randomBits((size + 1) * 6);
  10. if (buffer instanceof Error) {
  11. return buffer;
  12. }
  13. const string = buffer.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g, '');
  14. return string.slice(0, size);
  15. };
  16. // Return a random string of digits
  17. exports.randomDigits = function (size) {
  18. const buffer = exports.randomBits(size * 8);
  19. if (buffer instanceof Error) {
  20. return buffer;
  21. }
  22. const digits = [];
  23. for (let i = 0; i < buffer.length; ++i) {
  24. digits.push(Math.floor(buffer[i] / 25.6));
  25. }
  26. return digits.join('');
  27. };
  28. // Generate a buffer of random bits
  29. exports.randomBits = function (bits) {
  30. if (!bits ||
  31. bits < 0) {
  32. return Boom.internal('Invalid random bits count');
  33. }
  34. const bytes = Math.ceil(bits / 8);
  35. try {
  36. return Crypto.randomBytes(bytes);
  37. }
  38. catch (err) {
  39. return Boom.internal('Failed generating random bits: ' + err.message);
  40. }
  41. };
  42. // Compare two strings using fixed time algorithm (to prevent time-based analysis of MAC digest match)
  43. exports.fixedTimeComparison = function (a, b) {
  44. if (typeof a !== 'string' ||
  45. typeof b !== 'string') {
  46. return false;
  47. }
  48. let mismatch = (a.length === b.length ? 0 : 1);
  49. if (mismatch) {
  50. b = a;
  51. }
  52. for (let i = 0; i < a.length; ++i) {
  53. const ac = a.charCodeAt(i);
  54. const bc = b.charCodeAt(i);
  55. mismatch |= (ac ^ bc);
  56. }
  57. return (mismatch === 0);
  58. };