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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16752924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:51:14  来源:igfitidea点击:

Good Stanford Javascript Crypto Library (SJCL) examples? (JS cryptography)

javascriptcryptographysjcl

提问by Felix Rabe

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?

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

回答by Kevin Hakanson

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

去年我做了一个题为JavaScript 和 Web 密码学开发人员指南的演示文稿,并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

这包括 OpenSSL 命令行(作为基线)SJCLCryptoJSNode.js Crypto甚至W3C Web Cryptography API 的简单哈希、HMAC、PBKDF2 和 AES 示例

Here are the SJCL examples:

以下是 SJCL 示例:

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

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

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.jsin addition to sjcl.js.

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

回答by janka102

This might be a bit late, but I too have recently been looking into how to do client-side cryptographic hashing, and the answer by Kevin Hakansonwas very helpful, the demo site is very useful too! It shows how to use a custom PseudoRandom Function with PBKDF2 (the HMAC and SHA1), but I figured out that if one is not passed in, SJCL has defaults and I just wanted to show how to do that, along with generating a random salt.

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

I also found the sjcl docsquite helpful.

我还发现sjcl 文档非常有帮助。

To generate a random salt and use PBKDF2 on the password "password", you could do this, which ends up being just 3 lines:

要生成随机盐并在密码“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="

If you wanted to store the salt, you probably want to encode it

如果你想存储盐,你可能想对它进行编码

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