Skip to content

Commit

Permalink
Merge branch 'release/3.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
evanvosberg committed Feb 10, 2020
2 parents 0596fae + 77d1bdd commit 6a6d99a
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 123 deletions.
30 changes: 21 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# crypto-js
# crypto-js [![Build Status](https://travis-ci.org/brix/crypto-js.svg?branch=develop)](https://travis-ci.org/brix/crypto-js)

JavaScript library of crypto standards.

Expand All @@ -15,6 +15,18 @@ npm install crypto-js

### Usage

ES6 import for typical API call signing use case:

```javascript
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:

```javascript
Expand Down Expand Up @@ -89,7 +101,7 @@ require(["crypto-js"], function (CryptoJS) {

## API

See: https://code.google.com/p/crypto-js
See: https://cryptojs.gitbook.io/docs/

### AES Encryption

Expand All @@ -99,13 +111,13 @@ See: https://code.google.com/p/crypto-js
var CryptoJS = require("crypto-js");

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

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

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

#### Object encryption
Expand All @@ -116,13 +128,13 @@ var CryptoJS = require("crypto-js");
var data = [{id: 1}, {id: 2}]

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

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

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

### List of modules
Expand Down
4 changes: 3 additions & 1 deletion aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
*/
var AES = C_algo.AES = BlockCipher.extend({
_doReset: function () {
var t;

// Skip reset of nRounds has been set before and key did not change
if (this._nRounds && this._keyPriorReset === this._key) {
return;
Expand All @@ -113,7 +115,7 @@
if (ksRow < keySize) {
keySchedule[ksRow] = keyWords[ksRow];
} else {
var t = keySchedule[ksRow - 1];
t = keySchedule[ksRow - 1];

if (!(ksRow % keySize)) {
// Rot word
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crypto-js",
"version": "3.1.9",
"version": "3.2.0",
"description": "JavaScript library of crypto standards.",
"license": "MIT",
"homepage": "http://github.com/brix/crypto-js",
Expand Down
28 changes: 19 additions & 9 deletions cipher-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,17 +351,19 @@
});

function xorBlock(words, offset, blockSize) {
var block;

// Shortcut
var iv = this._iv;

// Choose mixing block
if (iv) {
var block = iv;
block = iv;

// Remove IV for subsequent blocks
this._iv = undefined;
} else {
var block = this._prevBlock;
block = this._prevBlock;
}

// XOR blocks
Expand Down Expand Up @@ -453,6 +455,8 @@
}),

reset: function () {
var modeCreator;

// Reset cipher
Cipher.reset.call(this);

Expand All @@ -463,9 +467,9 @@

// Reset block mode
if (this._xformMode == this._ENC_XFORM_MODE) {
var modeCreator = mode.createEncryptor;
modeCreator = mode.createEncryptor;
} else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
var modeCreator = mode.createDecryptor;
modeCreator = mode.createDecryptor;
// Keep at least one block in the buffer for unpadding
this._minBufferSize = 1;
}
Expand All @@ -483,6 +487,8 @@
},

_doFinalize: function () {
var finalProcessedBlocks;

// Shortcut
var padding = this.cfg.padding;

Expand All @@ -492,10 +498,10 @@
padding.pad(this._data, this.blockSize);

// Process final blocks
var finalProcessedBlocks = this._process(!!'flush');
finalProcessedBlocks = this._process(!!'flush');
} else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
// Process final blocks
var finalProcessedBlocks = this._process(!!'flush');
finalProcessedBlocks = this._process(!!'flush');

// Unpad data
padding.unpad(finalProcessedBlocks);
Expand Down Expand Up @@ -587,15 +593,17 @@
* var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
*/
stringify: function (cipherParams) {
var wordArray;

// Shortcuts
var ciphertext = cipherParams.ciphertext;
var salt = cipherParams.salt;

// Format
if (salt) {
var wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
} else {
var wordArray = ciphertext;
wordArray = ciphertext;
}

return wordArray.toString(Base64);
Expand All @@ -615,6 +623,8 @@
* var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
*/
parse: function (openSSLStr) {
var salt;

// Parse base64
var ciphertext = Base64.parse(openSSLStr);

Expand All @@ -624,7 +634,7 @@
// Test for salt
if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
// Extract salt
var salt = WordArray.create(ciphertextWords.slice(2, 4));
salt = WordArray.create(ciphertextWords.slice(2, 4));

// Remove salt from ciphertext
ciphertextWords.splice(0, 4);
Expand Down
59 changes: 36 additions & 23 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,40 @@
* CryptoJS core components.
*/
var CryptoJS = CryptoJS || (function (Math, undefined) {

/*
* Local polyfil of Object.create
* Cryptographically secure pseudorandom number generator
*
* As Math.random() is cryptographically not safe to use
*/
var secureRandom = function () {
// Native crypto module on NodeJS environment
try {
// Crypto from global object
var crypto = global.crypto;

// Create a random float number between 0 and 1
return Number('0.' + crypto.randomBytes(3).readUIntBE(0, 3));
} catch (err) {}

// Native crypto module in Browser environment
try {
// Support experimental crypto module in IE 11
var crypto = window.crypto || window.msCrypto;

// Create a random float number between 0 and 1
return Number('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]);
} catch (err) {}

throw new Error('Native crypto module could not be used to get secure random number.');
};

/*
* Local polyfill of Object.create
*/
var create = Object.create || (function () {
function F() {};
function F() {}

return function (obj) {
var subtype;
Expand Down Expand Up @@ -304,26 +333,8 @@
random: function (nBytes) {
var words = [];

var r = (function (m_w) {
var m_w = m_w;
var m_z = 0x3ade68b1;
var mask = 0xffffffff;

return function () {
m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
var result = ((m_z << 0x10) + m_w) & mask;
result /= 0x100000000;
result += 0.5;
return result * (Math.random() > .5 ? 1 : -1);
}
});

for (var i = 0, rcache; i < nBytes; i += 4) {
var _r = r((rcache || Math.random()) * 0x100000000);

rcache = _r() * 0x3ade67b7;
words.push((_r() * 0x100000000) | 0);
for (var i = 0; i < nBytes; i += 4) {
words.push((secureRandom() * 0x100000000) | 0);
}

return new WordArray.init(words, nBytes);
Expand Down Expand Up @@ -554,6 +565,8 @@
* var processedData = bufferedBlockAlgorithm._process(!!'flush');
*/
_process: function (doFlush) {
var processedWords;

// Shortcuts
var data = this._data;
var dataWords = data.words;
Expand Down Expand Up @@ -586,7 +599,7 @@
}

// Remove processed words
var processedWords = dataWords.splice(0, nWordsReady);
processedWords = dataWords.splice(0, nWordsReady);
data.sigBytes -= nBytesReady;
}

Expand Down
Loading

0 comments on commit 6a6d99a

Please sign in to comment.