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.

fixProc.js 1.2KB

12345678910111213141516171819202122232425262728293031
  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. var Buffer = require('safe-buffer').Buffer
  8. module.exports = function (okey, password) {
  9. var key = okey.toString()
  10. var match = key.match(findProc)
  11. var decrypted
  12. if (!match) {
  13. var match2 = key.match(fullRegex)
  14. decrypted = new Buffer(match2[2].replace(/[\r\n]/g, ''), 'base64')
  15. } else {
  16. var suite = 'aes' + match[1]
  17. var iv = Buffer.from(match[2], 'hex')
  18. var cipherText = Buffer.from(match[3].replace(/[\r\n]/g, ''), 'base64')
  19. var cipherKey = evp(password, iv.slice(0, 8), parseInt(match[1], 10)).key
  20. var out = []
  21. var cipher = ciphers.createDecipheriv(suite, cipherKey, iv)
  22. out.push(cipher.update(cipherText))
  23. out.push(cipher.final())
  24. decrypted = Buffer.concat(out)
  25. }
  26. var tag = key.match(startRegex)[1]
  27. return {
  28. tag: tag,
  29. data: decrypted
  30. }
  31. }