Advertisement
Guest User

nodejs

a guest
Feb 14th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. console.log('starting password manager');
  2. var storage = require('node-persist');
  3. var crypto = require('crypto-js');
  4. storage.initSync();
  5. var argv = require('yargs')
  6.         .command('create','Create a new account',function(yargs)
  7.         {
  8.             yargs.options({
  9.                  Name:{
  10.                     demand: true,
  11.                     alias: 'n',
  12.                     description: 'Account name',               
  13.                     type: 'string'
  14.                 },
  15.                  userName:{
  16.                     demand:true,
  17.                     alias :'u',
  18.                     description: 'User name',              
  19.                     type:'string'
  20.                 },
  21.                 password:{
  22.                     demand: true,
  23.                     alias: 'p',
  24.                     description: 'Account Password',                 
  25.                     type: 'string'
  26.                 },
  27.                 masterPassword:
  28.                 {
  29.                     demand : true,
  30.                     alias  : 'm',
  31.                     description : 'Master Password',
  32.                     type : 'string'
  33.  
  34.                 }
  35.             }).help('help');
  36.  
  37.         })
  38.  
  39.         .command('get','Get a new account',function(yargs)
  40.         {
  41.             yargs.options({
  42.                  Name:{
  43.                     demand: true,
  44.                     alias: 'n',
  45.                     description: 'Account name',               
  46.                     type: 'string'
  47.                     }
  48.             }).help('help');
  49.  
  50.         })
  51.  
  52.         .help('help')
  53.         .argv;
  54. var command = argv._[0];
  55.  
  56. function getAccounts(masterPassword)
  57. {
  58.     var encryptedAccount = storage.getItemSync('accounts');
  59.     var accounts = [];
  60.     if(typeof encryptedAccount != 'undefined')
  61.     {
  62.         var bytes = crypto.AES.decrypt(encryptedAccount,masterPassword);
  63.         var account = JSON.parse(bytes.toString(crypto.enc.Utf8));
  64.     }
  65.    
  66.     return accounts;
  67. }
  68.  
  69. function saveAccount(accounts,masterPassword)
  70. {
  71.     var encryptedAccounts = crypto.AES.encrypt(JSON.stringify(accounts),masterPassword);
  72.     storage.setItemSync('accounts',encryptedAccounts);
  73.  
  74.     return accounts;
  75. }
  76.  
  77. function createAccount(account,masterPassword)
  78. {
  79.     var accounts = getAccounts(masterPassword);
  80.     accounts.push(account);
  81.     saveAccount(accounts,masterPassword);
  82. //    storage.setItemSync('accounts',accounts);
  83.     return account;
  84.    
  85. }
  86. function getAccount(accountName,masterPassword)
  87. {
  88.     var accounts = getAccounts(masterPassword);
  89.     var matchedAccount;
  90.     accounts.forEach(function(account){
  91.         if(account.Name === accountName)
  92.             matchedAccount = accounts;
  93.     });
  94.     return matchedAccount;
  95. }
  96.  
  97. if(command === 'create' )
  98. {
  99.     var createdAccount = createAccount({
  100.         Name     : argv.Name,
  101.         userName : argv.userName,
  102.         password : argv.password
  103.     },argv.masterPassword);
  104.     console.log("Account Created");
  105.     console.log(createAccount);
  106. }
  107. else if(command ==='get')
  108. {
  109.     var fetchedAccount = getAccount(argv.Name,argv.masterPassword);
  110.     //var fetchedAccount ="gmail"
  111.     if(typeof fetchedAccount == 'undefined')
  112.         console.log(" Account not found");
  113.     else
  114.         console.log("Account found");
  115.         console.log(fetchedAccount);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement