Skip to content

Instantly share code, notes, and snippets.

@ruda
Created May 31, 2016 01:30
Show Gist options
  • Save ruda/aaed2fadbf85d9397f23ae0c6ae7871c to your computer and use it in GitHub Desktop.
Save ruda/aaed2fadbf85d9397f23ae0c6ae7871c to your computer and use it in GitHub Desktop.
// pwmanager.js - RSTM
var crypto = require('crypto');
function encrypt(plaintext, keyword) {
var cipher = crypto.createCipher('aes-256-ctr', keyword);
var ciphertext = cipher.update(plaintext, 'utf8', 'hex');
ciphertext += cipher.final('hex');
return ciphertext;
};
function decrypt(ciphertext, keyword) {
var decipher = crypto.createDecipher('aes-256-ctr', keyword);
var plaintext = decipher.update(ciphertext, 'hex', 'utf8');
plaintext += decipher.final('utf8')
return plaintext;
};
module.exports = function (ctx, done) {
ctx.storage.get(function (error, data) {
//console.log(ctx.data);
if (error) return done(error);
data = data || {};
if (ctx.data.password) {
if (!data[ctx.data.site]) {
data[ctx.data.site] = {};
}
data[ctx.data.site][ctx.data.login] = encrypt(ctx.data.password, ctx.data.keyword);
ctx.storage.set(data, function (error) {
if (error) return done(error);
});
} else {
if (ctx.data.site) {
if (!data[ctx.data.site]) return done('Invalid site!');
if (ctx.data.login) {
if (!data[ctx.data.site][ctx.data.login]) return done('Invalid login!');
data = { 'password': decrypt(data[ctx.data.site][ctx.data.login], ctx.data.keyword) };
} else {
data = { 'login': Object.keys(data[ctx.data.site])};
};
} else {
data = { 'site': Object.keys(data) };
};
//console.log(data);
};
done(null, data);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment