Created
July 4, 2014 17:28
-
-
Save Evgenus/26814357c46977993e73 to your computer and use it in GitHub Desktop.
sjcl cypher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sjcl = require("../bower_components/sjcl/sjcl.js") | |
decrypt = (ciphertext, password, p) -> | |
salt = sjcl.codec.hex.toBits(p.salt) | |
iv = sjcl.codec.hex.toBits(p.iv) | |
ct = sjcl.codec.bytes.toBits(ciphertext) | |
key = sjcl.misc.pbkdf2(password, salt, p.iter).slice(0, p.ks / 32) | |
prp = new sjcl.cipher[p.cipher](key) | |
text = sjcl.mode[p.mode].decrypt(prp, ct, iv, p.auth, p.ts) | |
return sjcl.codec.utf8String.fromBits(text) | |
encrypt = (message, password, p) -> | |
iv = sjcl.random.randomWords(4, 0) | |
p.iv = sjcl.codec.hex.fromBits(iv) | |
salt = sjcl.random.randomWords(2, 0) | |
p.salt = sjcl.codec.hex.fromBits(salt) | |
key = sjcl.misc.pbkdf2(password, salt, p.iter).slice(0, p.ks / 32) | |
msg = sjcl.codec.utf8String.toBits(message) | |
auth = sjcl.codec.utf8String.toBits(p.auth) | |
prp = new sjcl.cipher[p.cipher](key) | |
ct = sjcl.mode[p.mode].encrypt(prp, msg, iv, auth, p.ts); | |
return sjcl.codec.bytes.fromBits(ct) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment