a zip code crypto-currency system good for red ONLY

drbg-test.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. const assert = require('assert');
  3. const HmacDRBG = require('../');
  4. const hash = require('hash.js');
  5. describe('Hmac_DRBG', () => {
  6. it('should support hmac-drbg-sha256', () => {
  7. function doDrbg(opt) {
  8. const drbg = HmacDRBG({
  9. hash: hash.sha256,
  10. entropy: opt.entropy,
  11. entropyEnc: 'utf8',
  12. nonce: opt.nonce,
  13. nonceEnc: 'utf8',
  14. pers: opt.pers,
  15. persEnc: 'utf8'
  16. });
  17. return drbg.generate(opt.size, 'hex');
  18. }
  19. const test = [
  20. {
  21. entropy: 'totally random0123456789',
  22. nonce: 'secret nonce',
  23. pers: 'my drbg',
  24. size: 32,
  25. res: '018ec5f8e08c41e5ac974eb129ac297c5388ee1864324fa13d9b15cf98d9a157'
  26. },
  27. {
  28. entropy: 'totally random0123456789',
  29. nonce: 'secret nonce',
  30. pers: null,
  31. size: 32,
  32. res: 'ed5d61ecf0ef38258e62f03bbb49f19f2cd07ba5145a840d83b134d5963b3633'
  33. }
  34. ];
  35. for (let i = 0; i < test.length; i++)
  36. assert.equal(doDrbg(test[i]), test[i].res);
  37. });
  38. describe('NIST vector', function() {
  39. require('./fixtures/hmac-drbg-nist.json').forEach(function (opt) {
  40. it('should not fail at ' + opt.name, function() {
  41. const drbg = HmacDRBG({
  42. hash: hash.sha256,
  43. entropy: opt.entropy,
  44. nonce: opt.nonce,
  45. pers: opt.pers
  46. });
  47. let last;
  48. for (let i = 0; i < opt.add.length; i++) {
  49. let add = opt.add[i];
  50. last = drbg.generate(opt.expected.length / 2, 'hex', add);
  51. }
  52. assert.equal(last, opt.expected);
  53. });
  54. });
  55. });
  56. describe('reseeding', function() {
  57. it('should reseed', function() {
  58. const entropy = 'totally random string with many chars that I typed ' +
  59. 'in agony';
  60. const nonce = 'nonce';
  61. const pers = 'pers';
  62. const original = HmacDRBG({
  63. hash: hash.sha256,
  64. entropy,
  65. nonce,
  66. pers
  67. });
  68. const reseeded = HmacDRBG({
  69. hash: hash.sha256,
  70. entropy,
  71. nonce,
  72. pers
  73. });
  74. assert.strictEqual(original.generate(32, 'hex'),
  75. reseeded.generate(32, 'hex'));
  76. reseeded.reseed('another absolutely random string');
  77. assert.notEqual(original.generate(32, 'hex'),
  78. reseeded.generate(32, 'hex'));
  79. });
  80. });
  81. });