crypto-js vs libsodium vs sjcl vs tweetnacl vs tweetnacl-ts
加密库
crypto-jslibsodiumsjcltweetnacltweetnacl-ts类似的npm包:

加密库

这些库提供了用于数据加密和解密的功能,旨在保护数据的机密性和完整性。它们各自实现了不同的加密算法和技术,适用于各种安全需求。选择合适的库可以帮助开发者在保护用户数据的同时,确保应用的性能和可维护性。

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
crypto-js016,409487 kB2793 年前MIT
libsodium01,1551.23 MB05 个月前ISC
sjcl07,1962.24 MB1186 个月前(BSD-2-Clause OR GPL-2.0-only)
tweetnacl01,923-77 年前Unlicense
tweetnacl-ts013-28 年前UNLICENSED

功能对比: crypto-js vs libsodium vs sjcl vs tweetnacl vs tweetnacl-ts

算法支持

  • crypto-js:

    支持多种加密算法,包括 AES、DES、Triple DES、SHA1、SHA256、MD5 等,适合多种加密需求。

  • libsodium:

    提供现代加密算法,如公钥加密、对称加密、哈希和数字签名,确保高安全性和性能。

  • sjcl:

    实现了多种加密算法,包括 AES、SHA256、PBKDF2 等,适合浏览器环境下的加密需求。

  • tweetnacl:

    专注于提供高效的公钥加密、签名和密钥交换,算法简单且安全。

  • tweetnacl-ts:

    与 tweetnacl 相同,提供相同的算法支持,但增加了 TypeScript 的类型定义。

性能

  • crypto-js:

    在性能上表现良好,但在处理大数据时可能会受到影响,适合中小型数据的加密。

  • libsodium:

    经过优化,能够在大多数情况下提供高性能,适合需要高效加密的应用。

  • sjcl:

    性能较好,但在某些情况下可能会受到 JavaScript 的执行限制,适合中等规模的数据处理。

  • tweetnacl:

    设计上非常轻量,性能极佳,适合需要快速加密和解密的场景。

  • tweetnacl-ts:

    与 tweetnacl 相同,性能优秀,适合 TypeScript 项目。

易用性

  • crypto-js:

    API 简单易用,适合初学者和快速开发,但可能缺乏一些高级功能。

  • libsodium:

    提供简单的 API 设计,易于使用,适合希望快速实现加密功能的开发者。

  • sjcl:

    虽然功能强大,但 API 可能需要一定学习曲线,适合有经验的开发者。

  • tweetnacl:

    API 设计简洁,易于理解和使用,适合需要快速实现加密的开发者。

  • tweetnacl-ts:

    提供类型安全的 API,易于在 TypeScript 项目中使用,适合 TypeScript 开发者。

安全性

  • crypto-js:

    虽然支持多种算法,但某些算法(如 MD5 和 SHA1)在安全性上已被认为不够强大,需谨慎使用。

  • libsodium:

    被广泛认为是现代加密库中的安全标杆,设计上避免了常见的安全陷阱。

  • sjcl:

    提供较好的安全性,但使用时需注意算法的选择和实现。

  • tweetnacl:

    专注于安全性,经过严格审计,适合对安全性要求极高的应用。

  • tweetnacl-ts:

    与 tweetnacl 相同,提供相同的安全性保障,适合 TypeScript 项目。

社区支持

  • crypto-js:

    有较大的社区支持,文档丰富,适合初学者和开发者。

  • libsodium:

    社区活跃,提供良好的文档和示例,适合希望深入了解加密的开发者。

  • sjcl:

    社区相对较小,但仍有一定的支持,适合特定需求的开发者。

  • tweetnacl:

    社区支持较好,文档简单明了,适合快速上手。

  • tweetnacl-ts:

    社区支持与 tweetnacl 相同,适合 TypeScript 开发者。

如何选择: crypto-js vs libsodium vs sjcl vs tweetnacl vs tweetnacl-ts

  • crypto-js:

    选择 crypto-js 如果你需要一个轻量级的库,支持多种常见的加密算法,如 AES、SHA1、MD5 等,并且希望在浏览器和 Node.js 中都能使用。

  • libsodium:

    选择 libsodium 如果你需要一个现代且安全的库,提供高效的加密、解密、签名和密钥交换功能,且希望使用更简单的 API 来避免常见的安全陷阱。

  • sjcl:

    选择 sjcl 如果你需要一个纯 JavaScript 实现的库,支持多种加密算法,并且希望在浏览器中进行加密操作,适合对性能和安全性有较高要求的应用。

  • tweetnacl:

    选择 tweetnacl 如果你需要一个小而简单的库,专注于提供高效的公钥加密和签名功能,适合对安全性要求极高的场景。

  • tweetnacl-ts:

    选择 tweetnacl-ts 如果你希望在 TypeScript 环境中使用 tweetnacl,并且需要类型安全的 API,以便更好地集成到 TypeScript 项目中。

crypto-js的README

crypto-js

JavaScript library of crypto standards.

Discontinued

Active development of CryptoJS has been discontinued. This library is no longer maintained.

Nowadays, NodeJS and modern browsers have a native Crypto module. The latest version of CryptoJS already uses the native Crypto module for random number generation, since Math.random() is not crypto-safe. Further development of CryptoJS would result in it only being a wrapper of native Crypto. Therefore, development and maintenance has been discontinued, it is time to go for the native crypto module.

Node.js (Install)

Requirements:

  • Node.js
  • npm (Node.js package manager)
npm install crypto-js

Usage

ES6 import for typical API call signing use case:

import sha256 from 'crypto-js/sha256';
import hmacSHA512 from 'crypto-js/hmac-sha512';
import Base64 from 'crypto-js/enc-base64';

const message, nonce, path, privateKey; // ...
const hashDigest = sha256(nonce + message);
const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));

Modular include:

var AES = require("crypto-js/aes");
var SHA256 = require("crypto-js/sha256");
...
console.log(SHA256("Message"));

Including all libraries, for access to extra methods:

var CryptoJS = require("crypto-js");
console.log(CryptoJS.HmacSHA1("Message", "Key"));

Client (browser)

Requirements:

  • Node.js
  • Bower (package manager for frontend)
bower install crypto-js

Usage

Modular include:

require.config({
    packages: [
        {
            name: 'crypto-js',
            location: 'path-to/bower_components/crypto-js',
            main: 'index'
        }
    ]
});

require(["crypto-js/aes", "crypto-js/sha256"], function (AES, SHA256) {
    console.log(SHA256("Message"));
});

Including all libraries, for access to extra methods:

// Above-mentioned will work or use this simple form
require.config({
    paths: {
        'crypto-js': 'path-to/bower_components/crypto-js/crypto-js'
    }
});

require(["crypto-js"], function (CryptoJS) {
    console.log(CryptoJS.HmacSHA1("Message", "Key"));
});

Usage without RequireJS

<script type="text/javascript" src="path-to/bower_components/crypto-js/crypto-js.js"></script>
<script type="text/javascript">
    var encrypted = CryptoJS.AES(...);
    var encrypted = CryptoJS.SHA256(...);
</script>

API

See: https://cryptojs.gitbook.io/docs/

AES Encryption

Plain text encryption

var CryptoJS = require("crypto-js");

// Encrypt
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123').toString();

// Decrypt
var bytes  = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);

console.log(originalText); // 'my message'

Object encryption

var CryptoJS = require("crypto-js");

var data = [{id: 1}, {id: 2}]

// Encrypt
var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(data), 'secret key 123').toString();

// Decrypt
var bytes  = CryptoJS.AES.decrypt(ciphertext, 'secret key 123');
var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));

console.log(decryptedData); // [{id: 1}, {id: 2}]

List of modules

  • crypto-js/core
  • crypto-js/x64-core
  • crypto-js/lib-typedarrays

  • crypto-js/md5
  • crypto-js/sha1
  • crypto-js/sha256
  • crypto-js/sha224
  • crypto-js/sha512
  • crypto-js/sha384
  • crypto-js/sha3
  • crypto-js/ripemd160

  • crypto-js/hmac-md5
  • crypto-js/hmac-sha1
  • crypto-js/hmac-sha256
  • crypto-js/hmac-sha224
  • crypto-js/hmac-sha512
  • crypto-js/hmac-sha384
  • crypto-js/hmac-sha3
  • crypto-js/hmac-ripemd160

  • crypto-js/pbkdf2

  • crypto-js/aes
  • crypto-js/tripledes
  • crypto-js/rc4
  • crypto-js/rabbit
  • crypto-js/rabbit-legacy
  • crypto-js/evpkdf

  • crypto-js/format-openssl
  • crypto-js/format-hex

  • crypto-js/enc-latin1
  • crypto-js/enc-utf8
  • crypto-js/enc-hex
  • crypto-js/enc-utf16
  • crypto-js/enc-base64

  • crypto-js/mode-cfb
  • crypto-js/mode-ctr
  • crypto-js/mode-ctr-gladman
  • crypto-js/mode-ofb
  • crypto-js/mode-ecb

  • crypto-js/pad-pkcs7
  • crypto-js/pad-ansix923
  • crypto-js/pad-iso10126
  • crypto-js/pad-iso97971
  • crypto-js/pad-zeropadding
  • crypto-js/pad-nopadding

Release notes

4.2.0

Change default hash algorithm and iteration's for PBKDF2 to prevent weak security by using the default configuration.

Custom KDF Hasher

Blowfish support

4.1.1

Fix module order in bundled release.

Include the browser field in the released package.json.

4.1.0

Added url safe variant of base64 encoding. 357

Avoid webpack to add crypto-browser package. 364

4.0.0

This is an update including breaking changes for some environments.

In this version Math.random() has been replaced by the random methods of the native crypto module.

For this reason CryptoJS might not run in some JavaScript environments without native crypto module. Such as IE 10 or before or React Native.

3.3.0

Rollback, 3.3.0 is the same as 3.1.9-1.

The move of using native secure crypto module will be shifted to a new 4.x.x version. As it is a breaking change the impact is too big for a minor release.

3.2.1

The usage of the native crypto module has been fixed. The import and access of the native crypto module has been improved.

3.2.0

In this version Math.random() has been replaced by the random methods of the native crypto module.

For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before.

If it's absolute required to run CryptoJS in such an environment, stay with 3.1.x version. Encrypting and decrypting stays compatible. But keep in mind 3.1.x versions still use Math.random() which is cryptographically not secure, as it's not random enough.

This version came along with CRITICAL BUG.

DO NOT USE THIS VERSION! Please, go for a newer version!

3.1.x

The 3.1.x are based on the original CryptoJS, wrapped in CommonJS modules.