browser.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var DES = require('browserify-des')
  2. var aes = require('browserify-aes/browser')
  3. var aesModes = require('browserify-aes/modes')
  4. var desModes = require('browserify-des/modes')
  5. var ebtk = require('evp_bytestokey')
  6. function createCipher (suite, password) {
  7. suite = suite.toLowerCase()
  8. var keyLen, ivLen
  9. if (aesModes[suite]) {
  10. keyLen = aesModes[suite].key
  11. ivLen = aesModes[suite].iv
  12. } else if (desModes[suite]) {
  13. keyLen = desModes[suite].key * 8
  14. ivLen = desModes[suite].iv
  15. } else {
  16. throw new TypeError('invalid suite type')
  17. }
  18. var keys = ebtk(password, false, keyLen, ivLen)
  19. return createCipheriv(suite, keys.key, keys.iv)
  20. }
  21. function createDecipher (suite, password) {
  22. suite = suite.toLowerCase()
  23. var keyLen, ivLen
  24. if (aesModes[suite]) {
  25. keyLen = aesModes[suite].key
  26. ivLen = aesModes[suite].iv
  27. } else if (desModes[suite]) {
  28. keyLen = desModes[suite].key * 8
  29. ivLen = desModes[suite].iv
  30. } else {
  31. throw new TypeError('invalid suite type')
  32. }
  33. var keys = ebtk(password, false, keyLen, ivLen)
  34. return createDecipheriv(suite, keys.key, keys.iv)
  35. }
  36. function createCipheriv (suite, key, iv) {
  37. suite = suite.toLowerCase()
  38. if (aesModes[suite]) return aes.createCipheriv(suite, key, iv)
  39. if (desModes[suite]) return new DES({ key: key, iv: iv, mode: suite })
  40. throw new TypeError('invalid suite type')
  41. }
  42. function createDecipheriv (suite, key, iv) {
  43. suite = suite.toLowerCase()
  44. if (aesModes[suite]) return aes.createDecipheriv(suite, key, iv)
  45. if (desModes[suite]) return new DES({ key: key, iv: iv, mode: suite, decrypt: true })
  46. throw new TypeError('invalid suite type')
  47. }
  48. function getCiphers () {
  49. return Object.keys(desModes).concat(aes.getCiphers())
  50. }
  51. exports.createCipher = exports.Cipher = createCipher
  52. exports.createCipheriv = exports.Cipheriv = createCipheriv
  53. exports.createDecipher = exports.Decipher = createDecipher
  54. exports.createDecipheriv = exports.Decipheriv = createDecipheriv
  55. exports.listCiphers = exports.getCiphers = getCiphers