Monday, June 3, 2024
187
rated 0 times [  194] [ 7]  / answers: 1 / hits: 19966  / 11 Years ago, sat, may 25, 2013, 12:00:00

I am looking at a way to do client-side cryptography in Javascript (keeping http://www.matasano.com/articles/javascript-cryptography/ in mind) and have found SJCL. But I seem unable to find good code examples for it. Any pointers?


More From » cryptography

 Answers
10

I did a presentation last year titled Developer's Guide to JavaScript and Web Cryptography and have the demo site online at https://jswebcrypto.azurewebsites.net/



This includes simple Hash, HMAC, PBKDF2 and AES examples for OpenSSL command line (as a baseline) SJCL, CryptoJS, Node.js Crypto, and even W3C Web Cryptography API



Here are the SJCL examples:



Hash



var out = sjcl.hash.sha1.hash(The quick brown fox jumps over the lazy dog);
var hash = sjcl.codec.hex.fromBits(out)
// 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12


HMAC



var key = sjcl.codec.utf8String.toBits(key);
var out = (new sjcl.misc.hmac(key, sjcl.hash.sha256)).mac(The quick brown fox jumps over the lazy dog);
var hmac = sjcl.codec.hex.fromBits(out)
// f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8


PBKDF2



var hmacSHA1 = function (key) {
var hasher = new sjcl.misc.hmac( key, sjcl.hash.sha1 );
this.encrypt = function () {
return hasher.encrypt.apply( hasher, arguments );
};
};

var passwordSalt = sjcl.codec.hex.toBits( cf7488cd1e48e84990f51b3f121e161318ba2098aa6c993ded1012c955d5a3e8 );
var derivedKey = sjcl.misc.pbkdf2( password, passwordSalt, 100, 256, hmacSHA1 );
var hexKey = sjcl.codec.hex.fromBits( derivedKey );
// c12b2e03a08f3f0d23f3c4429c248c275a728814053a093835e803bc8e695b4e


Note: This requires you in include sha1.js in addition to sjcl.js.


[#78027] Thursday, May 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaacvalentinn

Total Points: 325
Total Questions: 120
Total Answers: 131

Location: North Korea
Member since Tue, Jun 16, 2020
4 Years ago
isaacvalentinn questions
Mon, Jan 18, 21, 00:00, 3 Years ago
Mon, Nov 23, 20, 00:00, 4 Years ago
Wed, Sep 23, 20, 00:00, 4 Years ago
;