Skip to content

Instantly share code, notes, and snippets.

@lholmquist
Created November 11, 2013 18:55
Show Gist options
  • Save lholmquist/7418347 to your computer and use it in GitHub Desktop.
Save lholmquist/7418347 to your computer and use it in GitHub Desktop.
AeroGear.Crypto = function() {
...
// Local Variables
var privateKey, publicKey, IV;
...
// Method to provide symmetric encryption with GCM by default
/**
Encrypts in GCM mode
@status Experimental
@param {Object} options - includes IV (Initialization Vector), AAD
(Additional Authenticated Data), key (private key for encryption),
plainText (data to be encrypted)
@return {bitArray} - The encrypted data represented by an array of bytes
@example
//Data encryption:
var options = {
IV: myIV,
AAD: myAAD,
key: mySecretKey,
data: message
};
AeroGear.crypto.encrypt( options );
*/
// Mine
this.encrypt = function( options ) {
options = options || {};
var gcm = sjcl.mode.gcm,
key = new sjcl.cipher.aes ( options.key );
IV = options.IV || IV ? IV : this.getRandomValue(); // this will always use the value in options.IV if available
// or it will check to see if the local var IV is not null/undefined
// it that is there, then it uses it, else it gets a randomValue
// what ever it uses, it stores in the local var IV
return gcm.encrypt( key, options.data, IV, options.aad, 128 );
};
// Yours
// this.encrypt = function( options ) {
// options = options || {};
// var gcm = sjcl.mode.gcm,
// random = options.IV || this.getRandomValue(),
// key = new sjcl.cipher.aes ( options.key );
// IV = random;
// return gcm.encrypt( key, options.data, options.IV, options.aad, 128 );
// };
// Method to provide symmetric decryption with GCM by default
/**
Decrypts in GCM mode
@status Experimental
@param {Object} options - includes IV (Initialization Vector), AAD
(Additional Authenticated Data), key (private key for encryption),
ciphertext (data to be decrypted)
@return {bitArray} - The decrypted data
@example
//Data decryption:
var options = {
IV: myIV,
AAD: myAAD,
key: mySecretKey,
data: ciphertext
};
AeroGear.crypto.decrypt( options );
*/
this.decrypt = function( options ) {
options = options || {};
var gcm = sjcl.mode.gcm,
random = options.IV || IV,
key = new sjcl.cipher.aes ( options.key );
return gcm.decrypt( key, options.data, options.IV, options.aad, 128 );
};
// Method to provide secure hashing
/**
Generates a hash output based on SHA-256
@status Experimental
@param {bitArray|String} data to hash.
@return {bitArray} - Hash value
@example
//Data hashing:
AeroGear.crypto.hash( options );
*/
this.hash = function( data ) {
return sjcl.hash.sha256.hash( data );
};
// Method to provide digital signatures
/**
Sign messages with ECDSA
@status Experimental
@param {Object} options - includes keys (provided keys to sign the message),
message (message to be signed)
@return {bitArray} - Digital signature
@example
//Message sign:
var options = {
keys: providedKey,
message: PLAIN_TEXT
};
AeroGear.crypto.sign( options );
*/
this.sign = function( options ) {
options = options || {};
var keys = options.keys || sjcl.ecc.ecdsa.generateKeys( 192 ),
hash = sjcl.hash.sha256.hash( options.message );
return keys.sec.sign( hash );
};
// Method to verify digital signatures
/**
Verify signed messages with ECDSA
@status Experimental
@param {Object} options - includes keys (provided keys to sign the message),
message (message to be verified), signature (Digital signature)
@return {bitArray} - Signature
@example
//Message validation
var options = {
keys: sjcl.ecc.ecdsa.generateKeys(192),
signature: signatureToBeVerified
};
AeroGear.crypto.verify( options );
*/
this.verify = function ( options ) {
options = options || {};
var message = sjcl.hash.sha256.hash( options.message );
return options.keys.pub.verify( message, options.signature );
};
this.KeyPair = function( prKey, pubKey ) {
var keys, pub;
if ( prKey && pubKey ) {
privateKey = prKey;
publicKey = pubKey;
} else {
keys = sjcl.ecc.elGamal.generateKeys( 192,0 );
//kem - key encapsulation mechanism
pub = keys.pub.kem();
publicKey = pub.key;
privateKey = keys.sec.unkem( pub.tag );
}
return this;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment