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

好的斯坦福Javascript加密库(SJCL)示例?(JS加密),javascript,cryptography,sjcl,Javascript,Cryptography,Sjcl,我正在寻找一种在Javascript中实现客户端加密的方法(请记住),并且已经找到了。但我似乎找不到好的代码示例。有什么建议吗?我去年做了一个名为的演示,演示网站在线 这包括OpenSSL命令行(作为基线)的简单哈希、HMAC、PBKDF2和AES示例,甚至 以下是SJCL示例: 散列 HMAC PBKDF2 var hmacSHA1 = function (key) { var hasher = new sjcl.misc.hmac( key, sjcl.hash.sha1 );

我正在寻找一种在Javascript中实现客户端加密的方法(请记住),并且已经找到了。但我似乎找不到好的代码示例。有什么建议吗?

我去年做了一个名为的演示,演示网站在线

这包括OpenSSL命令行(作为基线)的简单哈希、HMAC、PBKDF2和AES示例,甚至

以下是SJCL示例:

散列

HMAC

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

注意:这要求您在sjcl.js之外加入include。

这可能有点晚了,但我最近也在研究如何进行客户端加密哈希,这非常有用,演示站点也非常有用!它展示了如何在PBKDF2(HMAC和SHA1)中使用自定义伪随机函数,但我发现如果没有传入一个,SJCL就会有默认值,我只想展示如何做到这一点,并生成一个随机salt

我还发现这本书很有帮助

要生成随机salt并在密码“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="
如果你想储存盐,你可能需要对它进行编码

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);

那个有例子的网站真的很有用,谢谢!另外,如果您省略了hmacSHA1函数,而没有将其作为参数传入,则默认情况下它将使用hmac SHA256。@kevin hakanson在阅读您的答案时,我注意到,在
PBKDF2
示例函数
hmacSHA1
中,不执行任何操作-带或不带该函数都会返回相同的键-。您能告诉我为什么会发生这种情况吗?更新Fiddle()以包含sha1(不是sjcl.js的一部分)对于PBKDF2@KevinHakanson我试图实现您的PBKDF2示例,但是sha1函数需要访问sjcl函数。如何在它们之间共享名称空间?
// 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="
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);