From 1241fdb81948734ab613f5cd3c976747ba31c1ae Mon Sep 17 00:00:00 2001 From: Lubos Dolezel Date: Sun, 10 May 2020 22:15:56 +0200 Subject: [PATCH 01/26] Support self.crypto in Web Workers --- src/core.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core.js b/src/core.js index 8ba28ed..5f5d604 100644 --- a/src/core.js +++ b/src/core.js @@ -12,6 +12,11 @@ var CryptoJS = CryptoJS || (function (Math, undefined) { crypto = window.crypto; } + // Native crypto in web worker (Browser) + if (typeof self !== 'undefined' && self.crypto) { + crypto = self.crypto; + } + // Native (experimental IE 11) crypto from window (Browser) if (!crypto && typeof window !== 'undefined' && window.msCrypto) { crypto = window.msCrypto; From 71ad0bcd2cb015021cacdb0120c065e87b7669d2 Mon Sep 17 00:00:00 2001 From: paulmwatson Date: Mon, 11 May 2020 14:44:09 +0200 Subject: [PATCH 02/26] Minor typo fix: varialbes => variables --- src/md5.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/md5.js b/src/md5.js index 5189321..77ae69d 100644 --- a/src/md5.js +++ b/src/md5.js @@ -60,7 +60,7 @@ var M_offset_14 = M[offset + 14]; var M_offset_15 = M[offset + 15]; - // Working varialbes + // Working variables var a = H[0]; var b = H[1]; var c = H[2]; From 1adcb50e99a530b1457988ecabf2475f790c872a Mon Sep 17 00:00:00 2001 From: Frederic R Date: Sat, 20 Jun 2020 15:43:59 +0100 Subject: [PATCH 03/26] =?UTF-8?q?lint=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core.js b/src/core.js index 8ba28ed..0bdc81c 100644 --- a/src/core.js +++ b/src/core.js @@ -72,7 +72,7 @@ var CryptoJS = CryptoJS || (function (Math, undefined) { return subtype; }; - }()) + }()); /** * CryptoJS namespace. @@ -283,8 +283,8 @@ var CryptoJS = CryptoJS || (function (Math, undefined) { } } else { // Copy one word at a time - for (var i = 0; i < thatSigBytes; i += 4) { - thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2]; + for (var j = 0; j < thatSigBytes; j += 4) { + thisWords[(thisSigBytes + j) >>> 2] = thatWords[j >>> 2]; } } this.sigBytes += thatSigBytes; From 971c31f0c931f913d22a76ed488d9216ac04e306 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 19 Nov 2020 01:48:20 +0100 Subject: [PATCH 04/26] Add support for crypto from globalThis. --- src/core.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core.js b/src/core.js index a8a543e..e1330e7 100644 --- a/src/core.js +++ b/src/core.js @@ -17,6 +17,11 @@ var CryptoJS = CryptoJS || (function (Math, undefined) { crypto = self.crypto; } + // Native crypto from worker + if (typeof globalThis !== 'undefined' && globalThis.crypto) { + crypto = globalThis.crypto; + } + // Native (experimental IE 11) crypto from window (Browser) if (!crypto && typeof window !== 'undefined' && window.msCrypto) { crypto = window.msCrypto; From ae0696fe7c45eb3df7b841931dd18170959aa4b3 Mon Sep 17 00:00:00 2001 From: Spencer17x <1253478653@qq.com> Date: Fri, 18 Jun 2021 20:37:29 +0800 Subject: [PATCH 05/26] feat: add urlsafe --- grunt/config/modularize.js | 8 ++- package.json | 3 +- src/enc-base64url.js | 121 +++++++++++++++++++++++++++++++++++++ test/test1.html | 63 +++++++++++++++++++ 4 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 src/enc-base64url.js create mode 100644 test/test1.html diff --git a/grunt/config/modularize.js b/grunt/config/modularize.js index 2b4f2cc..58053a1 100644 --- a/grunt/config/modularize.js +++ b/grunt/config/modularize.js @@ -13,13 +13,13 @@ module.exports = { "index": { "global": "CryptoJS", "exports": "CryptoJS", - "components": ["core", "x64-core", "lib-typedarrays", "enc-utf16", "enc-base64", "md5", "sha1", "sha256", "sha224", "sha512", "sha384", "sha3", "ripemd160", "hmac", "pbkdf2", "evpkdf", "cipher-core", "mode-cfb", "mode-ctr", "mode-ctr-gladman", "mode-ofb", "mode-ecb", "pad-ansix923", "pad-iso10126", "pad-iso97971", "pad-zeropadding", "pad-nopadding", "format-hex", "aes", "tripledes", "rc4", "rabbit", "rabbit-legacy"] + "components": ["core", "x64-core", "lib-typedarrays", "enc-utf16", "enc-base64", "enc-base64url", "md5", "sha1", "sha256", "sha224", "sha512", "sha384", "sha3", "ripemd160", "hmac", "pbkdf2", "evpkdf", "cipher-core", "mode-cfb", "mode-ctr", "mode-ctr-gladman", "mode-ofb", "mode-ecb", "pad-ansix923", "pad-iso10126", "pad-iso97971", "pad-zeropadding", "pad-nopadding", "format-hex", "aes", "tripledes", "rc4", "rabbit", "rabbit-legacy"] }, "crypto-js": { "pack": true, "global": "CryptoJS", "exports": "CryptoJS", - "components": ["core", "x64-core", "lib-typedarrays", "enc-utf16", "enc-base64", "md5", "sha1", "sha256", "sha224", "sha512", "sha384", "sha3", "ripemd160", "hmac", "pbkdf2", "evpkdf", "cipher-core", "mode-cfb", "mode-ctr", "mode-ctr-gladman", "mode-ofb", "mode-ecb", "pad-ansix923", "pad-iso10126", "pad-iso97971", "pad-zeropadding", "pad-nopadding", "format-hex", "aes", "tripledes", "rc4", "rabbit", "rabbit-legacy"] + "components": ["core", "x64-core", "lib-typedarrays", "enc-utf16", "enc-base64", "enc-base64url", "md5", "sha1", "sha256", "sha224", "sha512", "sha384", "sha3", "ripemd160", "hmac", "pbkdf2", "evpkdf", "cipher-core", "mode-cfb", "mode-ctr", "mode-ctr-gladman", "mode-ofb", "mode-ecb", "pad-ansix923", "pad-iso10126", "pad-iso97971", "pad-zeropadding", "pad-nopadding", "format-hex", "aes", "tripledes", "rc4", "rabbit", "rabbit-legacy"] }, // hash @@ -174,6 +174,10 @@ module.exports = { "exports": "CryptoJS.enc.Base64", "components": ["core", "enc-base64"] }, + "enc-base64url": { + "exports": "CryptoJS.enc.Base64url", + "components": ["core", "enc-base64url"] + }, // mode "mode-cfb": { diff --git a/package.json b/package.json index b7686de..862a6a0 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "CFB", "CTR", "CBC", - "Base64" + "Base64", + "Base64url" ] } diff --git a/src/enc-base64url.js b/src/enc-base64url.js new file mode 100644 index 0000000..661ea39 --- /dev/null +++ b/src/enc-base64url.js @@ -0,0 +1,121 @@ +(function () { + // Shortcuts + var C = CryptoJS; + var C_lib = C.lib; + var WordArray = C_lib.WordArray; + var C_enc = C.enc; + + /** + * Base64url encoding strategy. + */ + var Base64url = C_enc.Base64url = { + /** + * Converts a word array to a Base64url string. + * + * @param {WordArray} wordArray The word array. + * + * @param {boolean} urlSafe Whether to use url safe + * + * @return {string} The Base64url string. + * + * @static + * + * @example + * + * var base64String = CryptoJS.enc.Base64url.stringify(wordArray); + */ + stringify: function (wordArray, urlSafe=true) { + // Shortcuts + var words = wordArray.words; + var sigBytes = wordArray.sigBytes; + var map = urlSafe ? this._safe_map : this._map; + + // Clamp excess bits + wordArray.clamp(); + + // Convert + var base64Chars = []; + for (var i = 0; i < sigBytes; i += 3) { + var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff; + var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff; + var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff; + + var triplet = (byte1 << 16) | (byte2 << 8) | byte3; + + for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) { + base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f)); + } + } + + // Add padding + var paddingChar = map.charAt(64); + if (paddingChar) { + while (base64Chars.length % 4) { + base64Chars.push(paddingChar); + } + } + + return base64Chars.join(''); + }, + + /** + * Converts a Base64url string to a word array. + * + * @param {string} base64Str The Base64url string. + * + * @param {boolean} urlSafe Whether to use url safe + * + * @return {WordArray} The word array. + * + * @static + * + * @example + * + * var wordArray = CryptoJS.enc.Base64url.parse(base64String); + */ + parse: function (base64Str, urlSafe=true) { + // Shortcuts + var base64StrLength = base64Str.length; + var map = urlSafe ? this._safe_map : this._map; + var reverseMap = this._reverseMap; + + if (!reverseMap) { + reverseMap = this._reverseMap = []; + for (var j = 0; j < map.length; j++) { + reverseMap[map.charCodeAt(j)] = j; + } + } + + // Ignore padding + var paddingChar = map.charAt(64); + if (paddingChar) { + var paddingIndex = base64Str.indexOf(paddingChar); + if (paddingIndex !== -1) { + base64StrLength = paddingIndex; + } + } + + // Convert + return parseLoop(base64Str, base64StrLength, reverseMap); + + }, + + _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=', + _safe_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_', + }; + + function parseLoop(base64Str, base64StrLength, reverseMap) { + var words = []; + var nBytes = 0; + for (var i = 0; i < base64StrLength; i++) { + if (i % 4) { + var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2); + var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2); + var bitsCombined = bits1 | bits2; + words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8); + nBytes++; + } + } + return WordArray.create(words, nBytes); + } +}()); \ No newline at end of file diff --git a/test/test1.html b/test/test1.html new file mode 100644 index 0000000..731b877 --- /dev/null +++ b/test/test1.html @@ -0,0 +1,63 @@ + + + + + CryptoJS Test Suite + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 058b8e7e59320ed3421ef3e36616670e6538a5fb Mon Sep 17 00:00:00 2001 From: hkjpotato Date: Tue, 6 Jul 2021 04:28:57 -0400 Subject: [PATCH 06/26] add browser field to avoid shimming crypto-browserify --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index b7686de..311da4f 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,9 @@ "test": "grunt default" }, "main": "index.js", + "browser": { + "crypto": false + }, "dependencies": {}, "devDependencies": { "fmd": "~0.0.3", From 37275c77270ff49af02c8939f67ed5339da83219 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 11:40:30 +0300 Subject: [PATCH 07/26] Bump release version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index ef873bb..014d83a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "crypto-js", "title": "crypto-js", "description": "JavaScript library of crypto standards.", - "version": "4.0.0", + "version": "4.1.0", "homepage": "http://github.com/brix/crypto-js", "author": { "name": "Evan Vosberg", From 495890cde84c3c492837e4f3775760f84cc591b4 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 11:44:54 +0300 Subject: [PATCH 08/26] Update release notes. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 50c4d57..6f55b03 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,12 @@ console.log(decryptedData); // [{id: 1}, {id: 2}] ## Release notes +### 4.1.0 + +Added url safe variant of base64 encoding. [357](https://github.com/brix/crypto-js/pull/357) + +Avoid webpack to add crypto-browser package. [364](https://github.com/brix/crypto-js/pull/364) + ### 4.0.0 This is an update including breaking changes for some environments. From 56ebdb99b6ddf5a6fe0c7bedaabac0d2f3c76ba3 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 14:39:48 +0300 Subject: [PATCH 09/26] Include browser field in release package.json. --- grunt/config/update_json.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/grunt/config/update_json.js b/grunt/config/update_json.js index 46a5656..1b92a78 100644 --- a/grunt/config/update_json.js +++ b/grunt/config/update_json.js @@ -19,7 +19,8 @@ module.exports = { 'repository': null, 'keywords': null, 'main': null, - 'dependencies': null + 'dependencies': null, + 'browser': null } }, bower: { @@ -36,6 +37,7 @@ module.exports = { 'keywords': null, 'main': null, 'dependencies': null, + 'browser': null, 'ignore': [] } } From a30519df4bfb6e0b880a3a34436f0526ec5adb87 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 14:50:27 +0300 Subject: [PATCH 10/26] Fix order of release modules. --- grunt/tasks/modularize.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/grunt/tasks/modularize.js b/grunt/tasks/modularize.js index 0fc3b46..228e9ae 100644 --- a/grunt/tasks/modularize.js +++ b/grunt/tasks/modularize.js @@ -55,7 +55,15 @@ module.exports = function (grunt) { .uniq() .without(name) .sort((a, b) => { - return options[a].components.indexOf(b) === -1 ? -1 : 1; + if (options[a].components.includes(b)) { + return 1 + } + + if (options[b].components.includes(a)) { + return -1 + } + + return 0; }) .value(); From 0326a863284ce58421ac809b230710329eae9574 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 14:51:34 +0300 Subject: [PATCH 11/26] Bump version. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 014d83a..6726ec7 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "crypto-js", "title": "crypto-js", "description": "JavaScript library of crypto standards.", - "version": "4.1.0", + "version": "4.1.1", "homepage": "http://github.com/brix/crypto-js", "author": { "name": "Evan Vosberg", From 81ed5629ddfd3c5ec6689921060bc91eb4d51c91 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 14:52:29 +0300 Subject: [PATCH 12/26] Update release notes. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 6f55b03..23795aa 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,12 @@ console.log(decryptedData); // [{id: 1}, {id: 2}] ## Release notes +### 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](https://github.com/brix/crypto-js/pull/357) From ecfe2e45f5237f1c27ce614c0a1ea442faa257b6 Mon Sep 17 00:00:00 2001 From: evanvosberg Date: Thu, 22 Jul 2021 15:01:11 +0300 Subject: [PATCH 13/26] Update dev dependencies. --- package.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 6726ec7..0bddb88 100644 --- a/package.json +++ b/package.json @@ -27,15 +27,15 @@ "dependencies": {}, "devDependencies": { "fmd": "~0.0.3", - "grunt": "^0.4.5", + "grunt": "^1.3.2", "grunt-cli": "^1.3.2", - "grunt-contrib-clean": "^0.6.0", - "grunt-contrib-copy": "^0.6.0", - "grunt-contrib-jshint": "^2.1.0", - "grunt-jsonlint": "^1.0.4", - "grunt-update-json": "^0.2.0", - "load-grunt-config": "^0.16.0", - "lodash": "^4.17.11" + "grunt-contrib-clean": "^2.0.0", + "grunt-contrib-copy": "^1.0.0", + "grunt-contrib-jshint": "^3.0.0", + "grunt-jsonlint": "^2.1.3", + "grunt-update-json": "^0.2.2", + "load-grunt-config": "^4.0.0", + "lodash": "^4.17.21" }, "keywords": [ "security", From dcc3848f5de5208bca73f36c0ed77fa192ee2ea8 Mon Sep 17 00:00:00 2001 From: Alanscut Date: Mon, 30 Aug 2021 14:28:30 +0800 Subject: [PATCH 14/26] fix:The "cfg.salt" parameter don't work --- src/cipher-core.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cipher-core.js b/src/cipher-core.js index 0fe6136..5086638 100644 --- a/src/cipher-core.js +++ b/src/cipher-core.js @@ -819,7 +819,7 @@ CryptoJS.lib.Cipher || (function (undefined) { cfg = this.cfg.extend(cfg); // Derive key and other params - var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize); + var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, cfg.salt); // Add IV to config cfg.iv = derivedParams.iv; From ca7384f8dc3d25437703b7a1899b9928e6379adc Mon Sep 17 00:00:00 2001 From: Alanscut Date: Tue, 31 Aug 2021 11:37:01 +0800 Subject: [PATCH 15/26] test: add test case,using salt in the config --- test/config-test.js | 26 ++++++++++++++++++++++++++ test/test.html | 1 + 2 files changed, 27 insertions(+) create mode 100644 test/config-test.js diff --git a/test/config-test.js b/test/config-test.js new file mode 100644 index 0000000..090e9fd --- /dev/null +++ b/test/config-test.js @@ -0,0 +1,26 @@ +YUI.add('config-test', function (Y) { + var C = CryptoJS; + + Y.Test.Runner.add(new Y.Test.Case({ + name: 'Config', + + setUp: function () { + this.data = { + saltA: CryptoJS.enc.Hex.parse('AA00000000000000'), + saltB: CryptoJS.enc.Hex.parse('BB00000000000000') + }; + }, + + testEncrypt: function () { + Y.Assert.areEqual(C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString(), C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString()); + Y.Assert.areNotEqual(C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString(), C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB }).toString()); + }, + + testDecrypt: function () { + var encryptedA = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }); + var encryptedB = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB }); + Y.Assert.areEqual('Test', C.AES.decrypt(encryptedA, 'Pass').toString(C.enc.Utf8)); + Y.Assert.areEqual('Test', C.AES.decrypt(encryptedB, 'Pass').toString(C.enc.Utf8)); + } + })); +}, '$Rev$'); \ No newline at end of file diff --git a/test/test.html b/test/test.html index d469486..6ab23c2 100644 --- a/test/test.html +++ b/test/test.html @@ -88,6 +88,7 @@ + + @@ -89,6 +90,7 @@ +