aes.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. var test = require('tape');
  3. var bcCrypto = require('browserify-cipher/browser');
  4. var bcCyphers = bcCrypto.getCiphers();
  5. var randomBytes = require('pseudorandombytes');
  6. for (var i = 0; i < 4; i += 1) {
  7. bcCrypto.listCiphers().forEach(function (cipher) {
  8. test('run: ' + i, function (t) {
  9. /* eslint no-loop-func: 0 */
  10. t.test('ciphers: ' + cipher, function (st) {
  11. st.plan(1);
  12. var data = randomBytes(562);
  13. var password = randomBytes(20);
  14. var crypter = bcCrypto.createCipher(cipher, password);
  15. var decrypter = bcCrypto.createDecipher(cipher, password);
  16. var out = [];
  17. out.push(decrypter.update(crypter.update(data)));
  18. out.push(decrypter.update(crypter['final']()));
  19. if (cipher.indexOf('gcm') > -1) {
  20. decrypter.setAuthTag(crypter.getAuthTag());
  21. }
  22. out.push(decrypter['final']());
  23. st.equals(data.toString('hex'), Buffer.concat(out).toString('hex'));
  24. });
  25. });
  26. });
  27. }
  28. test('getCiphers', function (t) {
  29. t.plan(1);
  30. t.ok(bcCyphers.length, 'get ciphers returns an array');
  31. });
  32. // eslint-disable-next-line global-require
  33. test('through crypto browserify works', { skip: !require('crypto').createCipher && 'node 22+ removes createCipher' }, function (t) {
  34. t.plan(2);
  35. var crypto = require('../'); // eslint-disable-line global-require
  36. var cipher = 'aes-128-ctr';
  37. var data = randomBytes(562);
  38. var password = randomBytes(20);
  39. var crypter = crypto.createCipher(cipher, password);
  40. var decrypter = crypto.createDecipher(cipher, password);
  41. var out = [];
  42. out.push(decrypter.update(crypter.update(data)));
  43. out.push(decrypter.update(crypter['final']()));
  44. out.push(decrypter['final']());
  45. t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'));
  46. t.ok(crypto.getCiphers().length, 'get ciphers returns an array');
  47. });