You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aes.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var test = require('tape')
  2. var crypto = require('browserify-cipher/browser')
  3. var randomBytes = require('pseudorandombytes')
  4. function runIt (i) {
  5. crypto.listCiphers().forEach(function (cipher) {
  6. test('run: ' + i, function (t) {
  7. t.test('ciphers: ' + cipher, function (t) {
  8. t.plan(1)
  9. var data = randomBytes(562)
  10. var password = randomBytes(20)
  11. var crypter = crypto.createCipher(cipher, password)
  12. var decrypter = crypto.createDecipher(cipher, password)
  13. var out = []
  14. out.push(decrypter.update(crypter.update(data)))
  15. out.push(decrypter.update(crypter.final()))
  16. if (cipher.indexOf('gcm') > -1) {
  17. decrypter.setAuthTag(crypter.getAuthTag())
  18. }
  19. out.push(decrypter.final())
  20. t.equals(data.toString('hex'), Buffer.concat(out).toString('hex'))
  21. })
  22. })
  23. })
  24. if (i < 4) {
  25. setTimeout(runIt, 0, i + 1)
  26. }
  27. }
  28. runIt(1)
  29. test('getCiphers', function (t) {
  30. t.plan(1)
  31. t.ok(crypto.getCiphers().length, 'get ciphers returns an array')
  32. })
  33. test('through crypto browserify works', function (t) {
  34. t.plan(2)
  35. var crypto = 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. })