fixProc.js 1.2 KB

123456789101112131415161718192021222324252627282930
  1. // adapted from https://github.com/apatil/pemstrip
  2. var findProc = /Proc-Type: 4,ENCRYPTED[\n\r]+DEK-Info: AES-((?:128)|(?:192)|(?:256))-CBC,([0-9A-H]+)[\n\r]+([0-9A-z\n\r\+\/\=]+)[\n\r]+/m
  3. var startRegex = /^-----BEGIN ((?:.* KEY)|CERTIFICATE)-----/m
  4. var fullRegex = /^-----BEGIN ((?:.* KEY)|CERTIFICATE)-----([0-9A-z\n\r\+\/\=]+)-----END \1-----$/m
  5. var evp = require('evp_bytestokey')
  6. var ciphers = require('browserify-aes')
  7. module.exports = function (okey, password) {
  8. var key = okey.toString()
  9. var match = key.match(findProc)
  10. var decrypted
  11. if (!match) {
  12. var match2 = key.match(fullRegex)
  13. decrypted = new Buffer(match2[2].replace(/[\r\n]/g, ''), 'base64')
  14. } else {
  15. var suite = 'aes' + match[1]
  16. var iv = new Buffer(match[2], 'hex')
  17. var cipherText = new Buffer(match[3].replace(/[\r\n]/g, ''), 'base64')
  18. var cipherKey = evp(password, iv.slice(0, 8), parseInt(match[1], 10)).key
  19. var out = []
  20. var cipher = ciphers.createDecipheriv(suite, cipherKey, iv)
  21. out.push(cipher.update(cipherText))
  22. out.push(cipher.final())
  23. decrypted = Buffer.concat(out)
  24. }
  25. var tag = key.match(startRegex)[1]
  26. return {
  27. tag: tag,
  28. data: decrypted
  29. }
  30. }