1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-15 21:55:04 -05:00
mail/src/js/crypto/aes-gcm.js

51 lines
1.7 KiB
JavaScript
Raw Normal View History

2013-03-13 11:58:46 -04:00
'use strict';
/**
* A Wrapper for SJCL's authenticated AES-GCM encryption
*/
app.crypto.AesGCM = function() {
var adata = []; // authenticated data (empty by default)
var tlen = 128; // The tag length in bits
/**
* Encrypt a String using AES-GCM using the provided keysize (e.g. 128, 256)
* @param plaintext [String] The input string in UTF8
* @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV
* @return [String] The base64 encoded ciphertext
*/
this.encrypt = function(plaintext, key, iv) {
// convert parameters to WordArrays
var keyWords = sjcl.codec.base64.toBits(key);
var ivWords = sjcl.codec.base64.toBits(iv);
var plaintextWords = sjcl.codec.utf8String.toBits(plaintext);
var blockCipher = new sjcl.cipher.aes(keyWords);
var ciphertext = sjcl.mode.gcm.encrypt(blockCipher, plaintextWords, ivWords, adata, tlen);
var ctBase64 = sjcl.codec.base64.fromBits(ciphertext);
return ctBase64;
};
/**
* Decrypt a String using AES-GCM using the provided keysize (e.g. 128, 256)
* @param ciphertext [String] The base64 encoded ciphertext
* @param key [String] The base64 encoded key
* @param iv [String] The base64 encoded IV
* @return [String] The decrypted plaintext in UTF8
*/
this.decrypt = function(ciphertext, key, iv) {
// convert parameters to WordArrays
var keyWords = sjcl.codec.base64.toBits(key);
var ivWords = sjcl.codec.base64.toBits(iv);
var ciphertextWords = sjcl.codec.base64.toBits(ciphertext);
var blockCipher = new sjcl.cipher.aes(keyWords);
var decrypted = sjcl.mode.gcm.decrypt(blockCipher, ciphertextWords, ivWords, adata, tlen);
var pt = sjcl.codec.utf8String.fromBits(decrypted);
return pt;
};
};