updates to cryptoService for rsa keypairs

This commit is contained in:
Kyle Spearrin 2017-02-20 22:21:14 -05:00
parent 3ed69d887f
commit 2478a8f3cc
1 changed files with 57 additions and 5 deletions

View File

@ -4,13 +4,19 @@ angular
.factory('cryptoService', function ($sessionStorage) { .factory('cryptoService', function ($sessionStorage) {
var _service = {}, var _service = {},
_key, _key,
_b64Key; _b64Key,
_privateKey;
_service.setKey = function (key) { _service.setKey = function (key) {
_key = key; _key = key;
$sessionStorage.key = forge.util.encode64(key); $sessionStorage.key = forge.util.encode64(key);
}; };
_service.setPrivateKey = function (privateKey) {
_privateKey = privateKey;
$sessionStorage.privateKey = forge.util.encode64(privateKey);
};
_service.getKey = function (b64) { _service.getKey = function (b64) {
if (b64 && b64 === true && _b64Key) { if (b64 && b64 === true && _b64Key) {
return _b64Key; return _b64Key;
@ -46,11 +52,33 @@ angular
return buffer.getBytes(16); return buffer.getBytes(16);
}; };
_service.getPrivateKey = function () {
if (_privateKey) {
return _privateKey;
}
if ($sessionStorage.privateKey) {
_privateKey = forge.util.decode64($sessionStorage.privateKey);
}
return _privateKey;
};
_service.clearKey = function () { _service.clearKey = function () {
_key = _b64Key = null; _key = _b64Key = null;
delete $sessionStorage.key; delete $sessionStorage.key;
}; };
_service.clearPrivateKey = function () {
_privateKey = null;
delete $sessionStorage.privateKey;
};
_service.clearKeys = function () {
_service.clearKey();
_service.clearPrivateKey();
};
_service.makeKey = function (password, salt, b64) { _service.makeKey = function (password, salt, b64) {
var key = forge.pbkdf2(forge.util.encodeUtf8(password), forge.util.encodeUtf8(salt), var key = forge.pbkdf2(forge.util.encodeUtf8(password), forge.util.encodeUtf8(salt),
5000, 256 / 8, 'sha256'); 5000, 256 / 8, 'sha256');
@ -62,6 +90,23 @@ angular
return key; return key;
}; };
_service.makeKeyPair = function (callback) {
forge.pki.rsa.generateKeyPair({ bits: 2048, workers: 2 }, function (error, keypair) {
if (error) {
callback(null, null, error);
return;
}
var privateKey = forge.pki.privateKeyToAsn1(keypair.privateKey);
var privateKeyBytes = forge.asn1.toDer(privateKey).getBytes();
var publicKey = forge.pki.publicKeyToAsn1(keypair.publicKey);
var publicKeyBytes = forge.asn1.toDer(publicKey).getBytes();
callback(privateKeyBytes, publicKeyBytes, null);
});
};
_service.hashPassword = function (password, key) { _service.hashPassword = function (password, key) {
if (!key) { if (!key) {
key = _service.getKey(); key = _service.getKey();
@ -75,7 +120,7 @@ angular
return forge.util.encode64(hashBits); return forge.util.encode64(hashBits);
}; };
_service.encrypt = function (plaintextValue, key) { _service.encrypt = function (plainValue, key, encoding) {
if (!_service.getKey() && !key) { if (!_service.getKey() && !key) {
throw 'Encryption key unavailable.'; throw 'Encryption key unavailable.';
} }
@ -89,7 +134,8 @@ angular
encKey = key || _service.getKey(); encKey = key || _service.getKey();
} }
var buffer = forge.util.createBuffer(plaintextValue, 'utf8'); encoding = encoding || 'utf8';
var buffer = forge.util.createBuffer(plainValue, encoding);
var ivBytes = forge.random.getBytesSync(16); var ivBytes = forge.random.getBytesSync(16);
var cipher = forge.cipher.createCipher('AES-CBC', encKey); var cipher = forge.cipher.createCipher('AES-CBC', encKey);
cipher.start({ iv: ivBytes }); cipher.start({ iv: ivBytes });
@ -110,7 +156,7 @@ angular
return cipherString; return cipherString;
}; };
_service.decrypt = function (encValue) { _service.decrypt = function (encValue, outputEncoding) {
if (!_service.getKey()) { if (!_service.getKey()) {
throw 'AES encryption unavailable.'; throw 'AES encryption unavailable.';
} }
@ -138,7 +184,13 @@ angular
decipher.update(ctBuffer); decipher.update(ctBuffer);
decipher.finish(); decipher.finish();
outputEncoding = outputEncoding || 'utf8';
if (outputEncoding === 'utf8') {
return decipher.output.toString('utf8'); return decipher.output.toString('utf8');
}
else {
return decipher.output.getBytes();
}
}; };
function computeMac(ct, iv, macKey) { function computeMac(ct, iv, macKey) {