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.
35 lines
1.0 KiB
35 lines
1.0 KiB
11 years ago
|
var AES = require('./aes');
|
||
|
var CBC = require('./cbc');
|
||
|
var Random = require('../random');
|
||
|
var Hash = require('../hash');
|
||
|
|
||
11 years ago
|
// Symmetric encryption with AES and CBC convenience class
|
||
|
var AESCBC = function AESCBC() {
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
AESCBC.encrypt = function(messagebuf, passwordstr) {
|
||
11 years ago
|
var cipherkeybuf = Hash.sha256(new Buffer(passwordstr));
|
||
11 years ago
|
return AESCBC.encryptCipherkey(messagebuf, cipherkeybuf);
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
AESCBC.decrypt = function(encbuf, passwordstr) {
|
||
11 years ago
|
var cipherkeybuf = Hash.sha256(new Buffer(passwordstr));
|
||
11 years ago
|
return AESCBC.decryptCipherkey(encbuf, cipherkeybuf);
|
||
11 years ago
|
};
|
||
|
|
||
11 years ago
|
AESCBC.encryptCipherkey = function(messagebuf, cipherkeybuf, ivbuf) {
|
||
11 years ago
|
ivbuf = ivbuf || Random.getRandomBuffer(128 / 8);
|
||
|
var ctbuf = CBC.encrypt(messagebuf, ivbuf, AES, cipherkeybuf);
|
||
|
var encbuf = Buffer.concat([ivbuf, ctbuf]);
|
||
|
return encbuf;
|
||
|
};
|
||
|
|
||
11 years ago
|
AESCBC.decryptCipherkey = function(encbuf, cipherkeybuf) {
|
||
11 years ago
|
var ivbuf = encbuf.slice(0, 128 / 8);
|
||
|
var ctbuf = encbuf.slice(128 / 8);
|
||
|
var messagebuf = CBC.decrypt(ctbuf, ivbuf, AES, cipherkeybuf);
|
||
|
return messagebuf;
|
||
|
};
|
||
|
|
||
11 years ago
|
module.exports = AESCBC;
|