crypto-js README

The project's README from github.com/brix/crypto-js. As one markdown file: README.md.

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:

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:

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









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.

crypto-js (brix/crypto-js)

Searching Database…

16.4K ·GitHub
这是干什么的?Crypto-js 是一个 JavaScript 加密算法库,提供哈希、AES 等加密算法、PBKDF2 密钥派生,以及 Base64 之类的编码功能,可以在 Node.js 和浏览器里使用。
它是给 Node.js 用的还是给浏览器用的?都可以——它只是一个普通的库,不是服务。在 Node.js 里用 npm 安装即可;README 里说明前端可以用 Bower 作为包管理器,你既可以按需单独引入各个模块,也可以一次性引入全部内容。
我怎么把它装到我的项目里?在 Node.js 里用 npm 安装,然后用 ES6 的 import 引入你需要的模块。在浏览器里可以通过 Bower 按模块引入,也可以引入全部库以获得额外的方法,或者使用无需 RequireJS 就能直接运行的构建版本。
它能在老浏览器或者 React Native 这类环境里运行吗?4.x 版本需要系统原生的 crypto 模块来生成随机数,所以在没有这个模块的环境里可能跑不起来,比如 IE 10 及更早版本,或者 React Native。3.1.x 系列可以在这些环境运行,但它用的是 Math.random(),加密安全性不够。
CryptoJS 还在维护吗?不维护了。这个库已经停止积极开发,不再维护;作者建议改用 NodeJS 和现代浏览器内置的原生 Crypto 模块。
它包含哪些算法?API 部分涵盖了对纯文本和对象的 AES 加密。发布说明里还提到了 PBKDF2——它在 4.2.0 中修复了不安全的默认设置——以及同一版本新增的 Blowfish 支持和一个自定义 KDF 哈希器。
为什么停止开发了?因为 NodeJS 和现代浏览器现在都自带原生 Crypto 模块,而且较新的 CryptoJS 版本已经在用它生成随机数了。如果继续开发,CryptoJS 最终只会变成那个原生模块的一层包装。
有哪些版本应该避开?有——3.2.0 带有一个严重 bug,README 里明确写着 DO NOT USE THIS VERSION(不要使用此版本)。另外,3.1.x 仍然依赖 Math.random(),它的随机性达不到加密要求,不过它的加密/解密输出和新版本保持兼容。
Creator
brix
Size
10.8 MB
Stars
16.4K
Updated
CommitOct 24
Age
13y
Commits · 90 Days