crypto-js

repository·develop·Indexed 12 days ago

https://github.com/brix/crypto-js

A JavaScript library of cryptographic standards providing hashes, HMAC, AES, and more. Version 4.2.0. Note that this library is discontinued in favor of native Web Crypto and Node.js crypto modules.

Tokens
1.5K
Snippets
5
Records
6
Agent score
47%

What's inside crypto-js

  1. Use crypto-js in Node.js

    develop

    You can use crypto-js in Node.js using ES6 imports for specific modules (recommended for tree-shaking/smaller bundles) or CommonJS require for modular or full library access.

    ES6 Import (Specific Modules)

    Best for typical API call signing use cases where you only need specific algorithms.

    Modular Include (CommonJS)

    Import specific modules like aes or sha256 individually.

    Full Library Access (CommonJS)

    Include the entire library to access all available methods via the CryptoJS object.

    // ES6 import
    import sha256 from 'crypto-js/sha256';
    import hmacSHA512 from 'crypto-js/hmac-sha512';
    import Base64 from 'crypto-js/enc-base64';
    
    const message = 'hello';
    const nonce = '12345';
    const path = '/api';
    const privateKey = 'secret';
    const hashDigest = sha256(nonce + message);
    const hmacDigest = Base64.stringify(hmacSHA512(path + hashDigest, privateKey));
    
    // Modular CommonJS
    var AES = require("crypto-js/aes");
    var SHA256 = require("crypto-js/sha256");
    console.log(SHA256("Message"));
    
    // Full Library CommonJS
    var CryptoJS = require("crypto-js");
    console.log(CryptoJS.HmacSHA1("Message", "Key"));
  2. Use crypto-js in the Browser

    develop

    For frontend development, you can install via Bower or include the script directly in your HTML.

    Using Bower and RequireJS

    Install via Bower and configure require.config to point to the bower_components directory. You can load specific modules or the entire crypto-js package.

    Using without RequireJS

    Include the crypto-js.js file via a <script> tag. Once loaded, the CryptoJS global object is available for use.

    <!-- 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>
  3. Install crypto-js via npm

    develop

    To use crypto-js in a Node.js environment, install it using npm.

    Note: This library is discontinued. For modern Node.js and browser applications, it is recommended to use the native crypto module instead.

    npm install crypto-js
  4. Encrypt and Decrypt objects with AES

    develop

    To encrypt JavaScript objects, stringify them using JSON.stringify() before encryption. After decryption, convert the resulting bytes back to a UTF-8 string and use JSON.parse() to restore the object.

    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}]
  5. Encrypt and Decrypt plain text with AES

    develop

    Use CryptoJS.AES.encrypt to encrypt a string and CryptoJS.AES.decrypt to recover it. To get the original string back from the decrypted bytes, you must specify the encoding (e.g., CryptoJS.enc.Utf8).

    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'
  6. Reference: List of crypto-js modules

    develop

    The following modules are available for import within the crypto-js package structure:

    Core & Helpers

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

    Hashes

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

    HMAC

    • 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

    Key Derivation

    • crypto-js/pbkdf2
    • crypto-js/evpkdf

    Encryption Algorithms

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

    Encodings

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

    Modes & Padding

    • Modes: cfb, ctr, ctr-gladman, ofb, ecb
    • Padding: pkcs7, ansix923, iso10126, iso97971, zeropadding, nopadding
    • Formats: format-openssl, format-hex