好斯坦福Javascript加密库(SJCL)的例子?(JS加密)

Fel*_*abe 25 javascript cryptography sjcl

我正在寻找一种在Javascript中进行客户端加密的方法(记住http://www.matasano.com/articles/javascript-cryptography/)并找到了SJCL.但我似乎找不到好的代码示例.有什么指针吗?

Kev*_*son 42

去年我做了一个题为" 开发人员JavaScript和Web密码学指南 "的演示文稿,并https://jswebcrypto.azurewebsites.net/上在线演示了该演示站点.

这包括OpenSSL命令行(作为基线)SJCL, CryptoJS,Node.js Crypto甚至W3C Web Cryptography API的简单Hash,HMAC,PBKDF2和AES示例

以下是SJCL示例:

哈希

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

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"
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

注意:这需要你在包括sha1.js除了sjcl.js.

  • @kevin-hakanson 在阅读您的回答时,我注意到,在`PBKDF2` 示例函数`hmacSHA1` 中什么都不做 - 有和没有它都返回相同的密钥 - http://jsfiddle.net/2802m8n5/。你能说出为什么会这样吗? (2认同)

jan*_*102 14

这可能有点晚了,但我最近也在研究如何进行客户端加密散列,而Kevin Hakanson答案非常有用,演示网站也很有用!它展示了如何使用自定义PseudoRandom函数与PBKDF2(HMAC和SHA1),但我发现,如果没有传入,SJCL有默认值,我只想展示如何做,以及生成随机盐.

我还发现sjcl文档非常有用.

要生成随机盐并在密码"password"上使用PBKDF2,您可以执行此操作,最终只需3行:

// Each random "word" is 4 bytes, so 8 would be 32 bytes
var saltBits = sjcl.random.randomWords(8);
// eg. [588300265, -1755622410, -533744668, 1408647727, -876578935, 12500664, 179736681, 1321878387]

// I left out the 5th argument, which defaults to HMAC which in turn defaults to use SHA256
var derivedKey = sjcl.misc.pbkdf2("password", saltBits, 1000, 256);
// eg. [-605875851, 757263041, -993332615, 465335420, 1306210159, -1270931768, -1185781663, -477369628]

// Storing the key is probably easier encoded and not as a bitArray
// I choose base64 just because the output is shorter, but you could use sjcl.codec.hex.fromBits
var key = sjcl.codec.base64.fromBits(derivedKey);
// eg. "2+MRdS0i6sHEyvJ5G7x0fE3bL2+0Px7IuVJoYeOL6uQ="
Run Code Online (Sandbox Code Playgroud)

如果你想存储盐,你可能想要编码它

var salt = sjcl.codec.base64.fromBits(saltBits);
// eg. "IxC/6ZdbU/bgL7PkU/ZCL8vAd4kAvr64CraQaU7KQ3M="
// Again I just used base64 because it's shorter, but you could use hex

// And to get the bitArray back, you would do the exact opposite
var saltBits = sjcl.codec.base64.toBits(salt);
Run Code Online (Sandbox Code Playgroud)