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

2024-04-18

我正在寻找一种在 Javascript 中进行客户端加密的方法(保持http://www.matasano.com/articles/javascript-cryptography/ http://www.matasano.com/articles/javascript-cryptography/记住)并发现SJCL http://bitwiseshiftleft.github.io/sjcl/。但我似乎无法找到好的代码示例。有什么指点吗?


我去年做了一个演讲,标题为JavaScript 和 Web 加密技术开发人员指南 http://www.slideshare.net/kevinhakanson/developers-guide-to-javascript-and-web-cryptography并在线提供演示网站:https://jswebcrypto.azurewebsites.net/ https://jswebcrypto.azurewebsites.net/

这包括 OpenSSL 命令行的简单哈希、HMAC、PBKDF2 和 AES 示例(作为基线)SJCL http://bitwiseshiftleft.github.io/sjcl/, CryptoJS https://code.google.com/p/crypto-js/, Node.js 加密货币 http://nodejs.org/api/crypto.html, 乃至W3C Web 加密 API http://www.w3.org/TR/WebCryptoAPI/

以下是 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

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:这需要你包含sha1.js https://github.com/bitwiseshiftleft/sjcl/blob/master/core/sha1.js除了 sjcl.js 之外。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

好的斯坦福 Javascript 加密库 (SJCL) 示例? (JS密码学)[关闭] 的相关文章

随机推荐