Guest User

Untitled

a guest
Jul 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. const id = require('nanoid')
  2. const read = require('read')
  3. const clipboardy = require('clipboardy')
  4. const crypt = require('crypto')
  5. const fs = require('fs')
  6. const algo = 'aes-256-ctr'
  7.  
  8. read(
  9. { prompt: 'Master key: ', silent: true },
  10. (err, key) => {
  11. if (err) {
  12. throw err;
  13. return;
  14. }
  15.  
  16. init(key);
  17. }
  18. )
  19.  
  20. function init(key) {
  21. read(
  22. { prompt: 'Choose action: \n1. Generate\n2. Store to clipboard' },
  23. (err, output) => {
  24. if (output == '1') {
  25. generateNew(key)
  26. } else if (output == '2') {
  27. storeToClipboard(key)
  28. } else {
  29. init(key);
  30. }
  31. }
  32. )
  33. }
  34.  
  35. function generateNew(key) {
  36. read(
  37. { prompt: 'Slave key name: ' },
  38. (err, name) => {
  39. let cipher = crypt.createCipher(algo, key),
  40. encrypted = cipher.update(
  41. id(24),
  42. 'utf8',
  43. 'hex'
  44. )
  45.  
  46. encrypted += cipher.final('hex')
  47.  
  48. // store to file
  49. fs.writeFileSync(
  50. __dirname + '/keys/' + md5(name),
  51. encrypted
  52. )
  53. }
  54. )
  55. }
  56.  
  57. function storeToClipboard(key) {
  58. read(
  59. { prompt: 'Slave key name: ' },
  60. (err, name) => {
  61. let fc = fs.readFileSync(
  62. __dirname + '/keys/' + md5(name)
  63. ).toString()
  64.  
  65. let decipher = crypt.createDecipher(algo, key)
  66. let crypted = decipher.update(
  67. fc, 'hex', 'utf8'
  68. )
  69.  
  70. crypted += decipher.final('utf8')
  71.  
  72. clipboardy.writeSync(crypted);
  73. }
  74. )
  75. }
  76.  
  77. function md5(data) {
  78. return crypt.createHash('md5').update(data).digest("hex")
  79. }
Add Comment
Please, Sign In to add comment