├── .gitignore
├── grunt
├── config
│ ├── clean.js
│ ├── jsonlint.js
│ ├── copy.js
│ ├── jshint.js
│ └── update_json.js
└── tasks
│ └── modularize.js
├── src
├── pad-nopadding.js
├── mode-ecb.js
├── pad-iso97971.js
├── pad-iso10126.js
├── pad-zeropadding.js
├── pad-ansix923.js
├── mode-ofb.js
├── mode-ctr.js
├── format-hex.js
├── sha224.js
├── mode-cfb.js
├── sha384.js
├── lib-typedarrays.js
├── mode-ctr-gladman.js
├── rc4.js
├── evpkdf.js
├── enc-base64.js
├── enc-utf16.js
├── hmac.js
├── sha1.js
├── pbkdf2.js
├── enc-base64url.js
└── sha256.js
├── .travis.yml
├── test
├── evpkdf-profile.js
├── pbkdf2-profile.js
├── enc-hex-test.js
├── enc-latin1-test.js
├── kdf-openssl-test.js
├── ripemd160-test.js
├── sha224-test.js
├── md5-profile.js
├── sha1-profile.js
├── sha3-profile.js
├── sha256-profile.js
├── sha512-profile.js
├── hmac-md5-profile.js
├── pad-pkcs7-test.js
├── lib-passwordbasedcipher-test.js
├── rc4-profile.js
├── pad-ansix923-test.js
├── rabbit-profile.js
├── pad-zeropadding-test.js
├── blowfish-test.js
├── des-profile.js
├── tripledes-profile.js
├── aes-profile.js
├── pad-iso97971-test.js
├── x64-wordarray-test.js
├── evpkdf-test.js
├── enc-utf8-test.js
├── mode-ecb-test.js
├── pad-iso10126-test.js
├── format-openssl-test.js
├── mode-cbc-test.js
├── mode-ofb-test.js
├── mode-cfb-test.js
├── enc-utf16-test.js
├── mode-ctr-test.js
├── lib-typedarrays-test.js
├── lib-serializablecipher-test.js
├── sha384-test.js
├── sha512-test.js
├── test1.html
├── config-test.js
├── lib-cipherparams-test.js
├── hmac-md5-test.js
├── md5-test.js
├── hmac-sha224-test.js
├── hmac-sha256-test.js
├── sha1-test.js
├── hmac-sha384-test.js
├── rc4-test.js
├── enc-base64-test.js
├── sha256-test.js
├── hmac-sha512-test.js
├── lib-base-test.js
├── lib-wordarray-test.js
├── x64-word-test.js
├── rabbit-legacy-test.js
├── test-build.html
├── rabbit-test.js
├── pbkdf2-test.js
├── aes-test.js
├── sha3-test.js
├── test.html
└── des-test.js
├── CONTRIBUTING.md
├── LICENSE
├── Gruntfile.js
├── .jshintrc
└── package.json
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | build
3 | .svn
--------------------------------------------------------------------------------
/grunt/config/clean.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 |
3 | 'use strict';
4 |
5 | module.exports = {
6 | build: [
7 | '<%= meta.build %>'
8 | ]
9 | };
10 |
--------------------------------------------------------------------------------
/src/pad-nopadding.js:
--------------------------------------------------------------------------------
1 | /**
2 | * A noop padding strategy.
3 | */
4 | CryptoJS.pad.NoPadding = {
5 | pad: function () {
6 | },
7 |
8 | unpad: function () {
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | dist: trusty
2 | sudo: false
3 |
4 | language: node_js
5 | node_js:
6 | - "6"
7 | - "7"
8 |
9 | before_script:
10 | - npm install -g grunt-cli
11 | - npm install build
12 |
13 | cache:
14 | directories:
15 | - "node_modules"
16 |
--------------------------------------------------------------------------------
/grunt/config/jsonlint.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 |
3 | 'use strict';
4 |
5 | module.exports = {
6 | all: {
7 | files: {
8 | src: [
9 | '<%= meta.cwdAll %>.json',
10 | '!<%= meta.buildAll %>',
11 | '!<%= meta.npmAll %>'
12 | ]
13 | }
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/test/evpkdf-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-evpkdf-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'EvpKDF',
6 |
7 | profileKeySize256Iterations20: function () {
8 | C.algo.EvpKDF.create({ keySize: 256/32, iterations: 20 }).compute('password', 'ATHENA.MIT.EDUraeburn');
9 | }
10 | });
11 | }, '$Rev$');
12 |
--------------------------------------------------------------------------------
/test/pbkdf2-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-pbkdf2-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'PBKDF2',
6 |
7 | profileKeySize256Iterations20: function () {
8 | C.algo.PBKDF2.create({ keySize: 256/32, iterations: 20 }).compute('password', 'ATHENA.MIT.EDUraeburn');
9 | }
10 | });
11 | }, '$Rev$');
12 |
--------------------------------------------------------------------------------
/grunt/config/copy.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 |
3 | 'use strict';
4 |
5 | module.exports = {
6 | build: {
7 | files: [{
8 | expand: false,
9 | cwd: '<%= meta.cwd %>',
10 | src: [
11 | 'README.md',
12 | 'CONTRIBUTING.md',
13 | 'LICENSE',
14 | 'docs/**/*'
15 | ],
16 | dest: '<%= meta.build %>'
17 | }]
18 | }
19 | };
20 |
--------------------------------------------------------------------------------
/grunt/config/jshint.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 |
3 | 'use strict';
4 |
5 | module.exports = {
6 | dev: {
7 | options: {
8 | jshintrc: process.cwd() + '/.jshintrc',
9 | reporterOutput: ''
10 | },
11 | files: {
12 | src: [
13 | '<%= meta.cwdAll %>.js',
14 | '!<%= meta.buildAll %>',
15 | '!<%= meta.testAll %>',
16 | '!<%= meta.npmAll %>'
17 | ]
18 | }
19 | }
20 | };
21 |
--------------------------------------------------------------------------------
/test/enc-hex-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('enc-hex-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Hex',
6 |
7 | testStringify: function () {
8 | Y.Assert.areEqual('12345678', C.enc.Hex.stringify(C.lib.WordArray.create([0x12345678])));
9 | },
10 |
11 | testParse: function () {
12 | Y.Assert.areEqual(C.lib.WordArray.create([0x12345678]).toString(), C.enc.Hex.parse('12345678').toString());
13 | }
14 | }));
15 | }, '$Rev$');
16 |
--------------------------------------------------------------------------------
/src/mode-ecb.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Electronic Codebook block mode.
3 | */
4 | CryptoJS.mode.ECB = (function () {
5 | var ECB = CryptoJS.lib.BlockCipherMode.extend();
6 |
7 | ECB.Encryptor = ECB.extend({
8 | processBlock: function (words, offset) {
9 | this._cipher.encryptBlock(words, offset);
10 | }
11 | });
12 |
13 | ECB.Decryptor = ECB.extend({
14 | processBlock: function (words, offset) {
15 | this._cipher.decryptBlock(words, offset);
16 | }
17 | });
18 |
19 | return ECB;
20 | }());
21 |
--------------------------------------------------------------------------------
/test/enc-latin1-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('enc-latin1-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Latin1',
6 |
7 | testStringify: function () {
8 | Y.Assert.areEqual('\x12\x34\x56\x78', C.enc.Latin1.stringify(C.lib.WordArray.create([0x12345678])));
9 | },
10 |
11 | testParse: function () {
12 | Y.Assert.areEqual(C.lib.WordArray.create([0x12345678]).toString(), C.enc.Latin1.parse('\x12\x34\x56\x78').toString());
13 | }
14 | }));
15 | }, '$Rev$');
16 |
--------------------------------------------------------------------------------
/src/pad-iso97971.js:
--------------------------------------------------------------------------------
1 | /**
2 | * ISO/IEC 9797-1 Padding Method 2.
3 | */
4 | CryptoJS.pad.Iso97971 = {
5 | pad: function (data, blockSize) {
6 | // Add 0x80 byte
7 | data.concat(CryptoJS.lib.WordArray.create([0x80000000], 1));
8 |
9 | // Zero pad the rest
10 | CryptoJS.pad.ZeroPadding.pad(data, blockSize);
11 | },
12 |
13 | unpad: function (data) {
14 | // Remove zero padding
15 | CryptoJS.pad.ZeroPadding.unpad(data);
16 |
17 | // Remove one more byte -- the 0x80 byte
18 | data.sigBytes--;
19 | }
20 | };
21 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contribution
2 |
3 | # Git Flow
4 |
5 | The crypto-js project uses [git flow](https://github.com/nvie/gitflow) to manage branches.
6 | Do your changes on the `develop` or even better on a `feature/*` branch. Don't do any changes on the `master` branch.
7 |
8 | # Pull request
9 |
10 | Target your pull request on `develop` branch. Other pull request won't be accepted.
11 |
12 | # How to build
13 |
14 | 1. Clone
15 |
16 | 2. Run
17 |
18 | ```sh
19 | npm install
20 | ```
21 |
22 | 3. Run
23 |
24 | ```sh
25 | npm run build
26 | ```
27 |
28 | 4. Check `build` folder
--------------------------------------------------------------------------------
/test/kdf-openssl-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('kdf-openssl-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'OpenSSLKdf',
6 |
7 | testVector: function () {
8 | var derivedParams = C.kdf.OpenSSL.execute('password', 256/32, 128/32, C.enc.Hex.parse('0a9d8620cf7219f1'));
9 |
10 | Y.Assert.areEqual('50f32e0ec9408e02ff42364a52aac95c3694fc027256c6f488bf84b8e60effcd', derivedParams.key.toString());
11 | Y.Assert.areEqual('81381e39b94fd692dff7e2239a298cb6', derivedParams.iv.toString());
12 | Y.Assert.areEqual('0a9d8620cf7219f1', derivedParams.salt.toString());
13 | }
14 | }));
15 | }, '$Rev$');
16 |
--------------------------------------------------------------------------------
/test/ripemd160-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-ripemd160-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'RIPEMD160',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('37f332f68db77bd9d7edd4969571ad671cf9dd3b', C.RIPEMD160('The quick brown fox jumps over the lazy dog').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('132072df690933835eb8b6ad0b77e7b6f14acad7', C.RIPEMD160('The quick brown fox jumps over the lazy cog').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('9c1185a5c5e9fc54612808977ee8f548b2258d31', C.RIPEMD160('').toString());
17 | }
18 | }));
19 | }, '$Rev$');
20 |
--------------------------------------------------------------------------------
/src/pad-iso10126.js:
--------------------------------------------------------------------------------
1 | /**
2 | * ISO 10126 padding strategy.
3 | */
4 | CryptoJS.pad.Iso10126 = {
5 | pad: function (data, blockSize) {
6 | // Shortcut
7 | var blockSizeBytes = blockSize * 4;
8 |
9 | // Count padding bytes
10 | var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
11 |
12 | // Pad
13 | data.concat(CryptoJS.lib.WordArray.random(nPaddingBytes - 1)).
14 | concat(CryptoJS.lib.WordArray.create([nPaddingBytes << 24], 1));
15 | },
16 |
17 | unpad: function (data) {
18 | // Get number of padding bytes from last byte
19 | var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
20 |
21 | // Remove padding
22 | data.sigBytes -= nPaddingBytes;
23 | }
24 | };
25 |
--------------------------------------------------------------------------------
/src/pad-zeropadding.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Zero padding strategy.
3 | */
4 | CryptoJS.pad.ZeroPadding = {
5 | pad: function (data, blockSize) {
6 | // Shortcut
7 | var blockSizeBytes = blockSize * 4;
8 |
9 | // Pad
10 | data.clamp();
11 | data.sigBytes += blockSizeBytes - ((data.sigBytes % blockSizeBytes) || blockSizeBytes);
12 | },
13 |
14 | unpad: function (data) {
15 | // Shortcut
16 | var dataWords = data.words;
17 |
18 | // Unpad
19 | var i = data.sigBytes - 1;
20 | for (var i = data.sigBytes - 1; i >= 0; i--) {
21 | if (((dataWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff)) {
22 | data.sigBytes = i + 1;
23 | break;
24 | }
25 | }
26 | }
27 | };
28 |
--------------------------------------------------------------------------------
/test/sha224-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha224-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SHA224',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f', C.SHA224('').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('730e109bd7a8a32b1cb9d9a09aa2325d2430587ddbc0c38bad911525', C.SHA224('The quick brown fox jumps over the lazy dog').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('619cba8e8e05826e9b8c519c0a5c68f4fb653e8a3d8aa04bb2c8cd4c', C.SHA224('The quick brown fox jumps over the lazy dog.').toString());
17 | }
18 | }));
19 | }, '$Rev$');
20 |
--------------------------------------------------------------------------------
/test/md5-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-md5-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'MD5',
6 |
7 | profileSinglePartMessage: function () {
8 | var singlePartMessage = '';
9 | for (var i = 0; i < 500; i++) {
10 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
11 | }
12 |
13 | C.algo.MD5.create().finalize(singlePartMessage) + '';
14 | },
15 |
16 | profileMultiPartMessage: function () {
17 | var md5 = C.algo.MD5.create();
18 | for (var i = 0; i < 500; i++) {
19 | md5.update('12345678901234567890123456789012345678901234567890');
20 | }
21 | md5.finalize() + '';
22 | }
23 | });
24 | }, '$Rev$');
25 |
--------------------------------------------------------------------------------
/test/sha1-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha1-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'SHA1',
6 |
7 | profileSinglePartMessage: function () {
8 | var singlePartMessage = '';
9 | for (var i = 0; i < 500; i++) {
10 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
11 | }
12 |
13 | C.algo.SHA1.create().finalize(singlePartMessage) + '';
14 | },
15 |
16 | profileMultiPartMessage: function () {
17 | var sha1 = C.algo.SHA1.create();
18 | for (var i = 0; i < 500; i++) {
19 | sha1.update('12345678901234567890123456789012345678901234567890');
20 | }
21 | sha1.finalize() + '';
22 | }
23 | });
24 | }, '$Rev$');
25 |
--------------------------------------------------------------------------------
/test/sha3-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha3-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'SHA3',
6 |
7 | profileSinglePartMessage: function () {
8 | var singlePartMessage = '';
9 | for (var i = 0; i < 500; i++) {
10 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
11 | }
12 |
13 | C.algo.SHA3.create().finalize(singlePartMessage) + '';
14 | },
15 |
16 | profileMultiPartMessage: function () {
17 | var sha3 = C.algo.SHA3.create();
18 | for (var i = 0; i < 500; i++) {
19 | sha3.update('12345678901234567890123456789012345678901234567890');
20 | }
21 | sha3.finalize() + '';
22 | }
23 | });
24 | }, '$Rev$');
25 |
--------------------------------------------------------------------------------
/test/sha256-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha256-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'SHA256',
6 |
7 | profileSinglePartMessage: function () {
8 | var singlePartMessage = '';
9 | for (var i = 0; i < 500; i++) {
10 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
11 | }
12 |
13 | C.algo.SHA256.create().finalize(singlePartMessage) + '';
14 | },
15 |
16 | profileMultiPartMessage: function () {
17 | var sha256 = C.algo.SHA256.create();
18 | for (var i = 0; i < 500; i++) {
19 | sha256.update('12345678901234567890123456789012345678901234567890');
20 | }
21 | sha256.finalize() + '';
22 | }
23 | });
24 | }, '$Rev$');
25 |
--------------------------------------------------------------------------------
/test/sha512-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha512-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'SHA512',
6 |
7 | profileSinglePartMessage: function () {
8 | var singlePartMessage = '';
9 | for (var i = 0; i < 500; i++) {
10 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
11 | }
12 |
13 | C.algo.SHA512.create().finalize(singlePartMessage) + '';
14 | },
15 |
16 | profileMultiPartMessage: function () {
17 | var sha512 = C.algo.SHA512.create();
18 | for (var i = 0; i < 500; i++) {
19 | sha512.update('12345678901234567890123456789012345678901234567890');
20 | }
21 | sha512.finalize() + '';
22 | }
23 | });
24 | }, '$Rev$');
25 |
--------------------------------------------------------------------------------
/src/pad-ansix923.js:
--------------------------------------------------------------------------------
1 | /**
2 | * ANSI X.923 padding strategy.
3 | */
4 | CryptoJS.pad.AnsiX923 = {
5 | pad: function (data, blockSize) {
6 | // Shortcuts
7 | var dataSigBytes = data.sigBytes;
8 | var blockSizeBytes = blockSize * 4;
9 |
10 | // Count padding bytes
11 | var nPaddingBytes = blockSizeBytes - dataSigBytes % blockSizeBytes;
12 |
13 | // Compute last byte position
14 | var lastBytePos = dataSigBytes + nPaddingBytes - 1;
15 |
16 | // Pad
17 | data.clamp();
18 | data.words[lastBytePos >>> 2] |= nPaddingBytes << (24 - (lastBytePos % 4) * 8);
19 | data.sigBytes += nPaddingBytes;
20 | },
21 |
22 | unpad: function (data) {
23 | // Get number of padding bytes from last byte
24 | var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
25 |
26 | // Remove padding
27 | data.sigBytes -= nPaddingBytes;
28 | }
29 | };
30 |
--------------------------------------------------------------------------------
/src/mode-ofb.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Output Feedback block mode.
3 | */
4 | CryptoJS.mode.OFB = (function () {
5 | var OFB = CryptoJS.lib.BlockCipherMode.extend();
6 |
7 | var Encryptor = OFB.Encryptor = OFB.extend({
8 | processBlock: function (words, offset) {
9 | // Shortcuts
10 | var cipher = this._cipher
11 | var blockSize = cipher.blockSize;
12 | var iv = this._iv;
13 | var keystream = this._keystream;
14 |
15 | // Generate keystream
16 | if (iv) {
17 | keystream = this._keystream = iv.slice(0);
18 |
19 | // Remove IV for subsequent blocks
20 | this._iv = undefined;
21 | }
22 | cipher.encryptBlock(keystream, 0);
23 |
24 | // Encrypt
25 | for (var i = 0; i < blockSize; i++) {
26 | words[offset + i] ^= keystream[i];
27 | }
28 | }
29 | });
30 |
31 | OFB.Decryptor = Encryptor;
32 |
33 | return OFB;
34 | }());
35 |
--------------------------------------------------------------------------------
/test/hmac-md5-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-hmac-md5-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'HMAC MD5',
6 |
7 | setUp: function () {
8 | this.data = {
9 | key: C.lib.WordArray.random(128/8)
10 | };
11 | },
12 |
13 | profileSinglePartMessage: function () {
14 | var singlePartMessage = '';
15 | for (var i = 0; i < 500; i++) {
16 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
17 | }
18 |
19 | C.algo.HMAC.create(C.algo.MD5, this.data.key).finalize(singlePartMessage) + '';
20 | },
21 |
22 | profileMultiPartMessage: function () {
23 | var hmac = C.algo.HMAC.create(C.algo.MD5, this.data.key);
24 | for (var i = 0; i < 500; i++) {
25 | hmac.update('12345678901234567890123456789012345678901234567890');
26 | }
27 | hmac.finalize() + '';
28 | }
29 | });
30 | }, '$Rev$');
31 |
--------------------------------------------------------------------------------
/test/pad-pkcs7-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('pad-pkcs7-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Pkcs7',
6 |
7 | testPad: function () {
8 | var data = C.lib.WordArray.create([0xdddddd00], 3);
9 | C.pad.Pkcs7.pad(data, 2);
10 |
11 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd05, 0x05050505]).toString(), data.toString());
12 | },
13 |
14 | testPadClamp: function () {
15 | var data = C.lib.WordArray.create([0xdddddddd, 0xdddddddd], 3);
16 | C.pad.Pkcs7.pad(data, 2);
17 |
18 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd05, 0x05050505]).toString(), data.toString());
19 | },
20 |
21 | testUnpad: function () {
22 | var data = C.lib.WordArray.create([0xdddddd05, 0x05050505]);
23 | C.pad.Pkcs7.unpad(data);
24 |
25 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00], 3).toString(), data.toString());
26 | }
27 | }));
28 | }, '$Rev$');
29 |
--------------------------------------------------------------------------------
/test/lib-passwordbasedcipher-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('lib-passwordbasedcipher-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'PasswordBasedCipher',
6 |
7 | testEncrypt: function () {
8 | // Compute actual
9 | var actual = C.lib.PasswordBasedCipher.encrypt(C.algo.AES, 'Hello, World!', 'password');
10 |
11 | // Compute expected
12 | var aes = C.algo.AES.createEncryptor(actual.key, { iv: actual.iv });
13 | var expected = aes.finalize('Hello, World!');
14 |
15 | Y.Assert.areEqual(expected.toString(), actual.ciphertext.toString());
16 | },
17 |
18 | testDecrypt: function () {
19 | var ciphertext = C.lib.PasswordBasedCipher.encrypt(C.algo.AES, 'Hello, World!', 'password');
20 | var plaintext = C.lib.PasswordBasedCipher.decrypt(C.algo.AES, ciphertext, 'password');
21 |
22 | Y.Assert.areEqual('Hello, World!', plaintext.toString(C.enc.Utf8));
23 | }
24 | }));
25 | }, '$Rev$');
26 |
--------------------------------------------------------------------------------
/test/rc4-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-rc4-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'RC4',
6 |
7 | setUp: function () {
8 | this.data = {
9 | key: C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f')
10 | };
11 | },
12 |
13 | profileSinglePartMessage: function () {
14 | var singlePartMessage = '';
15 | for (var i = 0; i < 500; i++) {
16 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
17 | }
18 |
19 | C.algo.RC4.createEncryptor(this.data.key).finalize(singlePartMessage) + '';
20 | },
21 |
22 | profileMultiPartMessage: function () {
23 | var rc4 = C.algo.RC4.createEncryptor(this.data.key);
24 | for (var i = 0; i < 500; i++) {
25 | rc4.process('12345678901234567890123456789012345678901234567890') + '';
26 | }
27 | rc4.finalize() + '';
28 | }
29 | });
30 | }, '$Rev$');
31 |
--------------------------------------------------------------------------------
/test/pad-ansix923-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('pad-ansix923-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'AnsiX923',
6 |
7 | testPad: function () {
8 | var data = C.lib.WordArray.create([0xdddddd00], 3);
9 | C.pad.AnsiX923.pad(data, 2);
10 |
11 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00, 0x00000005]).toString(), data.toString());
12 | },
13 |
14 | testPadClamp: function () {
15 | var data = C.lib.WordArray.create([0xdddddddd, 0xdddddddd], 3);
16 | C.pad.AnsiX923.pad(data, 2);
17 |
18 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00, 0x00000005]).toString(), data.toString());
19 | },
20 |
21 | testUnpad: function () {
22 | var data = C.lib.WordArray.create([0xdddddd00, 0x00000005]);
23 | C.pad.AnsiX923.unpad(data);
24 |
25 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00], 3).toString(), data.toString());
26 | }
27 | }));
28 | }, '$Rev$');
29 |
--------------------------------------------------------------------------------
/test/rabbit-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-rabbit-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'Rabbit',
6 |
7 | setUp: function () {
8 | this.data = {
9 | key: C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f')
10 | };
11 | },
12 |
13 | profileSinglePartMessage: function () {
14 | var singlePartMessage = '';
15 | for (var i = 0; i < 500; i++) {
16 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
17 | }
18 |
19 | C.algo.Rabbit.createEncryptor(this.data.key).finalize(singlePartMessage) + '';
20 | },
21 |
22 | profileMultiPartMessage: function () {
23 | var rabbit = C.algo.Rabbit.createEncryptor(this.data.key);
24 | for (var i = 0; i < 500; i++) {
25 | rabbit.process('12345678901234567890123456789012345678901234567890') + '';
26 | }
27 | rabbit.finalize() + '';
28 | }
29 | });
30 | }, '$Rev$');
31 |
--------------------------------------------------------------------------------
/test/pad-zeropadding-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('pad-zeropadding-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'ZeroPadding',
6 |
7 | testPad: function () {
8 | var data = C.lib.WordArray.create([0xdddddd00], 3);
9 | C.pad.ZeroPadding.pad(data, 2);
10 |
11 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00, 0x00000000]).toString(), data.toString());
12 | },
13 |
14 | testPadClamp: function () {
15 | var data = C.lib.WordArray.create([0xdddddddd, 0xdddddddd], 3);
16 | C.pad.ZeroPadding.pad(data, 2);
17 |
18 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00, 0x00000000]).toString(), data.toString());
19 | },
20 |
21 | testUnpad: function () {
22 | var data = C.lib.WordArray.create([0xdddddd00, 0x00000000]);
23 | C.pad.ZeroPadding.unpad(data);
24 |
25 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00], 3).toString(), data.toString());
26 | }
27 | }));
28 | }, '$Rev$');
29 |
--------------------------------------------------------------------------------
/test/blowfish-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-aes-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Blowfish',
6 |
7 | setUp: function () {
8 | this.data = {
9 | saltA: CryptoJS.enc.Hex.parse('AA00000000000000')
10 | };
11 | },
12 |
13 | testEncrypt: function () {
14 | let encryptedA = C.Blowfish.encrypt('Test',
15 | 'pass',
16 | {
17 | salt: this.data.saltA,
18 | hasher: CryptoJS.algo.SHA256
19 | }).toString();
20 | Y.Assert.areEqual('U2FsdGVkX1+qAAAAAAAAAKTIU8MPrBdH', encryptedA);
21 | },
22 |
23 | testDecrypt: function () {
24 | let encryptedA = C.Blowfish.encrypt('Test',
25 | 'pass',
26 | {
27 | salt: this.data.saltA,
28 | hasher: CryptoJS.algo.SHA256
29 | }).toString();
30 | Y.Assert.areEqual('Test', C.Blowfish.decrypt(encryptedA, 'pass', {hasher: CryptoJS.algo.SHA256}).toString(C.enc.Utf8));
31 | }
32 | }));
33 | }, '$Rev$');
34 |
--------------------------------------------------------------------------------
/test/des-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-des-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'DES',
6 |
7 | setUp: function () {
8 | this.data = {
9 | key: C.enc.Hex.parse('0001020304050607'),
10 | iv: C.enc.Hex.parse('08090a0b0c0d0e0f')
11 | };
12 | },
13 |
14 | profileSinglePartMessage: function () {
15 | var singlePartMessage = '';
16 | for (var i = 0; i < 100; i++) {
17 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
18 | }
19 |
20 | C.algo.DES.createEncryptor(this.data.key, { iv: this.data.iv }).finalize(singlePartMessage) + '';
21 | },
22 |
23 | profileMultiPartMessage: function () {
24 | var des = C.algo.DES.createEncryptor(this.data.key, { iv: this.data.iv });
25 | for (var i = 0; i < 100; i++) {
26 | des.process('12345678901234567890123456789012345678901234567890') + '';
27 | }
28 | des.finalize() + '';
29 | }
30 | });
31 | }, '$Rev$');
32 |
--------------------------------------------------------------------------------
/test/tripledes-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-tripledes-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'TripleDES',
6 |
7 | setUp: function () {
8 | this.data = {
9 | key: C.enc.Hex.parse('0001020304050607'),
10 | iv: C.enc.Hex.parse('08090a0b0c0d0e0f')
11 | };
12 | },
13 |
14 | profileSinglePartMessage: function () {
15 | var singlePartMessage = '';
16 | for (var i = 0; i < 100; i++) {
17 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
18 | }
19 |
20 | C.algo.TripleDES.createEncryptor(this.data.key, { iv: this.data.iv }).finalize(singlePartMessage) + '';
21 | },
22 |
23 | profileMultiPartMessage: function () {
24 | var des = C.algo.TripleDES.createEncryptor(this.data.key, { iv: this.data.iv });
25 | for (var i = 0; i < 100; i++) {
26 | des.process('12345678901234567890123456789012345678901234567890') + '';
27 | }
28 | des.finalize() + '';
29 | }
30 | });
31 | }, '$Rev$');
32 |
--------------------------------------------------------------------------------
/src/mode-ctr.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Counter block mode.
3 | */
4 | CryptoJS.mode.CTR = (function () {
5 | var CTR = CryptoJS.lib.BlockCipherMode.extend();
6 |
7 | var Encryptor = CTR.Encryptor = CTR.extend({
8 | processBlock: function (words, offset) {
9 | // Shortcuts
10 | var cipher = this._cipher
11 | var blockSize = cipher.blockSize;
12 | var iv = this._iv;
13 | var counter = this._counter;
14 |
15 | // Generate keystream
16 | if (iv) {
17 | counter = this._counter = iv.slice(0);
18 |
19 | // Remove IV for subsequent blocks
20 | this._iv = undefined;
21 | }
22 | var keystream = counter.slice(0);
23 | cipher.encryptBlock(keystream, 0);
24 |
25 | // Increment counter
26 | counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
27 |
28 | // Encrypt
29 | for (var i = 0; i < blockSize; i++) {
30 | words[offset + i] ^= keystream[i];
31 | }
32 | }
33 | });
34 |
35 | CTR.Decryptor = Encryptor;
36 |
37 | return CTR;
38 | }());
39 |
--------------------------------------------------------------------------------
/test/aes-profile.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-aes-profile', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Profiler.add({
5 | name: 'AES',
6 |
7 | setUp: function () {
8 | this.data = {
9 | key: C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'),
10 | iv: C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f')
11 | };
12 | },
13 |
14 | profileSinglePartMessage: function () {
15 | var singlePartMessage = '';
16 | for (var i = 0; i < 500; i++) {
17 | singlePartMessage += '12345678901234567890123456789012345678901234567890';
18 | }
19 |
20 | C.algo.AES.createEncryptor(this.data.key, { iv: this.data.iv }).finalize(singlePartMessage) + '';
21 | },
22 |
23 | profileMultiPartMessage: function () {
24 | var aes = C.algo.AES.createEncryptor(this.data.key, { iv: this.data.iv });
25 | for (var i = 0; i < 500; i++) {
26 | aes.process('12345678901234567890123456789012345678901234567890') + '';
27 | }
28 | aes.finalize() + '';
29 | }
30 | });
31 | }, '$Rev$');
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | # License
2 |
3 | [The MIT License (MIT)](http://opensource.org/licenses/MIT)
4 |
5 | Copyright (c) 2009-2013 Jeff Mott
6 | Copyright (c) 2013-2016 Evan Vosberg
7 |
8 | Permission is hereby granted, free of charge, to any person obtaining a copy
9 | of this software and associated documentation files (the "Software"), to deal
10 | in the Software without restriction, including without limitation the rights
11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 | copies of the Software, and to permit persons to whom the Software is
13 | furnished to do so, subject to the following conditions:
14 |
15 | The above copyright notice and this permission notice shall be included in
16 | all copies or substantial portions of the Software.
17 |
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 | THE SOFTWARE.
25 |
--------------------------------------------------------------------------------
/grunt/config/update_json.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 |
3 | 'use strict';
4 |
5 | module.exports = {
6 | options: {
7 | indent: ' '
8 | },
9 | npm: {
10 | src: '<%= meta.cwd %>package.json',
11 | dest: '<%= meta.build %>package.json',
12 | fields: {
13 | 'name': null,
14 | 'version': null,
15 | 'description': null,
16 | 'license': null,
17 | 'author': null,
18 | 'homepage': null,
19 | 'repository': null,
20 | 'keywords': null,
21 | 'main': null,
22 | 'dependencies': null,
23 | 'browser': null
24 | }
25 | },
26 | bower: {
27 | src: '<%= meta.cwd %>package.json',
28 | dest: '<%= meta.build %>bower.json',
29 | fields: {
30 | 'name': null,
31 | 'version': null,
32 | 'description': null,
33 | 'license': null,
34 | 'author': 'authors',
35 | 'homepage': null,
36 | 'repository': null,
37 | 'keywords': null,
38 | 'main': null,
39 | 'dependencies': null,
40 | 'browser': null,
41 | 'ignore': []
42 | }
43 | }
44 | };
45 |
--------------------------------------------------------------------------------
/Gruntfile.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 |
3 | 'use strict';
4 |
5 | var path = require('path');
6 |
7 | module.exports = function (grunt) {
8 |
9 | // Load all grunt tasks from node_modules, and config from /grunt/config
10 | require('load-grunt-config')(grunt, {
11 | configPath: path.join(process.cwd(), 'grunt/config'),
12 | config: {
13 | pkg: grunt.file.readJSON('package.json'),
14 | meta: {
15 | cwd: '',
16 | cwdAll: '**/*',
17 |
18 | source: 'src/',
19 | sourceAll: 'src/**/*',
20 |
21 | build: 'build/',
22 | buildAll: 'build/**/*',
23 |
24 | test: 'test/',
25 | testAll: 'test/**/*',
26 |
27 | npm: 'node_modules/',
28 | npmAll: 'node_modules/**/*'
29 | }
30 | }
31 | });
32 |
33 |
34 |
35 | // Will load the custom tasks
36 | grunt.loadTasks('./grunt/tasks');
37 |
38 | grunt.registerTask('build', 'Build a bundle', [
39 | 'clean:build',
40 | 'modularize:build',
41 | 'copy:build',
42 | 'update_json:npm',
43 | 'update_json:bower'
44 | ]);
45 |
46 | grunt.registerTask('default', 'Run code checker', [
47 | 'jsonlint',
48 | 'jshint'
49 | ]);
50 |
51 | };
52 |
--------------------------------------------------------------------------------
/test/pad-iso97971-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('pad-iso97971-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Iso97971',
6 |
7 | testPad1: function () {
8 | var data = C.lib.WordArray.create([0xdddddd00], 3);
9 | C.pad.Iso97971.pad(data, 1);
10 |
11 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd80]).toString(), data.toString());
12 | },
13 |
14 | testPad2: function () {
15 | var data = C.lib.WordArray.create([0xdddddd00], 3);
16 | C.pad.Iso97971.pad(data, 2);
17 |
18 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd80, 0x00000000]).toString(), data.toString());
19 | },
20 |
21 | testPadClamp: function () {
22 | var data = C.lib.WordArray.create([0xdddddddd, 0xdddddddd], 3);
23 | C.pad.Iso97971.pad(data, 2);
24 |
25 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd80, 0x00000000]).toString(), data.toString());
26 | },
27 |
28 | testUnpad: function () {
29 | var data = C.lib.WordArray.create([0xdddddd80, 0x00000000]);
30 | C.pad.Iso97971.unpad(data);
31 |
32 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00], 3).toString(), data.toString());
33 | }
34 | }));
35 | }, '$Rev$');
36 |
--------------------------------------------------------------------------------
/.jshintrc:
--------------------------------------------------------------------------------
1 | {
2 | "bitwise": false, // Prohibits the use of bitwise operators (not confuse & with &&)
3 | "curly": true, // Requires to always put curly braces around blocks in loops and conditionals
4 | "eqeqeq": false, // Prohibits the use of == and != in favor of === and !==
5 | "eqnull": true, // Suppresses warnings about == null comparisons
6 | "immed": true, // Requires immediate invocations to be wrapped in parens e.g. `(function () { } ());`
7 | "latedef": false, // Prohibits the use of a variable before it was defined
8 | "newcap": false, // Requires to capitalize names of constructor functions
9 | "noarg": true, // Prohibits the use of arguments.caller and arguments.callee
10 | "strict": false, // Requires all functions to run in ECMAScript 5's strict mode
11 | "undef": true, // Require non-global variables to be declared (prevents global leaks)
12 | "asi": true, // Suppresses warnings about missing semicolons
13 | "funcscope": false,
14 | "shadow": true,
15 | "expr": true,
16 | "-W041": true,
17 | "-W018": true,
18 | "globals": {
19 | "CryptoJS": true,
20 | "escape": true,
21 | "unescape": true,
22 | "Int8Array": true,
23 | "Int16Array": true,
24 | "Int32Array": true,
25 | "Uint8Array": true,
26 | "Uint16Array": true,
27 | "Uint32Array": true,
28 | "Uint8ClampedArray": true,
29 | "ArrayBuffer": true,
30 | "Float32Array": true,
31 | "Float64Array": true
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/test/x64-wordarray-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('x64-wordarray-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'X64WordArray',
6 |
7 | testInit0: function () {
8 | Y.Assert.areEqual('', C.x64.WordArray.create().toX32().toString());
9 | },
10 |
11 | testInit1: function () {
12 | var wordArray = C.x64.WordArray.create([
13 | C.x64.Word.create(0x00010203, 0x04050607),
14 | C.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
15 | ]);
16 |
17 | Y.Assert.areEqual('000102030405060718191a1b1c1d1e1f', wordArray.toX32().toString());
18 | },
19 |
20 | testInit2: function () {
21 | var wordArray = C.x64.WordArray.create([
22 | C.x64.Word.create(0x00010203, 0x04050607),
23 | C.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
24 | ], 10);
25 |
26 | Y.Assert.areEqual('00010203040506071819', wordArray.toX32().toString());
27 | },
28 |
29 | testToX32: function () {
30 | var wordArray = C.x64.WordArray.create([
31 | C.x64.Word.create(0x00010203, 0x04050607),
32 | C.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
33 | ], 10);
34 |
35 | Y.Assert.areEqual('00010203040506071819', wordArray.toX32().toString());
36 | }
37 | }));
38 | }, '$Rev$');
39 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "crypto-js",
3 | "title": "crypto-js",
4 | "description": "JavaScript library of crypto standards.",
5 | "version": "4.2.0",
6 | "homepage": "http://github.com/brix/crypto-js",
7 | "author": {
8 | "name": "Evan Vosberg",
9 | "url": "http://github.com/evanvosberg"
10 | },
11 | "repository": {
12 | "type": "git",
13 | "url": "http://github.com/brix/crypto-js.git"
14 | },
15 | "bugs": {
16 | "url": "http://github.com/brix/crypto-js/issues"
17 | },
18 | "license": "MIT",
19 | "scripts": {
20 | "build": "grunt build",
21 | "test": "grunt default"
22 | },
23 | "main": "index.js",
24 | "browser": {
25 | "crypto": false
26 | },
27 | "dependencies": {},
28 | "devDependencies": {
29 | "fmd": "~0.0.3",
30 | "grunt": "^1.4.3",
31 | "grunt-cli": "^1.4.3",
32 | "grunt-contrib-clean": "^2.0.0",
33 | "grunt-contrib-copy": "^1.0.0",
34 | "grunt-contrib-jshint": "^3.0.0",
35 | "grunt-jsonlint": "^2.1.3",
36 | "grunt-update-json": "^0.2.2",
37 | "load-grunt-config": "^4.0.0",
38 | "lodash": "^4.17.21"
39 | },
40 | "keywords": [
41 | "security",
42 | "crypto",
43 | "Hash",
44 | "MD5",
45 | "SHA1",
46 | "SHA-1",
47 | "SHA256",
48 | "SHA-256",
49 | "RC4",
50 | "Rabbit",
51 | "AES",
52 | "DES",
53 | "PBKDF2",
54 | "HMAC",
55 | "OFB",
56 | "CFB",
57 | "CTR",
58 | "CBC",
59 | "Base64",
60 | "Base64url"
61 | ]
62 | }
63 |
--------------------------------------------------------------------------------
/test/evpkdf-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-evpkdf-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'EvpKDF',
6 |
7 | testVector: function () {
8 | Y.Assert.areEqual('fdbdf3419fff98bdb0241390f62a9db35f4aba29d77566377997314ebfc709f20b5ca7b1081f94b1ac12e3c8ba87d05a', C.EvpKDF('password', 'saltsalt', { keySize: (256+128)/32 }).toString());
9 | },
10 |
11 | // There are no official test vectors that I could find, and the EVP implementation is short on comments.
12 | // Need to use the C code to generate more test vectors.
13 | // The iteration count in particular needs to be tested.
14 |
15 | testInputIntegrity: function () {
16 | var password = C.lib.WordArray.create([0x12345678]);
17 | var salt = C.lib.WordArray.create([0x12345678]);
18 |
19 | var expectedPassword = password.toString();
20 | var expectedSalt = salt.toString();
21 |
22 | C.EvpKDF(password, salt);
23 |
24 | Y.Assert.areEqual(expectedPassword, password.toString());
25 | Y.Assert.areEqual(expectedSalt, salt.toString());
26 | },
27 |
28 | testHelper: function () {
29 | Y.Assert.areEqual(C.algo.EvpKDF.create({ keySize: (256+128)/32 }).compute('password', 'saltsalt').toString(), C.EvpKDF('password', 'saltsalt', { keySize: (256+128)/32 }).toString());
30 | }
31 | }));
32 | }, '$Rev$');
33 |
--------------------------------------------------------------------------------
/test/enc-utf8-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('enc-utf8-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Utf8',
6 |
7 | testStringify1: function () {
8 | Y.Assert.areEqual('$', C.enc.Utf8.stringify(C.lib.WordArray.create([0x24000000], 1)));
9 | },
10 |
11 | testStringify2: function () {
12 | Y.Assert.areEqual('¢', C.enc.Utf8.stringify(C.lib.WordArray.create([0xc2a20000], 2)));
13 | },
14 |
15 | testStringify3: function () {
16 | Y.Assert.areEqual('€', C.enc.Utf8.stringify(C.lib.WordArray.create([0xe282ac00], 3)));
17 | },
18 |
19 | testStringify4: function () {
20 | Y.Assert.areEqual('𤭢', C.enc.Utf8.stringify(C.lib.WordArray.create([0xf0a4ada2], 4)));
21 | },
22 |
23 | testParse1: function () {
24 | Y.Assert.areEqual(C.lib.WordArray.create([0x24000000], 1).toString(), C.enc.Utf8.parse('$').toString());
25 | },
26 |
27 | testParse2: function () {
28 | Y.Assert.areEqual(C.lib.WordArray.create([0xc2a20000], 2).toString(), C.enc.Utf8.parse('¢').toString());
29 | },
30 |
31 | testParse3: function () {
32 | Y.Assert.areEqual(C.lib.WordArray.create([0xe282ac00], 3).toString(), C.enc.Utf8.parse('€').toString());
33 | },
34 |
35 | testParse4: function () {
36 | Y.Assert.areEqual(C.lib.WordArray.create([0xf0a4ada2], 4).toString(), C.enc.Utf8.parse('𤭢').toString());
37 | }
38 | }));
39 | }, '$Rev$');
40 |
--------------------------------------------------------------------------------
/src/format-hex.js:
--------------------------------------------------------------------------------
1 | (function (undefined) {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var CipherParams = C_lib.CipherParams;
6 | var C_enc = C.enc;
7 | var Hex = C_enc.Hex;
8 | var C_format = C.format;
9 |
10 | var HexFormatter = C_format.Hex = {
11 | /**
12 | * Converts the ciphertext of a cipher params object to a hexadecimally encoded string.
13 | *
14 | * @param {CipherParams} cipherParams The cipher params object.
15 | *
16 | * @return {string} The hexadecimally encoded string.
17 | *
18 | * @static
19 | *
20 | * @example
21 | *
22 | * var hexString = CryptoJS.format.Hex.stringify(cipherParams);
23 | */
24 | stringify: function (cipherParams) {
25 | return cipherParams.ciphertext.toString(Hex);
26 | },
27 |
28 | /**
29 | * Converts a hexadecimally encoded ciphertext string to a cipher params object.
30 | *
31 | * @param {string} input The hexadecimally encoded string.
32 | *
33 | * @return {CipherParams} The cipher params object.
34 | *
35 | * @static
36 | *
37 | * @example
38 | *
39 | * var cipherParams = CryptoJS.format.Hex.parse(hexString);
40 | */
41 | parse: function (input) {
42 | var ciphertext = Hex.parse(input);
43 | return CipherParams.create({ ciphertext: ciphertext });
44 | }
45 | };
46 | }());
47 |
--------------------------------------------------------------------------------
/test/mode-ecb-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('mode-ecb-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'ECB',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.message = C.lib.WordArray.create([
11 | 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
12 | 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f
13 | ]);
14 | this.data.key = C.lib.WordArray.create([0x20212223, 0x24252627, 0x28292a2b, 0x2c2d2e2f]);
15 | },
16 |
17 | testEncryptor: function () {
18 | // Compute expected
19 | var expected = this.data.message.clone();
20 | var aes = C.algo.AES.createEncryptor(this.data.key);
21 | aes.encryptBlock(expected.words, 0);
22 | aes.encryptBlock(expected.words, 4);
23 |
24 | // Compute actual
25 | var actual = C.AES.encrypt(this.data.message, this.data.key, { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext;
26 |
27 | // Test
28 | Y.Assert.areEqual(expected.toString(), actual.toString());
29 | },
30 |
31 | testDecryptor: function () {
32 | var encrypted = C.AES.encrypt(this.data.message, this.data.key, { mode: C.mode.ECB, padding: C.pad.NoPadding });
33 | var decrypted = C.AES.decrypt(encrypted, this.data.key, { mode: C.mode.ECB, padding: C.pad.NoPadding });
34 |
35 | Y.Assert.areEqual(this.data.message.toString(), decrypted.toString());
36 | }
37 | }));
38 | }, '$Rev$');
39 |
--------------------------------------------------------------------------------
/src/sha224.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var WordArray = C_lib.WordArray;
6 | var C_algo = C.algo;
7 | var SHA256 = C_algo.SHA256;
8 |
9 | /**
10 | * SHA-224 hash algorithm.
11 | */
12 | var SHA224 = C_algo.SHA224 = SHA256.extend({
13 | _doReset: function () {
14 | this._hash = new WordArray.init([
15 | 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
16 | 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
17 | ]);
18 | },
19 |
20 | _doFinalize: function () {
21 | var hash = SHA256._doFinalize.call(this);
22 |
23 | hash.sigBytes -= 4;
24 |
25 | return hash;
26 | }
27 | });
28 |
29 | /**
30 | * Shortcut function to the hasher's object interface.
31 | *
32 | * @param {WordArray|string} message The message to hash.
33 | *
34 | * @return {WordArray} The hash.
35 | *
36 | * @static
37 | *
38 | * @example
39 | *
40 | * var hash = CryptoJS.SHA224('message');
41 | * var hash = CryptoJS.SHA224(wordArray);
42 | */
43 | C.SHA224 = SHA256._createHelper(SHA224);
44 |
45 | /**
46 | * Shortcut function to the HMAC's object interface.
47 | *
48 | * @param {WordArray|string} message The message to hash.
49 | * @param {WordArray|string} key The secret key.
50 | *
51 | * @return {WordArray} The HMAC.
52 | *
53 | * @static
54 | *
55 | * @example
56 | *
57 | * var hmac = CryptoJS.HmacSHA224(message, key);
58 | */
59 | C.HmacSHA224 = SHA256._createHmacHelper(SHA224);
60 | }());
61 |
--------------------------------------------------------------------------------
/test/pad-iso10126-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('pad-iso10126-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Iso10126',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | // Save original random method
11 | this.data.random = C.lib.WordArray.random;
12 |
13 | // Replace random method with one that returns a predictable value
14 | C.lib.WordArray.random = function (nBytes) {
15 | var words = [];
16 | for (var i = 0; i < nBytes; i += 4) {
17 | words.push([0x11223344]);
18 | }
19 |
20 | return C.lib.WordArray.create(words, nBytes);
21 | };
22 | },
23 |
24 | tearDown: function () {
25 | // Restore random method
26 | C.lib.WordArray.random = this.data.random;
27 | },
28 |
29 | testPad: function () {
30 | var data = C.lib.WordArray.create([0xdddddd00], 3);
31 | C.pad.Iso10126.pad(data, 2);
32 |
33 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd11, 0x22334405]).toString(), data.toString());
34 | },
35 |
36 | testPadClamp: function () {
37 | var data = C.lib.WordArray.create([0xdddddddd, 0xdddddddd], 3);
38 | C.pad.Iso10126.pad(data, 2);
39 |
40 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd11, 0x22334405]).toString(), data.toString());
41 | },
42 |
43 | testUnpad: function () {
44 | var data = C.lib.WordArray.create([0xdddddd11, 0x22334405]);
45 | C.pad.Iso10126.unpad(data);
46 |
47 | Y.Assert.areEqual(C.lib.WordArray.create([0xdddddd00], 3).toString(), data.toString());
48 | }
49 | }));
50 | }, '$Rev$');
51 |
--------------------------------------------------------------------------------
/test/format-openssl-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('format-openssl-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'OpenSSLFormatter',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.ciphertext = C.lib.WordArray.create([0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f]);
11 | this.data.salt = C.lib.WordArray.create([0x01234567, 0x89abcdef]);
12 | },
13 |
14 | testSaltedToString: function () {
15 | Y.Assert.areEqual(C.enc.Latin1.parse('Salted__').concat(this.data.salt).concat(this.data.ciphertext).toString(C.enc.Base64), C.format.OpenSSL.stringify(C.lib.CipherParams.create({ ciphertext: this.data.ciphertext, salt: this.data.salt })));
16 | },
17 |
18 | testUnsaltedToString: function () {
19 | Y.Assert.areEqual(this.data.ciphertext.toString(C.enc.Base64), C.format.OpenSSL.stringify(C.lib.CipherParams.create({ ciphertext: this.data.ciphertext })));
20 | },
21 |
22 | testSaltedFromString: function () {
23 | var openSSLStr = C.format.OpenSSL.stringify(C.lib.CipherParams.create({ ciphertext: this.data.ciphertext, salt: this.data.salt }));
24 | var cipherParams = C.format.OpenSSL.parse(openSSLStr);
25 |
26 | Y.Assert.areEqual(this.data.ciphertext.toString(), cipherParams.ciphertext.toString());
27 | Y.Assert.areEqual(this.data.salt.toString(), cipherParams.salt.toString());
28 | },
29 |
30 | testUnsaltedFromString: function () {
31 | var openSSLStr = C.format.OpenSSL.stringify(C.lib.CipherParams.create({ ciphertext: this.data.ciphertext }));
32 | var cipherParams = C.format.OpenSSL.parse(openSSLStr);
33 |
34 | Y.Assert.areEqual(this.data.ciphertext.toString(), cipherParams.ciphertext.toString());
35 | }
36 | }));
37 | }, '$Rev$');
38 |
--------------------------------------------------------------------------------
/src/mode-cfb.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Cipher Feedback block mode.
3 | */
4 | CryptoJS.mode.CFB = (function () {
5 | var CFB = CryptoJS.lib.BlockCipherMode.extend();
6 |
7 | CFB.Encryptor = CFB.extend({
8 | processBlock: function (words, offset) {
9 | // Shortcuts
10 | var cipher = this._cipher;
11 | var blockSize = cipher.blockSize;
12 |
13 | generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
14 |
15 | // Remember this block to use with next block
16 | this._prevBlock = words.slice(offset, offset + blockSize);
17 | }
18 | });
19 |
20 | CFB.Decryptor = CFB.extend({
21 | processBlock: function (words, offset) {
22 | // Shortcuts
23 | var cipher = this._cipher;
24 | var blockSize = cipher.blockSize;
25 |
26 | // Remember this block to use with next block
27 | var thisBlock = words.slice(offset, offset + blockSize);
28 |
29 | generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
30 |
31 | // This block becomes the previous block
32 | this._prevBlock = thisBlock;
33 | }
34 | });
35 |
36 | function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) {
37 | var keystream;
38 |
39 | // Shortcut
40 | var iv = this._iv;
41 |
42 | // Generate keystream
43 | if (iv) {
44 | keystream = iv.slice(0);
45 |
46 | // Remove IV for subsequent blocks
47 | this._iv = undefined;
48 | } else {
49 | keystream = this._prevBlock;
50 | }
51 | cipher.encryptBlock(keystream, 0);
52 |
53 | // Encrypt
54 | for (var i = 0; i < blockSize; i++) {
55 | words[offset + i] ^= keystream[i];
56 | }
57 | }
58 |
59 | return CFB;
60 | }());
61 |
--------------------------------------------------------------------------------
/src/sha384.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_x64 = C.x64;
5 | var X64Word = C_x64.Word;
6 | var X64WordArray = C_x64.WordArray;
7 | var C_algo = C.algo;
8 | var SHA512 = C_algo.SHA512;
9 |
10 | /**
11 | * SHA-384 hash algorithm.
12 | */
13 | var SHA384 = C_algo.SHA384 = SHA512.extend({
14 | _doReset: function () {
15 | this._hash = new X64WordArray.init([
16 | new X64Word.init(0xcbbb9d5d, 0xc1059ed8), new X64Word.init(0x629a292a, 0x367cd507),
17 | new X64Word.init(0x9159015a, 0x3070dd17), new X64Word.init(0x152fecd8, 0xf70e5939),
18 | new X64Word.init(0x67332667, 0xffc00b31), new X64Word.init(0x8eb44a87, 0x68581511),
19 | new X64Word.init(0xdb0c2e0d, 0x64f98fa7), new X64Word.init(0x47b5481d, 0xbefa4fa4)
20 | ]);
21 | },
22 |
23 | _doFinalize: function () {
24 | var hash = SHA512._doFinalize.call(this);
25 |
26 | hash.sigBytes -= 16;
27 |
28 | return hash;
29 | }
30 | });
31 |
32 | /**
33 | * Shortcut function to the hasher's object interface.
34 | *
35 | * @param {WordArray|string} message The message to hash.
36 | *
37 | * @return {WordArray} The hash.
38 | *
39 | * @static
40 | *
41 | * @example
42 | *
43 | * var hash = CryptoJS.SHA384('message');
44 | * var hash = CryptoJS.SHA384(wordArray);
45 | */
46 | C.SHA384 = SHA512._createHelper(SHA384);
47 |
48 | /**
49 | * Shortcut function to the HMAC's object interface.
50 | *
51 | * @param {WordArray|string} message The message to hash.
52 | * @param {WordArray|string} key The secret key.
53 | *
54 | * @return {WordArray} The HMAC.
55 | *
56 | * @static
57 | *
58 | * @example
59 | *
60 | * var hmac = CryptoJS.HmacSHA384(message, key);
61 | */
62 | C.HmacSHA384 = SHA512._createHmacHelper(SHA384);
63 | }());
64 |
--------------------------------------------------------------------------------
/src/lib-typedarrays.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Check if typed arrays are supported
3 | if (typeof ArrayBuffer != 'function') {
4 | return;
5 | }
6 |
7 | // Shortcuts
8 | var C = CryptoJS;
9 | var C_lib = C.lib;
10 | var WordArray = C_lib.WordArray;
11 |
12 | // Reference original init
13 | var superInit = WordArray.init;
14 |
15 | // Augment WordArray.init to handle typed arrays
16 | var subInit = WordArray.init = function (typedArray) {
17 | // Convert buffers to uint8
18 | if (typedArray instanceof ArrayBuffer) {
19 | typedArray = new Uint8Array(typedArray);
20 | }
21 |
22 | // Convert other array views to uint8
23 | if (
24 | typedArray instanceof Int8Array ||
25 | (typeof Uint8ClampedArray !== "undefined" && typedArray instanceof Uint8ClampedArray) ||
26 | typedArray instanceof Int16Array ||
27 | typedArray instanceof Uint16Array ||
28 | typedArray instanceof Int32Array ||
29 | typedArray instanceof Uint32Array ||
30 | typedArray instanceof Float32Array ||
31 | typedArray instanceof Float64Array
32 | ) {
33 | typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
34 | }
35 |
36 | // Handle Uint8Array
37 | if (typedArray instanceof Uint8Array) {
38 | // Shortcut
39 | var typedArrayByteLength = typedArray.byteLength;
40 |
41 | // Extract bytes
42 | var words = [];
43 | for (var i = 0; i < typedArrayByteLength; i++) {
44 | words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
45 | }
46 |
47 | // Initialize this word array
48 | superInit.call(this, words, typedArrayByteLength);
49 | } else {
50 | // Else call normal init
51 | superInit.apply(this, arguments);
52 | }
53 | };
54 |
55 | subInit.prototype = WordArray;
56 | }());
57 |
--------------------------------------------------------------------------------
/test/mode-cbc-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('mode-cbc-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'CBC',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.message = C.lib.WordArray.create([
11 | 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
12 | 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f
13 | ]);
14 | this.data.key = C.lib.WordArray.create([0x20212223, 0x24252627, 0x28292a2b, 0x2c2d2e2f]);
15 | this.data.iv = C.lib.WordArray.create([0x30313233, 0x34353637, 0x38393a3b, 0x3c3d3e3f]);
16 | },
17 |
18 | testEncryptor: function () {
19 | // Compute expected
20 | var expected = this.data.message.clone();
21 | var aes = C.algo.AES.createEncryptor(this.data.key);
22 |
23 | // First block XORed with IV, then encrypted
24 | for (var i = 0; i < 4; i++) {
25 | expected.words[i] ^= this.data.iv.words[i];
26 | }
27 | aes.encryptBlock(expected.words, 0);
28 |
29 | // Subsequent blocks XORed with previous crypted block, then encrypted
30 | for (var i = 4; i < 8; i++) {
31 | expected.words[i] ^= expected.words[i - 4];
32 | }
33 | aes.encryptBlock(expected.words, 4);
34 |
35 | // Compute actual
36 | var actual = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.CBC, padding: C.pad.NoPadding }).ciphertext;
37 |
38 | // Test
39 | Y.Assert.areEqual(expected.toString(), actual.toString());
40 | },
41 |
42 | testDecryptor: function () {
43 | var encrypted = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.CBC, padding: C.pad.NoPadding });
44 | var decrypted = C.AES.decrypt(encrypted, this.data.key, { iv: this.data.iv, mode: C.mode.CBC, padding: C.pad.NoPadding });
45 |
46 | Y.Assert.areEqual(this.data.message.toString(), decrypted.toString());
47 | }
48 | }));
49 | }, '$Rev$');
50 |
--------------------------------------------------------------------------------
/test/mode-ofb-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('mode-ofb-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'OFB',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.message = C.lib.WordArray.create([
11 | 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
12 | 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f
13 | ]);
14 | this.data.key = C.lib.WordArray.create([0x20212223, 0x24252627, 0x28292a2b, 0x2c2d2e2f]);
15 | this.data.iv = C.lib.WordArray.create([0x30313233, 0x34353637, 0x38393a3b, 0x3c3d3e3f]);
16 | },
17 |
18 | testEncryptor: function () {
19 | // Compute expected
20 | var expected = this.data.message.clone();
21 | var aes = C.algo.AES.createEncryptor(this.data.key);
22 |
23 | // First block XORed with encrypted IV
24 | var keystream = this.data.iv.words.slice(0);
25 | aes.encryptBlock(keystream, 0);
26 | for (var i = 0; i < 4; i++) {
27 | expected.words[i] ^= keystream[i];
28 | }
29 |
30 | // Subsequent blocks XORed with encrypted previous keystream
31 | aes.encryptBlock(keystream, 0);
32 | for (var i = 4; i < 8; i++) {
33 | expected.words[i] ^= keystream[i % 4];
34 | }
35 |
36 | // Compute actual
37 | var actual = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.OFB, padding: C.pad.NoPadding }).ciphertext;
38 |
39 | // Test
40 | Y.Assert.areEqual(expected.toString(), actual.toString());
41 | },
42 |
43 | testDecryptor: function () {
44 | var encrypted = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.OFB, padding: C.pad.NoPadding });
45 | var decrypted = C.AES.decrypt(encrypted, this.data.key, { iv: this.data.iv, mode: C.mode.OFB, padding: C.pad.NoPadding });
46 |
47 | Y.Assert.areEqual(this.data.message.toString(), decrypted.toString());
48 | }
49 | }));
50 | }, '$Rev$');
51 |
--------------------------------------------------------------------------------
/test/mode-cfb-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('mode-cfb-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'CFB',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.message = C.lib.WordArray.create([
11 | 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
12 | 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f
13 | ]);
14 | this.data.key = C.lib.WordArray.create([0x20212223, 0x24252627, 0x28292a2b, 0x2c2d2e2f]);
15 | this.data.iv = C.lib.WordArray.create([0x30313233, 0x34353637, 0x38393a3b, 0x3c3d3e3f]);
16 | },
17 |
18 | testEncryptor: function () {
19 | // Compute expected
20 | var expected = this.data.message.clone();
21 | var aes = C.algo.AES.createEncryptor(this.data.key);
22 |
23 | // First block XORed with encrypted IV
24 | var keystream = this.data.iv.words.slice(0);
25 | aes.encryptBlock(keystream, 0);
26 | for (var i = 0; i < 4; i++) {
27 | expected.words[i] ^= keystream[i];
28 | }
29 |
30 | // Subsequent blocks XORed with encrypted previous crypted block
31 | var keystream = expected.words.slice(0, 4);
32 | aes.encryptBlock(keystream, 0);
33 | for (var i = 4; i < 8; i++) {
34 | expected.words[i] ^= keystream[i % 4];
35 | }
36 |
37 | // Compute actual
38 | var actual = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.CFB, padding: C.pad.NoPadding }).ciphertext;
39 |
40 | // Test
41 | Y.Assert.areEqual(expected.toString(), actual.toString());
42 | },
43 |
44 | testDecryptor: function () {
45 | var encrypted = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.CFB, padding: C.pad.NoPadding });
46 | var decrypted = C.AES.decrypt(encrypted, this.data.key, { iv: this.data.iv, mode: C.mode.CFB, padding: C.pad.NoPadding });
47 |
48 | Y.Assert.areEqual(this.data.message.toString(), decrypted.toString());
49 | }
50 | }));
51 | }, '$Rev$');
52 |
--------------------------------------------------------------------------------
/src/mode-ctr-gladman.js:
--------------------------------------------------------------------------------
1 | /** @preserve
2 | * Counter block mode compatible with Dr Brian Gladman fileenc.c
3 | * derived from CryptoJS.mode.CTR
4 | * Jan Hruby jhruby.web@gmail.com
5 | */
6 | CryptoJS.mode.CTRGladman = (function () {
7 | var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
8 |
9 | function incWord(word)
10 | {
11 | if (((word >> 24) & 0xff) === 0xff) { //overflow
12 | var b1 = (word >> 16)&0xff;
13 | var b2 = (word >> 8)&0xff;
14 | var b3 = word & 0xff;
15 |
16 | if (b1 === 0xff) // overflow b1
17 | {
18 | b1 = 0;
19 | if (b2 === 0xff)
20 | {
21 | b2 = 0;
22 | if (b3 === 0xff)
23 | {
24 | b3 = 0;
25 | }
26 | else
27 | {
28 | ++b3;
29 | }
30 | }
31 | else
32 | {
33 | ++b2;
34 | }
35 | }
36 | else
37 | {
38 | ++b1;
39 | }
40 |
41 | word = 0;
42 | word += (b1 << 16);
43 | word += (b2 << 8);
44 | word += b3;
45 | }
46 | else
47 | {
48 | word += (0x01 << 24);
49 | }
50 | return word;
51 | }
52 |
53 | function incCounter(counter)
54 | {
55 | if ((counter[0] = incWord(counter[0])) === 0)
56 | {
57 | // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
58 | counter[1] = incWord(counter[1]);
59 | }
60 | return counter;
61 | }
62 |
63 | var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
64 | processBlock: function (words, offset) {
65 | // Shortcuts
66 | var cipher = this._cipher
67 | var blockSize = cipher.blockSize;
68 | var iv = this._iv;
69 | var counter = this._counter;
70 |
71 | // Generate keystream
72 | if (iv) {
73 | counter = this._counter = iv.slice(0);
74 |
75 | // Remove IV for subsequent blocks
76 | this._iv = undefined;
77 | }
78 |
79 | incCounter(counter);
80 |
81 | var keystream = counter.slice(0);
82 | cipher.encryptBlock(keystream, 0);
83 |
84 | // Encrypt
85 | for (var i = 0; i < blockSize; i++) {
86 | words[offset + i] ^= keystream[i];
87 | }
88 | }
89 | });
90 |
91 | CTRGladman.Decryptor = Encryptor;
92 |
93 | return CTRGladman;
94 | }());
95 |
96 |
97 |
--------------------------------------------------------------------------------
/test/enc-utf16-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('enc-utf16-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Utf16',
6 |
7 | testStringify1: function () {
8 | Y.Assert.areEqual('z', C.enc.Utf16.stringify(C.lib.WordArray.create([0x007a0000], 2)));
9 | },
10 |
11 | testStringify2: function () {
12 | Y.Assert.areEqual('水', C.enc.Utf16.stringify(C.lib.WordArray.create([0x6c340000], 2)));
13 | },
14 |
15 | testStringify3: function () {
16 | Y.Assert.areEqual('𐀀', C.enc.Utf16.stringify(C.lib.WordArray.create([0xd800dc00], 4)));
17 | },
18 |
19 | testStringify4: function () {
20 | Y.Assert.areEqual('𝄞', C.enc.Utf16.stringify(C.lib.WordArray.create([0xd834dd1e], 4)));
21 | },
22 |
23 | testStringify5: function () {
24 | Y.Assert.areEqual('', C.enc.Utf16.stringify(C.lib.WordArray.create([0xdbffdffd], 4)));
25 | },
26 |
27 | testStringifyLE: function () {
28 | Y.Assert.areEqual('', C.enc.Utf16LE.stringify(C.lib.WordArray.create([0xffdbfddf], 4)));
29 | },
30 |
31 | testParse1: function () {
32 | Y.Assert.areEqual(C.lib.WordArray.create([0x007a0000], 2).toString(), C.enc.Utf16.parse('z').toString());
33 | },
34 |
35 | testParse2: function () {
36 | Y.Assert.areEqual(C.lib.WordArray.create([0x6c340000], 2).toString(), C.enc.Utf16.parse('水').toString());
37 | },
38 |
39 | testParse3: function () {
40 | Y.Assert.areEqual(C.lib.WordArray.create([0xd800dc00], 4).toString(), C.enc.Utf16.parse('𐀀').toString());
41 | },
42 |
43 | testParse4: function () {
44 | Y.Assert.areEqual(C.lib.WordArray.create([0xd834dd1e], 4).toString(), C.enc.Utf16.parse('𝄞').toString());
45 | },
46 |
47 | testParse5: function () {
48 | Y.Assert.areEqual(C.lib.WordArray.create([0xdbffdffd], 4).toString(), C.enc.Utf16.parse('').toString());
49 | },
50 |
51 | testParseLE: function () {
52 | Y.Assert.areEqual(C.lib.WordArray.create([0xffdbfddf], 4).toString(), C.enc.Utf16LE.parse('').toString());
53 | }
54 | }));
55 | }, '$Rev$');
56 |
--------------------------------------------------------------------------------
/test/mode-ctr-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('mode-ctr-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'CTR',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.message = C.lib.WordArray.create([
11 | 0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f,
12 | 0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f
13 | ]);
14 | this.data.key = C.lib.WordArray.create([0x20212223, 0x24252627, 0x28292a2b, 0x2c2d2e2f]);
15 | this.data.iv = C.lib.WordArray.create([0x30313233, 0x34353637, 0x38393a3b, 0x3c3d3e3f]);
16 | },
17 |
18 | testEncryptor: function () {
19 | // Compute expected
20 | var expected = this.data.message.clone();
21 | var aes = C.algo.AES.createEncryptor(this.data.key);
22 |
23 | // Counter initialized with IV
24 | var counter = this.data.iv.words.slice(0);
25 |
26 | // First block XORed with encrypted counter
27 | var keystream = counter.slice(0);
28 | aes.encryptBlock(keystream, 0);
29 | for (var i = 0; i < 4; i++) {
30 | expected.words[i] ^= keystream[i];
31 | }
32 |
33 | // Subsequent blocks XORed with encrypted incremented counter
34 | counter[3]++;
35 | var keystream = counter.slice(0);
36 | aes.encryptBlock(keystream, 0);
37 | for (var i = 4; i < 8; i++) {
38 | expected.words[i] ^= keystream[i % 4];
39 | }
40 |
41 | // Compute actual
42 | var actual = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.CTR, padding: C.pad.NoPadding }).ciphertext;
43 |
44 | // Test
45 | Y.Assert.areEqual(expected.toString(), actual.toString());
46 | },
47 |
48 | testDecryptor: function () {
49 | var encrypted = C.AES.encrypt(this.data.message, this.data.key, { iv: this.data.iv, mode: C.mode.CTR, padding: C.pad.NoPadding });
50 | var decrypted = C.AES.decrypt(encrypted, this.data.key, { iv: this.data.iv, mode: C.mode.CTR, padding: C.pad.NoPadding });
51 |
52 | Y.Assert.areEqual(this.data.message.toString(), decrypted.toString());
53 | }
54 | }));
55 | }, '$Rev$');
56 |
--------------------------------------------------------------------------------
/test/lib-typedarrays-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('lib-wordarray-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | if (typeof ArrayBuffer != 'undefined') {
5 | Y.Test.Runner.add(new Y.Test.Case({
6 | name: 'TypedArrays',
7 |
8 | setUp: function () {
9 | this.data = {};
10 |
11 | this.data.buffer = new ArrayBuffer(8);
12 |
13 | var uint8View = new Uint8Array(this.data.buffer);
14 | uint8View[0] = 0x01;
15 | uint8View[1] = 0x23;
16 | uint8View[2] = 0x45;
17 | uint8View[3] = 0x67;
18 | uint8View[4] = 0x89;
19 | uint8View[5] = 0xab;
20 | uint8View[6] = 0xcd;
21 | uint8View[7] = 0xef;
22 | },
23 |
24 | testInt8Array: function () {
25 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Int8Array(this.data.buffer)).toString());
26 | },
27 |
28 | testUint8Array: function () {
29 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Uint8Array(this.data.buffer)).toString());
30 | },
31 |
32 | testUint8ClampedArray: function () {
33 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Uint8ClampedArray(this.data.buffer)).toString());
34 | },
35 |
36 | testInt16Array: function () {
37 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Int16Array(this.data.buffer)).toString());
38 | },
39 |
40 | testUint16Array: function () {
41 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Uint16Array(this.data.buffer)).toString());
42 | },
43 |
44 | testInt32Array: function () {
45 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Int32Array(this.data.buffer)).toString());
46 | },
47 |
48 | testUint32Array: function () {
49 | Y.Assert.areEqual('0123456789abcdef', C.lib.WordArray.create(new Uint32Array(this.data.buffer)).toString());
50 | },
51 |
52 | testPartialView: function () {
53 | Y.Assert.areEqual('456789ab', C.lib.WordArray.create(new Int16Array(this.data.buffer, 2, 2)).toString());
54 | }
55 | }));
56 | }
57 | }, '$Rev$');
58 |
--------------------------------------------------------------------------------
/test/lib-serializablecipher-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('lib-serializablecipher-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SerializableCipher',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.message = C.lib.WordArray.create([0x00010203, 0x04050607, 0x08090a0b, 0x0c0d0e0f]);
11 | this.data.key = C.lib.WordArray.create([0x10111213, 0x14151617, 0x18191a1b, 0x1c1d1e1f]);
12 | this.data.iv = C.lib.WordArray.create([0x20212223, 0x24252627, 0x28292a2b, 0x2c2d2e2f]);
13 | },
14 |
15 | testEncrypt: function () {
16 | // Compute expected
17 | var aes = C.algo.AES.createEncryptor(this.data.key, { iv: this.data.iv });
18 | var ciphertext = aes.finalize(this.data.message);
19 | var expected = C.lib.CipherParams.create({
20 | ciphertext: ciphertext,
21 | key: this.data.key,
22 | iv: this.data.iv,
23 | algorithm: C.algo.AES,
24 | mode: aes.cfg.mode,
25 | padding: aes.cfg.padding,
26 | blockSize: aes.blockSize,
27 | formatter: C.format.OpenSSL
28 | });
29 |
30 | // Compute actual
31 | var actual = C.lib.SerializableCipher.encrypt(C.algo.AES, this.data.message, this.data.key, { iv: this.data.iv });
32 |
33 | // Test
34 | Y.Assert.areEqual(expected.toString(), actual.toString());
35 | Y.Assert.areEqual(expected.ciphertext.toString(), actual.ciphertext.toString());
36 | Y.Assert.areEqual(expected.key.toString(), actual.key.toString());
37 | Y.Assert.areEqual(expected.iv.toString(), actual.iv.toString());
38 | Y.Assert.areEqual(expected.algorithm, actual.algorithm);
39 | Y.Assert.areEqual(expected.mode, actual.mode);
40 | Y.Assert.areEqual(expected.padding, actual.padding);
41 | Y.Assert.areEqual(expected.blockSize, actual.blockSize);
42 | },
43 |
44 | testDecrypt: function () {
45 | var encrypted = C.lib.SerializableCipher.encrypt(C.algo.AES, this.data.message, this.data.key, { iv: this.data.iv }) + '';
46 | var decrypted = C.lib.SerializableCipher.decrypt(C.algo.AES, encrypted, this.data.key, { iv: this.data.iv });
47 |
48 | Y.Assert.areEqual(this.data.message.toString(), decrypted.toString());
49 | }
50 | }));
51 | }, '$Rev$');
52 |
--------------------------------------------------------------------------------
/test/sha384-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha384-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SHA384',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', C.SHA384('').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1', C.SHA384('The quick brown fox jumps over the lazy dog').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7', C.SHA384('The quick brown fox jumps over the lazy dog.').toString());
17 | },
18 |
19 | testUpdateAndLongMessage: function () {
20 | var sha384 = C.algo.SHA384.create();
21 | for (var i = 0; i < 100; i++) {
22 | sha384.update('12345678901234567890123456789012345678901234567890');
23 | }
24 |
25 | Y.Assert.areEqual('297a519246d6f639a4020119e1f03fc8d77171647b2ff75ea4125b7150fed0cdcc93f8dca1c3c6a624d5e88d780d82cd', sha384.finalize().toString());
26 | },
27 |
28 | testClone: function () {
29 | var sha384 = C.algo.SHA384.create();
30 |
31 | Y.Assert.areEqual(C.SHA384('a').toString(), sha384.update('a').clone().finalize().toString());
32 | Y.Assert.areEqual(C.SHA384('ab').toString(), sha384.update('b').clone().finalize().toString());
33 | Y.Assert.areEqual(C.SHA384('abc').toString(), sha384.update('c').clone().finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 |
39 | var expected = message.toString();
40 |
41 | C.SHA384(message);
42 |
43 | Y.Assert.areEqual(expected, message.toString());
44 | },
45 |
46 | testHelper: function () {
47 | Y.Assert.areEqual(C.algo.SHA384.create().finalize('').toString(), C.SHA384('').toString());
48 | },
49 |
50 | testHmacHelper: function () {
51 | Y.Assert.areEqual(C.algo.HMAC.create(C.algo.SHA384, C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).finalize('Hi There').toString(), C.HmacSHA384('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
52 | }
53 | }));
54 | }, '$Rev$');
55 |
--------------------------------------------------------------------------------
/test/sha512-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha512-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SHA512',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', C.SHA512('').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6', C.SHA512('The quick brown fox jumps over the lazy dog').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed', C.SHA512('The quick brown fox jumps over the lazy dog.').toString());
17 | },
18 |
19 | testUpdateAndLongMessage: function () {
20 | var sha512 = C.algo.SHA512.create();
21 | for (var i = 0; i < 100; i++) {
22 | sha512.update('12345678901234567890123456789012345678901234567890');
23 | }
24 |
25 | Y.Assert.areEqual('9bc64f37c54606dff234b6607e06683c7ba248558d0ec74a11525d9f59e0be566489cc9413c00ca5e9db705fc52ba71214bcf118f65072fe284af8f8cf9500af', sha512.finalize().toString());
26 | },
27 |
28 | testClone: function () {
29 | var sha512 = C.algo.SHA512.create();
30 |
31 | Y.Assert.areEqual(C.SHA512('a').toString(), sha512.update('a').clone().finalize().toString());
32 | Y.Assert.areEqual(C.SHA512('ab').toString(), sha512.update('b').clone().finalize().toString());
33 | Y.Assert.areEqual(C.SHA512('abc').toString(), sha512.update('c').clone().finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 |
39 | var expected = message.toString();
40 |
41 | C.SHA512(message);
42 |
43 | Y.Assert.areEqual(expected, message.toString());
44 | },
45 |
46 | testHelper: function () {
47 | Y.Assert.areEqual(C.algo.SHA512.create().finalize('').toString(), C.SHA512('').toString());
48 | },
49 |
50 | testHmacHelper: function () {
51 | Y.Assert.areEqual(C.algo.HMAC.create(C.algo.SHA512, C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).finalize('Hi There').toString(), C.HmacSHA512('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
52 | }
53 | }));
54 | }, '$Rev$');
55 |
--------------------------------------------------------------------------------
/test/test1.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CryptoJS Test Suite
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
58 |
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/test/config-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('config-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Config',
6 |
7 | setUp: function () {
8 | this.data = {
9 | saltA: CryptoJS.enc.Hex.parse('AA00000000000000'),
10 | saltB: CryptoJS.enc.Hex.parse('BB00000000000000')
11 | };
12 | },
13 |
14 | testEncrypt: function () {
15 | Y.Assert.areEqual(C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString(), C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString());
16 | Y.Assert.areNotEqual(C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString(), C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB }).toString());
17 | },
18 |
19 | testDecrypt: function () {
20 | var encryptedA = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA });
21 | var encryptedB = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB });
22 | Y.Assert.areEqual('Test', C.AES.decrypt(encryptedA, 'Pass').toString(C.enc.Utf8));
23 | Y.Assert.areEqual('Test', C.AES.decrypt(encryptedB, 'Pass').toString(C.enc.Utf8));
24 | },
25 |
26 | testCustomKDFHasher: function () {
27 | //SHA1
28 | let encryptedSHA1 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA1}).toString();
29 | Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA1, 'Pass', { hasher: C.algo.SHA1}).toString(C.enc.Utf8));
30 |
31 | //SHA256
32 | let encryptedSHA256 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA256}).toString();
33 | Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA256, 'Pass', { hasher: C.algo.SHA256}).toString(C.enc.Utf8));
34 |
35 | //SHA512
36 | let encryptedSHA512 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA512}).toString();
37 | Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA512, 'Pass', { hasher: C.algo.SHA512}).toString(C.enc.Utf8));
38 |
39 | //Default: MD5
40 | let encryptedDefault = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString();
41 | let encryptedMD5 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.MD5}).toString();
42 | Y.Assert.areEqual('Test', C.AES.decrypt(encryptedMD5, 'Pass', { hasher: C.algo.MD5}).toString(C.enc.Utf8));
43 | Y.Assert.areEqual(encryptedDefault, encryptedMD5);
44 |
45 | //Different KDFHasher
46 | Y.Assert.areNotEqual(encryptedDefault, encryptedSHA1);
47 | Y.Assert.areNotEqual(encryptedDefault, encryptedSHA256);
48 | Y.Assert.areNotEqual(encryptedDefault, encryptedSHA512);
49 | }
50 | }));
51 | }, '$Rev$');
--------------------------------------------------------------------------------
/test/lib-cipherparams-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('lib-cipherparams-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'CipherParams',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.ciphertext = C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
11 | this.data.key = C.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
12 | this.data.iv = C.enc.Hex.parse('202122232425262728292a2b2c2d2e2f');
13 | this.data.salt = C.enc.Hex.parse('0123456789abcdef');
14 | this.data.algorithm = C.algo.AES;
15 | this.data.mode = C.mode.CBC;
16 | this.data.padding = C.pad.PKCS7;
17 | this.data.blockSize = this.data.algorithm.blockSize;
18 | this.data.formatter = C.format.OpenSSL;
19 |
20 | this.data.cipherParams = C.lib.CipherParams.create({
21 | ciphertext: this.data.ciphertext,
22 | key: this.data.key,
23 | iv: this.data.iv,
24 | salt: this.data.salt,
25 | algorithm: this.data.algorithm,
26 | mode: this.data.mode,
27 | padding: this.data.padding,
28 | blockSize: this.data.blockSize,
29 | formatter: this.data.formatter
30 | });
31 | },
32 |
33 | testInit: function () {
34 | Y.Assert.areEqual(this.data.ciphertext, this.data.cipherParams.ciphertext);
35 | Y.Assert.areEqual(this.data.key, this.data.cipherParams.key);
36 | Y.Assert.areEqual(this.data.iv, this.data.cipherParams.iv);
37 | Y.Assert.areEqual(this.data.salt, this.data.cipherParams.salt);
38 | Y.Assert.areEqual(this.data.algorithm, this.data.cipherParams.algorithm);
39 | Y.Assert.areEqual(this.data.mode, this.data.cipherParams.mode);
40 | Y.Assert.areEqual(this.data.padding, this.data.cipherParams.padding);
41 | Y.Assert.areEqual(this.data.blockSize, this.data.cipherParams.blockSize);
42 | Y.Assert.areEqual(this.data.formatter, this.data.cipherParams.formatter);
43 | },
44 |
45 | testToString0: function () {
46 | Y.Assert.areEqual(C.format.OpenSSL.stringify(this.data.cipherParams), this.data.cipherParams.toString());
47 | },
48 |
49 | testToString1: function () {
50 | var JsonFormatter = {
51 | stringify: function (cipherParams) {
52 | return '{ ct: ' + cipherParams.ciphertext + ', iv: ' + cipherParams.iv + ' }';
53 | }
54 | };
55 |
56 | Y.Assert.areEqual(JsonFormatter.stringify(this.data.cipherParams), this.data.cipherParams.toString(JsonFormatter));
57 | }
58 | }));
59 | }, '$Rev$');
60 |
--------------------------------------------------------------------------------
/test/hmac-md5-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-hmac-md5-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'HMAC MD5',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('9294727a3638bb1c13f48ef8158bfc9d', C.HmacMD5('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('750c783e6ab0b503eaa86e310a5db738', C.HmacMD5('what do ya want for nothing?', 'Jefe').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('56be34521d144c88dbb8c733f0e8b3f6', C.HmacMD5(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('7ee2a3cc979ab19865704644ce13355c', C.HmacMD5('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'A').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('0e1bd89c43e3e6e3b3f8cf1d5ba4f77a', C.HmacMD5('abcdefghijklmnopqrstuvwxyz', 'A').toString());
25 | },
26 |
27 | testUpdate: function () {
28 | var hmac = C.algo.HMAC.create(C.algo.MD5, C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'));
29 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddd'));
30 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
31 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
32 |
33 | Y.Assert.areEqual(C.HmacMD5(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString(), hmac.finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 | var key = C.lib.WordArray.create([0x12345678]);
39 |
40 | var expectedMessage = message.toString();
41 | var expectedKey = key.toString();
42 |
43 | C.HmacMD5(message, key);
44 |
45 | Y.Assert.areEqual(expectedMessage, message.toString());
46 | Y.Assert.areEqual(expectedKey, key.toString());
47 | },
48 |
49 | testRespectKeySigBytes: function () {
50 | var key = C.lib.WordArray.random(8);
51 | key.sigBytes = 4;
52 |
53 | var keyClamped = key.clone();
54 | keyClamped.clamp();
55 |
56 | Y.Assert.areEqual(CryptoJS.HmacSHA256("Message", keyClamped).toString(), CryptoJS.HmacSHA256("Message", key).toString());
57 | }
58 | }));
59 | }, '$Rev$');
60 |
--------------------------------------------------------------------------------
/test/md5-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-md5-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'MD5',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('d41d8cd98f00b204e9800998ecf8427e', C.MD5('').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('0cc175b9c0f1b6a831c399e269772661', C.MD5('a').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('900150983cd24fb0d6963f7d28e17f72', C.MD5('abc').toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('f96b697d7cb7938d525a2f31aaf161d0', C.MD5('message digest').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('c3fcd3d76192e4007dfb496cca67e13b', C.MD5('abcdefghijklmnopqrstuvwxyz').toString());
25 | },
26 |
27 | testVector6: function () {
28 | Y.Assert.areEqual('d174ab98d277d9f5a5611c2c9f419d9f', C.MD5('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').toString());
29 | },
30 |
31 | testVector7: function () {
32 | Y.Assert.areEqual('57edf4a22be3c955ac49da2e2107b67a', C.MD5('12345678901234567890123456789012345678901234567890123456789012345678901234567890').toString());
33 | },
34 |
35 | testUpdateAndLongMessage: function () {
36 | var md5 = C.algo.MD5.create();
37 | for (var i = 0; i < 100; i++) {
38 | md5.update('12345678901234567890123456789012345678901234567890');
39 | }
40 |
41 | Y.Assert.areEqual('7d017545e0268a6a12f2b507871d0429', md5.finalize().toString());
42 | },
43 |
44 | testClone: function () {
45 | var md5 = C.algo.MD5.create();
46 |
47 | Y.Assert.areEqual(C.MD5('a').toString(), md5.update('a').clone().finalize().toString());
48 | Y.Assert.areEqual(C.MD5('ab').toString(), md5.update('b').clone().finalize().toString());
49 | Y.Assert.areEqual(C.MD5('abc').toString(), md5.update('c').clone().finalize().toString());
50 | },
51 |
52 | testInputIntegrity: function () {
53 | var message = C.lib.WordArray.create([0x12345678]);
54 |
55 | var expected = message.toString();
56 |
57 | C.MD5(message);
58 |
59 | Y.Assert.areEqual(expected, message.toString());
60 | },
61 |
62 | testHelper: function () {
63 | Y.Assert.areEqual(C.algo.MD5.create().finalize('').toString(), C.MD5('').toString());
64 | },
65 |
66 | testHmacHelper: function () {
67 | Y.Assert.areEqual(C.algo.HMAC.create(C.algo.MD5, C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).finalize('Hi There').toString(), C.HmacMD5('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
68 | }
69 | }));
70 | }, '$Rev$');
71 |
--------------------------------------------------------------------------------
/test/hmac-sha224-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-hmac-sha224-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'HMAC SHA224',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('4e841ce7a4ae83fbcf71e3cd64bfbf277f73a14680aae8c518ac7861', C.HmacSHA224('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44', C.HmacSHA224('what do ya want for nothing?', 'Jefe').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('cbff7c2716bbaa7c77bed4f491d3e8456cb6c574e92f672b291acf5b', C.HmacSHA224(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('61bf669da4fdcd8e5c3bd09ebbb4a986d3d1b298d3ca05c511f7aeff', C.HmacSHA224('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'A').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('16fc69ada3c3edc1fe9144d6b98d93393833ae442bedf681110a1176', C.HmacSHA224('abcdefghijklmnopqrstuvwxyz', 'A').toString());
25 | },
26 |
27 | testUpdate: function () {
28 | var hmac = C.algo.HMAC.create(C.algo.SHA224, C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'));
29 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddd'));
30 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
31 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
32 |
33 | Y.Assert.areEqual(C.HmacSHA224(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString(), hmac.finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 | var key = C.lib.WordArray.create([0x12345678]);
39 |
40 | var expectedMessage = message.toString();
41 | var expectedKey = key.toString();
42 |
43 | C.HmacSHA224(message, key);
44 |
45 | Y.Assert.areEqual(expectedMessage, message.toString());
46 | Y.Assert.areEqual(expectedKey, key.toString());
47 | },
48 |
49 | testRespectKeySigBytes: function () {
50 | var key = C.lib.WordArray.random(8);
51 | key.sigBytes = 4;
52 |
53 | var keyClamped = key.clone();
54 | keyClamped.clamp();
55 |
56 | Y.Assert.areEqual(CryptoJS.HmacSHA224("Message", keyClamped).toString(), CryptoJS.HmacSHA224("Message", key).toString());
57 | }
58 | }));
59 | }, '$Rev$');
60 |
--------------------------------------------------------------------------------
/test/hmac-sha256-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-hmac-sha256-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'HMAC SHA256',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('492ce020fe2534a5789dc3848806c78f4f6711397f08e7e7a12ca5a4483c8aa6', C.HmacSHA256('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843', C.HmacSHA256('what do ya want for nothing?', 'Jefe').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('7dda3cc169743a6484649f94f0eda0f9f2ff496a9733fb796ed5adb40a44c3c1', C.HmacSHA256(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('a89dc8178c1184a62df87adaa77bf86e93064863d93c5131140b0ae98b866687', C.HmacSHA256('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'A').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('d8cb78419c02fe20b90f8b77427dd9f81817a751d74c2e484e0ac5fc4e6ca986', C.HmacSHA256('abcdefghijklmnopqrstuvwxyz', 'A').toString());
25 | },
26 |
27 | testUpdate: function () {
28 | var hmac = C.algo.HMAC.create(C.algo.SHA256, C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'));
29 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddd'));
30 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
31 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
32 |
33 | Y.Assert.areEqual(C.HmacSHA256(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString(), hmac.finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 | var key = C.lib.WordArray.create([0x12345678]);
39 |
40 | var expectedMessage = message.toString();
41 | var expectedKey = key.toString();
42 |
43 | C.HmacSHA256(message, key);
44 |
45 | Y.Assert.areEqual(expectedMessage, message.toString());
46 | Y.Assert.areEqual(expectedKey, key.toString());
47 | },
48 |
49 | testRespectKeySigBytes: function () {
50 | var key = C.lib.WordArray.random(8);
51 | key.sigBytes = 4;
52 |
53 | var keyClamped = key.clone();
54 | keyClamped.clamp();
55 |
56 | Y.Assert.areEqual(CryptoJS.HmacSHA256("Message", keyClamped).toString(), CryptoJS.HmacSHA256("Message", key).toString());
57 | }
58 | }));
59 | }, '$Rev$');
60 |
--------------------------------------------------------------------------------
/test/sha1-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha1-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SHA1',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('da39a3ee5e6b4b0d3255bfef95601890afd80709', C.SHA1('').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('86f7e437faa5a7fce15d1ddcb9eaeaea377667b8', C.SHA1('a').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('a9993e364706816aba3e25717850c26c9cd0d89d', C.SHA1('abc').toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('c12252ceda8be8994d5fa0290a47231c1d16aae3', C.SHA1('message digest').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('32d10c7b8cf96570ca04ce37f2a19d84240d3a89', C.SHA1('abcdefghijklmnopqrstuvwxyz').toString());
25 | },
26 |
27 | testVector6: function () {
28 | Y.Assert.areEqual('761c457bf73b14d27e9e9265c46f4b4dda11f940', C.SHA1('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').toString());
29 | },
30 |
31 | testVector7: function () {
32 | Y.Assert.areEqual('50abf5706a150990a08b2c5ea40fa0e585554732', C.SHA1('12345678901234567890123456789012345678901234567890123456789012345678901234567890').toString());
33 | },
34 |
35 | testUpdateAndLongMessage: function () {
36 | var sha1 = C.algo.SHA1.create();
37 | for (var i = 0; i < 100; i++) {
38 | sha1.update('12345678901234567890123456789012345678901234567890');
39 | }
40 |
41 | Y.Assert.areEqual('85e4c4b3933d5553ebf82090409a9d90226d845c', sha1.finalize().toString());
42 | },
43 |
44 | testClone: function () {
45 | var sha1 = C.algo.SHA1.create();
46 |
47 | Y.Assert.areEqual(C.SHA1('a').toString(), sha1.update('a').clone().finalize().toString());
48 | Y.Assert.areEqual(C.SHA1('ab').toString(), sha1.update('b').clone().finalize().toString());
49 | Y.Assert.areEqual(C.SHA1('abc').toString(), sha1.update('c').clone().finalize().toString());
50 | },
51 |
52 | testInputIntegrity: function () {
53 | var message = C.lib.WordArray.create([0x12345678]);
54 |
55 | var expected = message.toString();
56 |
57 | C.SHA1(message);
58 |
59 | Y.Assert.areEqual(expected, message.toString());
60 | },
61 |
62 | testHelper: function () {
63 | Y.Assert.areEqual(C.algo.SHA1.create().finalize('').toString(), C.SHA1('').toString());
64 | },
65 |
66 | testHmacHelper: function () {
67 | Y.Assert.areEqual(C.algo.HMAC.create(C.algo.SHA1, C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).finalize('Hi There').toString(), C.HmacSHA1('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
68 | }
69 | }));
70 | }, '$Rev$');
71 |
--------------------------------------------------------------------------------
/test/hmac-sha384-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-hmac-sha384-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'HMAC SHA384',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('7afaa633e20d379b02395915fbc385ff8dc27dcd3885e1068ab942eeab52ec1f20ad382a92370d8b2e0ac8b83c4d53bf', C.HmacSHA384('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec3736322445e8e2240ca5e69e2c78b3239ecfab21649', C.HmacSHA384('what do ya want for nothing?', 'Jefe').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('1383e82e28286b91f4cc7afbd13d5b5c6f887c05e7c4542484043a37a5fe45802a9470fb663bd7b6570fe2f503fc92f5', C.HmacSHA384(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('365dfb271adb8e30fe6c74220b75df1b38c2d19b9d37f2e5a0ec2f3f22bd0406bf5b786e98d81b82c36d3d8a1be6cd07', C.HmacSHA384('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'A').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('a8357d5e84da64140e41545562ae0782e2a58e39c6cd98939fad8d9080e774c84b7eaca4ba07f6dbf0f12eab912c5285', C.HmacSHA384('abcdefghijklmnopqrstuvwxyz', 'A').toString());
25 | },
26 |
27 | testUpdate: function () {
28 | var hmac = C.algo.HMAC.create(C.algo.SHA384, C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'));
29 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddd'));
30 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
31 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
32 |
33 | Y.Assert.areEqual(C.HmacSHA384(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString(), hmac.finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 | var key = C.lib.WordArray.create([0x12345678]);
39 |
40 | var expectedMessage = message.toString();
41 | var expectedKey = key.toString();
42 |
43 | C.HmacSHA384(message, key);
44 |
45 | Y.Assert.areEqual(expectedMessage, message.toString());
46 | Y.Assert.areEqual(expectedKey, key.toString());
47 | },
48 |
49 | testRespectKeySigBytes: function () {
50 | var key = C.lib.WordArray.random(8);
51 | key.sigBytes = 4;
52 |
53 | var keyClamped = key.clone();
54 | keyClamped.clamp();
55 |
56 | Y.Assert.areEqual(CryptoJS.HmacSHA384("Message", keyClamped).toString(), CryptoJS.HmacSHA384("Message", key).toString());
57 | }
58 | }));
59 | }, '$Rev$');
60 |
--------------------------------------------------------------------------------
/test/rc4-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-rc4-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'RC4',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('7494c2e7104b0879', C.RC4.encrypt(C.enc.Hex.parse('0000000000000000'), C.enc.Hex.parse('0123456789abcdef')).ciphertext.toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('f13829c9de', C.RC4.encrypt(C.enc.Hex.parse('dcee4cf92c'), C.enc.Hex.parse('618a63d2fb')).ciphertext.toString());
13 | },
14 |
15 | testDrop: function () {
16 | Y.Assert.areEqual(
17 | C.RC4.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('0123456789abcdef')).ciphertext.toString().substr(16),
18 | C.RC4Drop.encrypt(C.enc.Hex.parse('0000000000000000'), C.enc.Hex.parse('0123456789abcdef'), { drop: 2 }).ciphertext.toString()
19 | );
20 | },
21 |
22 | testMultiPart: function () {
23 | var rc4 = C.algo.RC4.createEncryptor(C.enc.Hex.parse('0123456789abcdef'));
24 | var ciphertext1 = rc4.process(C.enc.Hex.parse('00000000'));
25 | var ciphertext2 = rc4.process(C.enc.Hex.parse('0000'));
26 | var ciphertext3 = rc4.process(C.enc.Hex.parse('0000'));
27 | var ciphertext4 = rc4.finalize();
28 |
29 | Y.Assert.areEqual('7494c2e7104b0879', ciphertext1.concat(ciphertext2).concat(ciphertext3).concat(ciphertext4).toString());
30 | },
31 |
32 | testInputIntegrity: function () {
33 | var message = C.enc.Hex.parse('0000000000000000');
34 | var key = C.enc.Hex.parse('0123456789abcdef');
35 |
36 | var expectedMessage = message.toString();
37 | var expectedKey = key.toString();
38 |
39 | C.RC4.encrypt(message, key);
40 |
41 | Y.Assert.areEqual(expectedMessage, message.toString());
42 | Y.Assert.areEqual(expectedKey, key.toString());
43 | },
44 |
45 | testHelper: function () {
46 | // Save original random method
47 | var random = C.lib.WordArray.random;
48 |
49 | // Replace random method with one that returns a predictable value
50 | C.lib.WordArray.random = function (nBytes) {
51 | var words = [];
52 | for (var i = 0; i < nBytes; i += 4) {
53 | words.push([0x11223344]);
54 | }
55 |
56 | return C.lib.WordArray.create(words, nBytes);
57 | };
58 |
59 | // Test
60 | Y.Assert.areEqual(C.algo.RC4.createEncryptor(C.SHA256('Jefe')).finalize('Hi There').toString(), C.RC4.encrypt('Hi There', C.SHA256('Jefe')).ciphertext.toString());
61 | Y.Assert.areEqual(C.lib.SerializableCipher.encrypt(C.algo.RC4, 'Hi There', C.SHA256('Jefe')).toString(), C.RC4.encrypt('Hi There', C.SHA256('Jefe')).toString());
62 | Y.Assert.areEqual(C.lib.PasswordBasedCipher.encrypt(C.algo.RC4, 'Hi There', 'Jefe').toString(), C.RC4.encrypt('Hi There', 'Jefe').toString());
63 |
64 | // Restore random method
65 | C.lib.WordArray.random = random;
66 | }
67 | }));
68 | }, '$Rev$');
69 |
--------------------------------------------------------------------------------
/test/enc-base64-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('enc-base64-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Base64',
6 |
7 | testStringify0: function () {
8 | Y.Assert.areEqual('', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 0)));
9 | },
10 |
11 | testStringify1: function () {
12 | Y.Assert.areEqual('Zg==', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 1)));
13 | },
14 |
15 | testStringify2: function () {
16 | Y.Assert.areEqual('Zm8=', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 2)));
17 | },
18 |
19 | testStringify3: function () {
20 | Y.Assert.areEqual('Zm9v', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 3)));
21 | },
22 |
23 | testStringify4: function () {
24 | Y.Assert.areEqual('Zm9vYg==', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 4)));
25 | },
26 |
27 | testStringify5: function () {
28 | Y.Assert.areEqual('Zm9vYmE=', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 5)));
29 | },
30 |
31 | testStringify6: function () {
32 | Y.Assert.areEqual('Zm9vYmFy', C.enc.Base64.stringify(C.lib.WordArray.create([0x666f6f62, 0x61720000], 6)));
33 | },
34 |
35 | testStringify15: function () {
36 | Y.Assert.areEqual('Pj4+Pz8/Pj4+Pz8/PS8r', C.enc.Base64.stringify(C.lib.WordArray.create([0x3e3e3e3f, 0x3f3f3e3e, 0x3e3f3f3f, 0x3d2f2b00], 15)));
37 | },
38 |
39 | testParse0: function () {
40 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 0).toString(), C.enc.Base64.parse('').toString());
41 | },
42 |
43 | testParse1: function () {
44 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 1).toString(), C.enc.Base64.parse('Zg==').toString());
45 | },
46 |
47 | testParse2: function () {
48 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 2).toString(), C.enc.Base64.parse('Zm8=').toString());
49 | },
50 |
51 | testParse3: function () {
52 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 3).toString(), C.enc.Base64.parse('Zm9v').toString());
53 | },
54 |
55 | testParse4: function () {
56 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 4).toString(), C.enc.Base64.parse('Zm9vYg==').toString());
57 | },
58 |
59 | testParse5: function () {
60 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 5).toString(), C.enc.Base64.parse('Zm9vYmE=').toString());
61 | },
62 |
63 | testParse6: function () {
64 | Y.Assert.areEqual(C.lib.WordArray.create([0x666f6f62, 0x61720000], 6).toString(), C.enc.Base64.parse('Zm9vYmFy').toString());
65 | },
66 |
67 | testParse15: function () {
68 | Y.Assert.areEqual(C.lib.WordArray.create([0x3e3e3e3f, 0x3f3f3e3e, 0x3e3f3f3f, 0x3d2f2b00], 15).toString(), C.enc.Base64.parse('Pj4+Pz8/Pj4+Pz8/PS8r').toString());
69 | }
70 | }));
71 | }, '$Rev$');
72 |
--------------------------------------------------------------------------------
/test/sha256-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha256-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SHA256',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', C.SHA256('').toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb', C.SHA256('a').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad', C.SHA256('abc').toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650', C.SHA256('message digest').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73', C.SHA256('abcdefghijklmnopqrstuvwxyz').toString());
25 | },
26 |
27 | testVector6: function () {
28 | Y.Assert.areEqual('db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0', C.SHA256('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789').toString());
29 | },
30 |
31 | testVector7: function () {
32 | Y.Assert.areEqual('f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e', C.SHA256('12345678901234567890123456789012345678901234567890123456789012345678901234567890').toString());
33 | },
34 |
35 | testUpdateAndLongMessage: function () {
36 | var sha256 = C.algo.SHA256.create();
37 | for (var i = 0; i < 100; i++) {
38 | sha256.update('12345678901234567890123456789012345678901234567890');
39 | }
40 |
41 | Y.Assert.areEqual('f8146961d9b73d8da49ccd526fca65439cdd5b402f76971556d5f52fd129843e', sha256.finalize().toString());
42 | },
43 |
44 | testClone: function () {
45 | var sha256 = C.algo.SHA256.create();
46 |
47 | Y.Assert.areEqual(C.SHA256('a').toString(), sha256.update('a').clone().finalize().toString());
48 | Y.Assert.areEqual(C.SHA256('ab').toString(), sha256.update('b').clone().finalize().toString());
49 | Y.Assert.areEqual(C.SHA256('abc').toString(), sha256.update('c').clone().finalize().toString());
50 | },
51 |
52 | testInputIntegrity: function () {
53 | var message = C.lib.WordArray.create([0x12345678]);
54 |
55 | var expected = message.toString();
56 |
57 | C.SHA256(message);
58 |
59 | Y.Assert.areEqual(expected, message.toString());
60 | },
61 |
62 | testHelper: function () {
63 | Y.Assert.areEqual(C.algo.SHA256.create().finalize('').toString(), C.SHA256('').toString());
64 | },
65 |
66 | testHmacHelper: function () {
67 | Y.Assert.areEqual(C.algo.HMAC.create(C.algo.SHA256, C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).finalize('Hi There').toString(), C.HmacSHA256('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
68 | }
69 | }));
70 | }, '$Rev$');
71 |
--------------------------------------------------------------------------------
/grunt/tasks/modularize.js:
--------------------------------------------------------------------------------
1 | /*jshint node: true*/
2 | /*jshint esversion: 6*/
3 |
4 | var _ = require("lodash"),
5 |
6 | fmd = require("fmd");
7 |
8 | module.exports = function (grunt) {
9 |
10 | grunt.registerMultiTask('modularize', function () {
11 | var options = this.options(),
12 |
13 | done = this.async(),
14 |
15 | modules = {},
16 |
17 | config = {
18 | target: this.target + '/',
19 | factories: ["commonjs", "amd", "global"],
20 | trim_whitespace: true,
21 | new_line: "unix",
22 | indent: "\t"
23 | };
24 |
25 | // Prepare Factory-Module-Definition settings
26 | _.each(options, (conf, name) => {
27 | var sources = [],
28 |
29 | opts = {
30 | depends: {}
31 | },
32 |
33 | deps = [];
34 |
35 | if (conf.exports) {
36 | opts.exports = conf.exports;
37 | }
38 |
39 | if (conf.global) {
40 | opts.global = conf.global;
41 | }
42 |
43 | // Find and add self as source
44 | _.each(this.filesSrc, (source) => {
45 | if (grunt.file.exists(source + name + ".js")) {
46 | sources.push(source + name + ".js");
47 | }
48 | });
49 |
50 | if (conf.pack) {
51 | // Collect all components
52 | deps = _.chain(conf.components)
53 | .map(depName => options[depName].components)
54 | .flatten()
55 | .uniq()
56 | .without(name)
57 | .sort((a, b) => {
58 | if (options[a].components.includes(b)) {
59 | return 1
60 | }
61 |
62 | if (options[b].components.includes(a)) {
63 | return -1
64 | }
65 |
66 | return 0;
67 | })
68 | .value();
69 |
70 | // Add components as source files -> results a single file
71 | _.each(this.filesSrc, (source) => {
72 | _.each(deps, (depName) => {
73 | if (grunt.file.exists(source + depName + ".js")) {
74 | sources.push(source + depName + ".js");
75 | }
76 | });
77 | });
78 | } else {
79 | // Read components and add them as dependecies
80 | _.each(_.without(conf.components, name), (value, i) => {
81 | opts.depends['./' + value] = value === "core" ? "CryptoJS" : null;
82 | });
83 | }
84 |
85 | // Remove duplicates
86 | sources = _.uniq(sources);
87 |
88 | // Add module settings to fmd definition
89 | modules[name] = [sources, opts];
90 | });
91 |
92 | // Build packege modules
93 | fmd(config)
94 | .define(modules)
95 | .build(() => {
96 |
97 | done();
98 | });
99 |
100 | });
101 |
102 | };
103 |
--------------------------------------------------------------------------------
/test/hmac-sha512-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-hmac-sha512-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'HMAC SHA512',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('7641c48a3b4aa8f887c07b3e83f96affb89c978fed8c96fcbbf4ad596eebfe496f9f16da6cd080ba393c6f365ad72b50d15c71bfb1d6b81f66a911786c6ce932', C.HmacSHA512('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b636e070a38bce737', C.HmacSHA512('what do ya want for nothing?', 'Jefe').toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('ad9b5c7de72693737cd5e9d9f41170d18841fec1201c1c1b02e05cae116718009f771cad9946ddbf7e3cde3e818d9ae85d91b2badae94172d096a44a79c91e86', C.HmacSHA512(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('a303979f7c94bb39a8ab6ce05cdbe28f0255da8bb305263e3478ef7e855f0242729bf1d2be55398f14da8e63f0302465a8a3f76c297bd584ad028d18ed7f0195', C.HmacSHA512('ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'A').toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('8c2d56f7628325e62124c0a870ad98d101327fc42696899a06ce0d7121454022fae597e42c25ac3a4c380fd514f553702a5b0afaa9b5a22050902f024368e9d9', C.HmacSHA512('abcdefghijklmnopqrstuvwxyz', 'A').toString());
25 | },
26 |
27 | testUpdate: function () {
28 | var hmac = C.algo.HMAC.create(C.algo.SHA512, C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'));
29 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddd'));
30 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
31 | hmac.update(C.enc.Hex.parse('dddddddddddddddddddddddddddddddd'));
32 |
33 | Y.Assert.areEqual(C.HmacSHA512(C.enc.Hex.parse('dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'), C.enc.Hex.parse('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')).toString(), hmac.finalize().toString());
34 | },
35 |
36 | testInputIntegrity: function () {
37 | var message = C.lib.WordArray.create([0x12345678]);
38 | var key = C.lib.WordArray.create([0x12345678]);
39 |
40 | var expectedMessage = message.toString();
41 | var expectedKey = key.toString();
42 |
43 | C.HmacSHA512(message, key);
44 |
45 | Y.Assert.areEqual(expectedMessage, message.toString());
46 | Y.Assert.areEqual(expectedKey, key.toString());
47 | },
48 |
49 | testRespectKeySigBytes: function () {
50 | var key = C.lib.WordArray.random(8);
51 | key.sigBytes = 4;
52 |
53 | var keyClamped = key.clone();
54 | keyClamped.clamp();
55 |
56 | Y.Assert.areEqual(CryptoJS.HmacSHA512("Message", keyClamped).toString(), CryptoJS.HmacSHA512("Message", key).toString());
57 | }
58 | }));
59 | }, '$Rev$');
60 |
--------------------------------------------------------------------------------
/test/lib-base-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('lib-base-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Base',
6 |
7 | setUp: function () {
8 | this.data = {};
9 |
10 | this.data.overrides = {
11 | init: function (arg) {
12 | this.initFired = true;
13 | this.initArg = arg;
14 | },
15 |
16 | toString: function () {
17 | }
18 | };
19 |
20 | this.data.mixins = {
21 | mixinMethod: function () {
22 | }
23 | };
24 |
25 | this.data.Obj = C.lib.Base.extend(this.data.overrides);
26 |
27 | this.data.Obj.mixIn(this.data.mixins);
28 |
29 | this.data.obj = this.data.Obj.create('argValue');
30 |
31 | this.data.objClone = this.data.obj.clone();
32 | },
33 |
34 | testExtendInheritance: function () {
35 | Y.Assert.areEqual(C.lib.Base.extend, this.data.Obj.extend);
36 | Y.Assert.isFalse(this.data.Obj.hasOwnProperty('extend'));
37 | },
38 |
39 | testExtendSuper: function () {
40 | Y.Assert.areEqual(C.lib.Base, this.data.Obj.$super);
41 | },
42 |
43 | testExtendOverrideInit: function () {
44 | Y.Assert.areEqual(this.data.overrides.init, this.data.Obj.init);
45 | Y.Assert.isTrue(this.data.Obj.hasOwnProperty('init'));
46 | },
47 |
48 | testExtendOverrideToString: function () {
49 | Y.Assert.areEqual(this.data.overrides.toString, this.data.Obj.toString);
50 | Y.Assert.isTrue(this.data.Obj.hasOwnProperty('toString'));
51 | },
52 |
53 | testCreateInheritanceFromBase: function () {
54 | Y.Assert.areEqual(C.lib.Base.extend, this.data.obj.extend);
55 | Y.Assert.isFalse(this.data.obj.hasOwnProperty('extend'));
56 | },
57 |
58 | testCreateSuper: function () {
59 | Y.Assert.areEqual(this.data.Obj, this.data.obj.$super);
60 | },
61 |
62 | testCreateInit: function () {
63 | Y.Assert.isTrue(this.data.obj.initFired);
64 | Y.Assert.areEqual('argValue', this.data.obj.initArg);
65 | },
66 |
67 | testMixIn: function () {
68 | Y.Assert.areEqual(this.data.mixins.mixinMethod, this.data.Obj.mixinMethod);
69 | Y.Assert.isTrue(this.data.Obj.hasOwnProperty('mixinMethod'));
70 | },
71 |
72 | testCloneDistinct: function () {
73 | Y.Assert.areNotEqual(this.data.obj, this.data.objClone);
74 | },
75 |
76 | testCloneCopy: function () {
77 | Y.Assert.areEqual(this.data.obj.initArg, this.data.objClone.initArg);
78 | },
79 |
80 | testCloneIndependent: function () {
81 | this.data.obj.initArg = 'newValue';
82 |
83 | Y.Assert.areNotEqual(this.data.obj.initArg, this.data.objClone.initArg);
84 | },
85 |
86 | testCloneLeavesOriginalInitPrototypeUnchanged: function() {
87 | Y.Assert.areEqual(this.data.obj, this.data.obj.init.prototype);
88 | Y.Assert.areEqual(this.data.objClone, this.data.objClone.init.prototype);
89 | Y.Assert.areNotEqual(this.data.obj.init.prototype, this.data.objClone.init.prototype);
90 | }
91 | }));
92 | }, '$Rev$');
93 |
--------------------------------------------------------------------------------
/test/lib-wordarray-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('lib-wordarray-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'WordArray',
6 |
7 | testInit0: function () {
8 | Y.Assert.areEqual('', C.lib.WordArray.create().toString());
9 | },
10 |
11 | testInit1: function () {
12 | Y.Assert.areEqual('12345678', C.lib.WordArray.create([0x12345678]).toString());
13 | },
14 |
15 | testInit2: function () {
16 | Y.Assert.areEqual('1234', C.lib.WordArray.create([0x12345678], 2).toString());
17 | },
18 |
19 | testToStringPassedEncoder: function () {
20 | Y.Assert.areEqual('\x12\x34\x56\x78', C.lib.WordArray.create([0x12345678]).toString(C.enc.Latin1));
21 | },
22 |
23 | testToStringDefaultEncoder: function () {
24 | Y.Assert.areEqual('12345678', C.lib.WordArray.create([0x12345678]).toString());
25 | },
26 |
27 | testConcat3: function () {
28 | var wordArray1 = C.lib.WordArray.create([0x12345678], 3);
29 | var wordArray2 = C.lib.WordArray.create([0x12345678], 3);
30 |
31 | Y.Assert.areEqual('123456123456', wordArray1.concat(wordArray2).toString());
32 | Y.Assert.areEqual('123456123456', wordArray1.toString());
33 | },
34 |
35 | testConcat4: function () {
36 | var wordArray1 = C.lib.WordArray.create([0x12345678], 4);
37 | var wordArray2 = C.lib.WordArray.create([0x12345678], 3);
38 |
39 | Y.Assert.areEqual('12345678123456', wordArray1.concat(wordArray2).toString());
40 | Y.Assert.areEqual('12345678123456', wordArray1.toString());
41 | },
42 |
43 | testConcat5: function () {
44 | var wordArray1 = C.lib.WordArray.create([0x12345678], 5);
45 | var wordArray2 = C.lib.WordArray.create([0x12345678], 3);
46 |
47 | Y.Assert.areEqual('1234567800123456', wordArray1.concat(wordArray2).toString());
48 | Y.Assert.areEqual('1234567800123456', wordArray1.toString());
49 | },
50 |
51 | testConcatLong: function () {
52 | var wordArray1 = C.lib.WordArray.create();
53 |
54 | var wordArray2 = C.lib.WordArray.create();
55 | var wordArray3 = C.lib.WordArray.create();
56 | for (var i = 0; i < 500000; i++) {
57 | wordArray2.words[i] = i;
58 | wordArray3.words[i] = i;
59 | }
60 | wordArray2.sigBytes = wordArray3.sigBytes = 500000;
61 |
62 | Y.Assert.areEqual(wordArray2.toString() + wordArray3.toString(), wordArray1.concat(wordArray2.concat(wordArray3)).toString());
63 | },
64 |
65 | testClamp: function () {
66 | var wordArray = C.lib.WordArray.create([0x12345678, 0x12345678], 3);
67 | wordArray.clamp();
68 |
69 | Y.Assert.areEqual([0x12345600].toString(), wordArray.words.toString());
70 | },
71 |
72 | testClone: function () {
73 | var wordArray = C.lib.WordArray.create([0x12345678]);
74 | var clone = wordArray.clone();
75 | clone.words[0] = 0;
76 |
77 | Y.Assert.areNotEqual(wordArray.toString(), clone.toString());
78 | },
79 |
80 | testRandom: function () {
81 | Y.Assert.areNotEqual(C.lib.WordArray.random(8).toString(), C.lib.WordArray.random(8).toString());
82 | Y.Assert.areEqual(8, C.lib.WordArray.random(8).sigBytes);
83 | }
84 | }));
85 | }, '$Rev$');
86 |
--------------------------------------------------------------------------------
/src/rc4.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var StreamCipher = C_lib.StreamCipher;
6 | var C_algo = C.algo;
7 |
8 | /**
9 | * RC4 stream cipher algorithm.
10 | */
11 | var RC4 = C_algo.RC4 = StreamCipher.extend({
12 | _doReset: function () {
13 | // Shortcuts
14 | var key = this._key;
15 | var keyWords = key.words;
16 | var keySigBytes = key.sigBytes;
17 |
18 | // Init sbox
19 | var S = this._S = [];
20 | for (var i = 0; i < 256; i++) {
21 | S[i] = i;
22 | }
23 |
24 | // Key setup
25 | for (var i = 0, j = 0; i < 256; i++) {
26 | var keyByteIndex = i % keySigBytes;
27 | var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
28 |
29 | j = (j + S[i] + keyByte) % 256;
30 |
31 | // Swap
32 | var t = S[i];
33 | S[i] = S[j];
34 | S[j] = t;
35 | }
36 |
37 | // Counters
38 | this._i = this._j = 0;
39 | },
40 |
41 | _doProcessBlock: function (M, offset) {
42 | M[offset] ^= generateKeystreamWord.call(this);
43 | },
44 |
45 | keySize: 256/32,
46 |
47 | ivSize: 0
48 | });
49 |
50 | function generateKeystreamWord() {
51 | // Shortcuts
52 | var S = this._S;
53 | var i = this._i;
54 | var j = this._j;
55 |
56 | // Generate keystream word
57 | var keystreamWord = 0;
58 | for (var n = 0; n < 4; n++) {
59 | i = (i + 1) % 256;
60 | j = (j + S[i]) % 256;
61 |
62 | // Swap
63 | var t = S[i];
64 | S[i] = S[j];
65 | S[j] = t;
66 |
67 | keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
68 | }
69 |
70 | // Update counters
71 | this._i = i;
72 | this._j = j;
73 |
74 | return keystreamWord;
75 | }
76 |
77 | /**
78 | * Shortcut functions to the cipher's object interface.
79 | *
80 | * @example
81 | *
82 | * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
83 | * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
84 | */
85 | C.RC4 = StreamCipher._createHelper(RC4);
86 |
87 | /**
88 | * Modified RC4 stream cipher algorithm.
89 | */
90 | var RC4Drop = C_algo.RC4Drop = RC4.extend({
91 | /**
92 | * Configuration options.
93 | *
94 | * @property {number} drop The number of keystream words to drop. Default 192
95 | */
96 | cfg: RC4.cfg.extend({
97 | drop: 192
98 | }),
99 |
100 | _doReset: function () {
101 | RC4._doReset.call(this);
102 |
103 | // Drop
104 | for (var i = this.cfg.drop; i > 0; i--) {
105 | generateKeystreamWord.call(this);
106 | }
107 | }
108 | });
109 |
110 | /**
111 | * Shortcut functions to the cipher's object interface.
112 | *
113 | * @example
114 | *
115 | * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
116 | * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
117 | */
118 | C.RC4Drop = StreamCipher._createHelper(RC4Drop);
119 | }());
120 |
--------------------------------------------------------------------------------
/src/evpkdf.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var Base = C_lib.Base;
6 | var WordArray = C_lib.WordArray;
7 | var C_algo = C.algo;
8 | var MD5 = C_algo.MD5;
9 |
10 | /**
11 | * This key derivation function is meant to conform with EVP_BytesToKey.
12 | * www.openssl.org/docs/crypto/EVP_BytesToKey.html
13 | */
14 | var EvpKDF = C_algo.EvpKDF = Base.extend({
15 | /**
16 | * Configuration options.
17 | *
18 | * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
19 | * @property {Hasher} hasher The hash algorithm to use. Default: MD5
20 | * @property {number} iterations The number of iterations to perform. Default: 1
21 | */
22 | cfg: Base.extend({
23 | keySize: 128/32,
24 | hasher: MD5,
25 | iterations: 1
26 | }),
27 |
28 | /**
29 | * Initializes a newly created key derivation function.
30 | *
31 | * @param {Object} cfg (Optional) The configuration options to use for the derivation.
32 | *
33 | * @example
34 | *
35 | * var kdf = CryptoJS.algo.EvpKDF.create();
36 | * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
37 | * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
38 | */
39 | init: function (cfg) {
40 | this.cfg = this.cfg.extend(cfg);
41 | },
42 |
43 | /**
44 | * Derives a key from a password.
45 | *
46 | * @param {WordArray|string} password The password.
47 | * @param {WordArray|string} salt A salt.
48 | *
49 | * @return {WordArray} The derived key.
50 | *
51 | * @example
52 | *
53 | * var key = kdf.compute(password, salt);
54 | */
55 | compute: function (password, salt) {
56 | var block;
57 |
58 | // Shortcut
59 | var cfg = this.cfg;
60 |
61 | // Init hasher
62 | var hasher = cfg.hasher.create();
63 |
64 | // Initial values
65 | var derivedKey = WordArray.create();
66 |
67 | // Shortcuts
68 | var derivedKeyWords = derivedKey.words;
69 | var keySize = cfg.keySize;
70 | var iterations = cfg.iterations;
71 |
72 | // Generate key
73 | while (derivedKeyWords.length < keySize) {
74 | if (block) {
75 | hasher.update(block);
76 | }
77 | block = hasher.update(password).finalize(salt);
78 | hasher.reset();
79 |
80 | // Iterations
81 | for (var i = 1; i < iterations; i++) {
82 | block = hasher.finalize(block);
83 | hasher.reset();
84 | }
85 |
86 | derivedKey.concat(block);
87 | }
88 | derivedKey.sigBytes = keySize * 4;
89 |
90 | return derivedKey;
91 | }
92 | });
93 |
94 | /**
95 | * Derives a key from a password.
96 | *
97 | * @param {WordArray|string} password The password.
98 | * @param {WordArray|string} salt A salt.
99 | * @param {Object} cfg (Optional) The configuration options to use for this computation.
100 | *
101 | * @return {WordArray} The derived key.
102 | *
103 | * @static
104 | *
105 | * @example
106 | *
107 | * var key = CryptoJS.EvpKDF(password, salt);
108 | * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
109 | * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
110 | */
111 | C.EvpKDF = function (password, salt, cfg) {
112 | return EvpKDF.create(cfg).compute(password, salt);
113 | };
114 | }());
115 |
--------------------------------------------------------------------------------
/src/enc-base64.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var WordArray = C_lib.WordArray;
6 | var C_enc = C.enc;
7 |
8 | /**
9 | * Base64 encoding strategy.
10 | */
11 | var Base64 = C_enc.Base64 = {
12 | /**
13 | * Converts a word array to a Base64 string.
14 | *
15 | * @param {WordArray} wordArray The word array.
16 | *
17 | * @return {string} The Base64 string.
18 | *
19 | * @static
20 | *
21 | * @example
22 | *
23 | * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
24 | */
25 | stringify: function (wordArray) {
26 | // Shortcuts
27 | var words = wordArray.words;
28 | var sigBytes = wordArray.sigBytes;
29 | var map = this._map;
30 |
31 | // Clamp excess bits
32 | wordArray.clamp();
33 |
34 | // Convert
35 | var base64Chars = [];
36 | for (var i = 0; i < sigBytes; i += 3) {
37 | var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
38 | var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
39 | var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
40 |
41 | var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
42 |
43 | for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
44 | base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
45 | }
46 | }
47 |
48 | // Add padding
49 | var paddingChar = map.charAt(64);
50 | if (paddingChar) {
51 | while (base64Chars.length % 4) {
52 | base64Chars.push(paddingChar);
53 | }
54 | }
55 |
56 | return base64Chars.join('');
57 | },
58 |
59 | /**
60 | * Converts a Base64 string to a word array.
61 | *
62 | * @param {string} base64Str The Base64 string.
63 | *
64 | * @return {WordArray} The word array.
65 | *
66 | * @static
67 | *
68 | * @example
69 | *
70 | * var wordArray = CryptoJS.enc.Base64.parse(base64String);
71 | */
72 | parse: function (base64Str) {
73 | // Shortcuts
74 | var base64StrLength = base64Str.length;
75 | var map = this._map;
76 | var reverseMap = this._reverseMap;
77 |
78 | if (!reverseMap) {
79 | reverseMap = this._reverseMap = [];
80 | for (var j = 0; j < map.length; j++) {
81 | reverseMap[map.charCodeAt(j)] = j;
82 | }
83 | }
84 |
85 | // Ignore padding
86 | var paddingChar = map.charAt(64);
87 | if (paddingChar) {
88 | var paddingIndex = base64Str.indexOf(paddingChar);
89 | if (paddingIndex !== -1) {
90 | base64StrLength = paddingIndex;
91 | }
92 | }
93 |
94 | // Convert
95 | return parseLoop(base64Str, base64StrLength, reverseMap);
96 |
97 | },
98 |
99 | _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
100 | };
101 |
102 | function parseLoop(base64Str, base64StrLength, reverseMap) {
103 | var words = [];
104 | var nBytes = 0;
105 | for (var i = 0; i < base64StrLength; i++) {
106 | if (i % 4) {
107 | var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
108 | var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
109 | var bitsCombined = bits1 | bits2;
110 | words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
111 | nBytes++;
112 | }
113 | }
114 | return WordArray.create(words, nBytes);
115 | }
116 | }());
117 |
--------------------------------------------------------------------------------
/src/enc-utf16.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var WordArray = C_lib.WordArray;
6 | var C_enc = C.enc;
7 |
8 | /**
9 | * UTF-16 BE encoding strategy.
10 | */
11 | var Utf16BE = C_enc.Utf16 = C_enc.Utf16BE = {
12 | /**
13 | * Converts a word array to a UTF-16 BE string.
14 | *
15 | * @param {WordArray} wordArray The word array.
16 | *
17 | * @return {string} The UTF-16 BE string.
18 | *
19 | * @static
20 | *
21 | * @example
22 | *
23 | * var utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
24 | */
25 | stringify: function (wordArray) {
26 | // Shortcuts
27 | var words = wordArray.words;
28 | var sigBytes = wordArray.sigBytes;
29 |
30 | // Convert
31 | var utf16Chars = [];
32 | for (var i = 0; i < sigBytes; i += 2) {
33 | var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
34 | utf16Chars.push(String.fromCharCode(codePoint));
35 | }
36 |
37 | return utf16Chars.join('');
38 | },
39 |
40 | /**
41 | * Converts a UTF-16 BE string to a word array.
42 | *
43 | * @param {string} utf16Str The UTF-16 BE string.
44 | *
45 | * @return {WordArray} The word array.
46 | *
47 | * @static
48 | *
49 | * @example
50 | *
51 | * var wordArray = CryptoJS.enc.Utf16.parse(utf16String);
52 | */
53 | parse: function (utf16Str) {
54 | // Shortcut
55 | var utf16StrLength = utf16Str.length;
56 |
57 | // Convert
58 | var words = [];
59 | for (var i = 0; i < utf16StrLength; i++) {
60 | words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
61 | }
62 |
63 | return WordArray.create(words, utf16StrLength * 2);
64 | }
65 | };
66 |
67 | /**
68 | * UTF-16 LE encoding strategy.
69 | */
70 | C_enc.Utf16LE = {
71 | /**
72 | * Converts a word array to a UTF-16 LE string.
73 | *
74 | * @param {WordArray} wordArray The word array.
75 | *
76 | * @return {string} The UTF-16 LE string.
77 | *
78 | * @static
79 | *
80 | * @example
81 | *
82 | * var utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
83 | */
84 | stringify: function (wordArray) {
85 | // Shortcuts
86 | var words = wordArray.words;
87 | var sigBytes = wordArray.sigBytes;
88 |
89 | // Convert
90 | var utf16Chars = [];
91 | for (var i = 0; i < sigBytes; i += 2) {
92 | var codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
93 | utf16Chars.push(String.fromCharCode(codePoint));
94 | }
95 |
96 | return utf16Chars.join('');
97 | },
98 |
99 | /**
100 | * Converts a UTF-16 LE string to a word array.
101 | *
102 | * @param {string} utf16Str The UTF-16 LE string.
103 | *
104 | * @return {WordArray} The word array.
105 | *
106 | * @static
107 | *
108 | * @example
109 | *
110 | * var wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
111 | */
112 | parse: function (utf16Str) {
113 | // Shortcut
114 | var utf16StrLength = utf16Str.length;
115 |
116 | // Convert
117 | var words = [];
118 | for (var i = 0; i < utf16StrLength; i++) {
119 | words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
120 | }
121 |
122 | return WordArray.create(words, utf16StrLength * 2);
123 | }
124 | };
125 |
126 | function swapEndian(word) {
127 | return ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
128 | }
129 | }());
130 |
--------------------------------------------------------------------------------
/src/hmac.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var Base = C_lib.Base;
6 | var C_enc = C.enc;
7 | var Utf8 = C_enc.Utf8;
8 | var C_algo = C.algo;
9 |
10 | /**
11 | * HMAC algorithm.
12 | */
13 | var HMAC = C_algo.HMAC = Base.extend({
14 | /**
15 | * Initializes a newly created HMAC.
16 | *
17 | * @param {Hasher} hasher The hash algorithm to use.
18 | * @param {WordArray|string} key The secret key.
19 | *
20 | * @example
21 | *
22 | * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
23 | */
24 | init: function (hasher, key) {
25 | // Init hasher
26 | hasher = this._hasher = new hasher.init();
27 |
28 | // Convert string to WordArray, else assume WordArray already
29 | if (typeof key == 'string') {
30 | key = Utf8.parse(key);
31 | }
32 |
33 | // Shortcuts
34 | var hasherBlockSize = hasher.blockSize;
35 | var hasherBlockSizeBytes = hasherBlockSize * 4;
36 |
37 | // Allow arbitrary length keys
38 | if (key.sigBytes > hasherBlockSizeBytes) {
39 | key = hasher.finalize(key);
40 | }
41 |
42 | // Clamp excess bits
43 | key.clamp();
44 |
45 | // Clone key for inner and outer pads
46 | var oKey = this._oKey = key.clone();
47 | var iKey = this._iKey = key.clone();
48 |
49 | // Shortcuts
50 | var oKeyWords = oKey.words;
51 | var iKeyWords = iKey.words;
52 |
53 | // XOR keys with pad constants
54 | for (var i = 0; i < hasherBlockSize; i++) {
55 | oKeyWords[i] ^= 0x5c5c5c5c;
56 | iKeyWords[i] ^= 0x36363636;
57 | }
58 | oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
59 |
60 | // Set initial values
61 | this.reset();
62 | },
63 |
64 | /**
65 | * Resets this HMAC to its initial state.
66 | *
67 | * @example
68 | *
69 | * hmacHasher.reset();
70 | */
71 | reset: function () {
72 | // Shortcut
73 | var hasher = this._hasher;
74 |
75 | // Reset
76 | hasher.reset();
77 | hasher.update(this._iKey);
78 | },
79 |
80 | /**
81 | * Updates this HMAC with a message.
82 | *
83 | * @param {WordArray|string} messageUpdate The message to append.
84 | *
85 | * @return {HMAC} This HMAC instance.
86 | *
87 | * @example
88 | *
89 | * hmacHasher.update('message');
90 | * hmacHasher.update(wordArray);
91 | */
92 | update: function (messageUpdate) {
93 | this._hasher.update(messageUpdate);
94 |
95 | // Chainable
96 | return this;
97 | },
98 |
99 | /**
100 | * Finalizes the HMAC computation.
101 | * Note that the finalize operation is effectively a destructive, read-once operation.
102 | *
103 | * @param {WordArray|string} messageUpdate (Optional) A final message update.
104 | *
105 | * @return {WordArray} The HMAC.
106 | *
107 | * @example
108 | *
109 | * var hmac = hmacHasher.finalize();
110 | * var hmac = hmacHasher.finalize('message');
111 | * var hmac = hmacHasher.finalize(wordArray);
112 | */
113 | finalize: function (messageUpdate) {
114 | // Shortcut
115 | var hasher = this._hasher;
116 |
117 | // Compute HMAC
118 | var innerHash = hasher.finalize(messageUpdate);
119 | hasher.reset();
120 | var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
121 |
122 | return hmac;
123 | }
124 | });
125 | }());
126 |
--------------------------------------------------------------------------------
/src/sha1.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var WordArray = C_lib.WordArray;
6 | var Hasher = C_lib.Hasher;
7 | var C_algo = C.algo;
8 |
9 | // Reusable object
10 | var W = [];
11 |
12 | /**
13 | * SHA-1 hash algorithm.
14 | */
15 | var SHA1 = C_algo.SHA1 = Hasher.extend({
16 | _doReset: function () {
17 | this._hash = new WordArray.init([
18 | 0x67452301, 0xefcdab89,
19 | 0x98badcfe, 0x10325476,
20 | 0xc3d2e1f0
21 | ]);
22 | },
23 |
24 | _doProcessBlock: function (M, offset) {
25 | // Shortcut
26 | var H = this._hash.words;
27 |
28 | // Working variables
29 | var a = H[0];
30 | var b = H[1];
31 | var c = H[2];
32 | var d = H[3];
33 | var e = H[4];
34 |
35 | // Computation
36 | for (var i = 0; i < 80; i++) {
37 | if (i < 16) {
38 | W[i] = M[offset + i] | 0;
39 | } else {
40 | var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
41 | W[i] = (n << 1) | (n >>> 31);
42 | }
43 |
44 | var t = ((a << 5) | (a >>> 27)) + e + W[i];
45 | if (i < 20) {
46 | t += ((b & c) | (~b & d)) + 0x5a827999;
47 | } else if (i < 40) {
48 | t += (b ^ c ^ d) + 0x6ed9eba1;
49 | } else if (i < 60) {
50 | t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
51 | } else /* if (i < 80) */ {
52 | t += (b ^ c ^ d) - 0x359d3e2a;
53 | }
54 |
55 | e = d;
56 | d = c;
57 | c = (b << 30) | (b >>> 2);
58 | b = a;
59 | a = t;
60 | }
61 |
62 | // Intermediate hash value
63 | H[0] = (H[0] + a) | 0;
64 | H[1] = (H[1] + b) | 0;
65 | H[2] = (H[2] + c) | 0;
66 | H[3] = (H[3] + d) | 0;
67 | H[4] = (H[4] + e) | 0;
68 | },
69 |
70 | _doFinalize: function () {
71 | // Shortcuts
72 | var data = this._data;
73 | var dataWords = data.words;
74 |
75 | var nBitsTotal = this._nDataBytes * 8;
76 | var nBitsLeft = data.sigBytes * 8;
77 |
78 | // Add padding
79 | dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
80 | dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
81 | dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
82 | data.sigBytes = dataWords.length * 4;
83 |
84 | // Hash final blocks
85 | this._process();
86 |
87 | // Return final computed hash
88 | return this._hash;
89 | },
90 |
91 | clone: function () {
92 | var clone = Hasher.clone.call(this);
93 | clone._hash = this._hash.clone();
94 |
95 | return clone;
96 | }
97 | });
98 |
99 | /**
100 | * Shortcut function to the hasher's object interface.
101 | *
102 | * @param {WordArray|string} message The message to hash.
103 | *
104 | * @return {WordArray} The hash.
105 | *
106 | * @static
107 | *
108 | * @example
109 | *
110 | * var hash = CryptoJS.SHA1('message');
111 | * var hash = CryptoJS.SHA1(wordArray);
112 | */
113 | C.SHA1 = Hasher._createHelper(SHA1);
114 |
115 | /**
116 | * Shortcut function to the HMAC's object interface.
117 | *
118 | * @param {WordArray|string} message The message to hash.
119 | * @param {WordArray|string} key The secret key.
120 | *
121 | * @return {WordArray} The HMAC.
122 | *
123 | * @static
124 | *
125 | * @example
126 | *
127 | * var hmac = CryptoJS.HmacSHA1(message, key);
128 | */
129 | C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
130 | }());
131 |
--------------------------------------------------------------------------------
/test/x64-word-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('x64-word-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'X64Word',
6 |
7 | testInit: function () {
8 | var word = C.x64.Word.create(0x00010203, 0x04050607);
9 |
10 | Y.Assert.areEqual(0x00010203, word.high, 'word.high');
11 | Y.Assert.areEqual(0x04050607, word.low, 'word.low');
12 | }
13 |
14 | // testNot: function () {
15 | // var word = C.x64.Word.create(0x00010203, 0x04050607).not();
16 |
17 | // Y.Assert.areEqual(~0x00010203, word.high, 'word.high');
18 | // Y.Assert.areEqual(~0x04050607, word.low, 'word.low');
19 | // },
20 |
21 | // testAnd: function () {
22 | // var word1 = C.x64.Word.create(0x00010203, 0x04050607);
23 | // var word2 = C.x64.Word.create(0x18191a1b, 0x1c1d1e1f);
24 | // var anded = word1.and(word2);
25 |
26 | // Y.Assert.areEqual(0x00010203 & 0x18191a1b, anded.high, 'word.high');
27 | // Y.Assert.areEqual(0x04050607 & 0x1c1d1e1f, anded.low, 'word.low');
28 | // },
29 |
30 | // testOr: function () {
31 | // var word1 = C.x64.Word.create(0x00010203, 0x04050607);
32 | // var word2 = C.x64.Word.create(0x18191a1b, 0x1c1d1e1f);
33 | // var ored = word1.or(word2);
34 |
35 | // Y.Assert.areEqual(0x00010203 | 0x18191a1b, ored.high, 'word.high');
36 | // Y.Assert.areEqual(0x04050607 | 0x1c1d1e1f, ored.low, 'word.low');
37 | // },
38 |
39 | // testXor: function () {
40 | // var word1 = C.x64.Word.create(0x00010203, 0x04050607);
41 | // var word2 = C.x64.Word.create(0x18191a1b, 0x1c1d1e1f);
42 | // var xored = word1.xor(word2);
43 |
44 | // Y.Assert.areEqual(0x00010203 ^ 0x18191a1b, xored.high, 'word.high');
45 | // Y.Assert.areEqual(0x04050607 ^ 0x1c1d1e1f, xored.low, 'word.low');
46 | // },
47 |
48 | // testShiftL25: function () {
49 | // var word = C.x64.Word.create(0x00010203, 0x04050607).shiftL(25);
50 |
51 | // Y.Assert.areEqual(0x06080a0c, word.high, 'word.high');
52 | // Y.Assert.areEqual(0x0e000000, word.low, 'word.low');
53 | // },
54 |
55 | // testShiftL32: function () {
56 | // var word = C.x64.Word.create(0x00010203, 0x04050607).shiftL(32);
57 |
58 | // Y.Assert.areEqual(0x04050607, word.high, 'word.high');
59 | // Y.Assert.areEqual(0x00000000, word.low, 'word.low');
60 | // },
61 |
62 | // testShiftR7: function () {
63 | // var word = C.x64.Word.create(0x00010203, 0x04050607).shiftR(7);
64 |
65 | // Y.Assert.areEqual(0x00000204, word.high, 'word.high');
66 | // Y.Assert.areEqual(0x06080A0C, word.low, 'word.low');
67 | // },
68 |
69 | // testShiftR32: function () {
70 | // var word = C.x64.Word.create(0x00010203, 0x04050607).shiftR(32);
71 |
72 | // Y.Assert.areEqual(0x00000000, word.high, 'word.high');
73 | // Y.Assert.areEqual(0x00010203, word.low, 'word.low');
74 | // },
75 |
76 | // testRotL: function () {
77 | // var word = C.x64.Word.create(0x00010203, 0x04050607).rotL(25);
78 |
79 | // Y.Assert.areEqual(0x06080a0c, word.high, 'word.high');
80 | // Y.Assert.areEqual(0x0e000204, word.low, 'word.low');
81 | // },
82 |
83 | // testRotR: function () {
84 | // var word = C.x64.Word.create(0x00010203, 0x04050607).rotR(7);
85 |
86 | // Y.Assert.areEqual(0x0e000204, word.high, 'word.high');
87 | // Y.Assert.areEqual(0x06080a0c, word.low, 'word.low');
88 | // },
89 |
90 | // testAdd: function () {
91 | // var word1 = C.x64.Word.create(0x00010203, 0x04050607);
92 | // var word2 = C.x64.Word.create(0x18191a1b, 0x1c1d1e1f);
93 | // var added = word1.add(word2);
94 |
95 | // Y.Assert.areEqual(0x181a1c1e, added.high, 'word.high');
96 | // Y.Assert.areEqual(0x20222426, added.low, 'word.low');
97 | // }
98 | }));
99 | }, '$Rev$');
100 |
--------------------------------------------------------------------------------
/test/rabbit-legacy-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-rabbit-legacy-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'RabbitLegacy',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('02f74a1c26456bf5ecd6a536f05457b1', C.RabbitLegacy.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('00000000000000000000000000000000')).ciphertext.toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('9c51e28784c37fe9a127f63ec8f32d3d', C.RabbitLegacy.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('dc51c3ac3bfc62f12e3d36fe91281329')).ciphertext.toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('9b60d002fd5ceb32accd41a0cd0db10c', C.RabbitLegacy.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('c09b0043e9e9ab0187e0c73383957415')).ciphertext.toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('edb70567375dcd7cd89554f85e27a7c6', C.RabbitLegacy.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('00000000000000000000000000000000'), { iv: C.enc.Hex.parse('0000000000000000') }).ciphertext.toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('6d7d012292ccdce0e2120058b94ecd1f', C.RabbitLegacy.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('00000000000000000000000000000000'), { iv: C.enc.Hex.parse('597e26c175f573c3') }).ciphertext.toString());
25 | },
26 |
27 | testVector6: function () {
28 | Y.Assert.areEqual('4d1051a123afb670bf8d8505c8d85a44', C.RabbitLegacy.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('00000000000000000000000000000000'), { iv: C.enc.Hex.parse('2717f4d21a56eba6') }).ciphertext.toString());
29 | },
30 |
31 | testMultiPart: function () {
32 | var rabbit = C.algo.RabbitLegacy.createEncryptor(C.enc.Hex.parse('00000000000000000000000000000000'));
33 | var ciphertext1 = rabbit.process(C.enc.Hex.parse('000000000000'));
34 | var ciphertext2 = rabbit.process(C.enc.Hex.parse('0000000000'));
35 | var ciphertext3 = rabbit.process(C.enc.Hex.parse('0000000000'));
36 | var ciphertext4 = rabbit.finalize();
37 |
38 | Y.Assert.areEqual('02f74a1c26456bf5ecd6a536f05457b1', ciphertext1.concat(ciphertext2).concat(ciphertext3).concat(ciphertext4).toString());
39 | },
40 |
41 | testInputIntegrity: function () {
42 | var message = C.enc.Hex.parse('00000000000000000000000000000000');
43 | var key = C.enc.Hex.parse('00000000000000000000000000000000');
44 | var iv = C.enc.Hex.parse('0000000000000000');
45 |
46 | var expectedMessage = message.toString();
47 | var expectedKey = key.toString();
48 | var expectedIv = iv.toString();
49 |
50 | C.RabbitLegacy.encrypt(message, key, { iv: iv });
51 |
52 | Y.Assert.areEqual(expectedMessage, message.toString());
53 | Y.Assert.areEqual(expectedKey, key.toString());
54 | Y.Assert.areEqual(expectedIv, iv.toString());
55 | },
56 |
57 | testHelper: function () {
58 | // Save original random method
59 | var random = C.lib.WordArray.random;
60 |
61 | // Replace random method with one that returns a predictable value
62 | C.lib.WordArray.random = function (nBytes) {
63 | var words = [];
64 | for (var i = 0; i < nBytes; i += 4) {
65 | words.push([0x11223344]);
66 | }
67 |
68 | return C.lib.WordArray.create(words, nBytes);
69 | };
70 |
71 | // Test
72 | Y.Assert.areEqual(C.algo.RabbitLegacy.createEncryptor(C.MD5('Jefe')).finalize('Hi There').toString(), C.RabbitLegacy.encrypt('Hi There', C.MD5('Jefe')).ciphertext.toString());
73 | Y.Assert.areEqual(C.lib.SerializableCipher.encrypt(C.algo.RabbitLegacy, 'Hi There', C.MD5('Jefe')).toString(), C.RabbitLegacy.encrypt('Hi There', C.MD5('Jefe')).toString());
74 | Y.Assert.areEqual(C.lib.PasswordBasedCipher.encrypt(C.algo.RabbitLegacy, 'Hi There', 'Jefe').toString(), C.RabbitLegacy.encrypt('Hi There', 'Jefe').toString());
75 |
76 | // Restore random method
77 | C.lib.WordArray.random = random;
78 | }
79 | }));
80 | }, '$Rev$');
81 |
--------------------------------------------------------------------------------
/src/pbkdf2.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var Base = C_lib.Base;
6 | var WordArray = C_lib.WordArray;
7 | var C_algo = C.algo;
8 | var SHA256 = C_algo.SHA256;
9 | var HMAC = C_algo.HMAC;
10 |
11 | /**
12 | * Password-Based Key Derivation Function 2 algorithm.
13 | */
14 | var PBKDF2 = C_algo.PBKDF2 = Base.extend({
15 | /**
16 | * Configuration options.
17 | *
18 | * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
19 | * @property {Hasher} hasher The hasher to use. Default: SHA256
20 | * @property {number} iterations The number of iterations to perform. Default: 250000
21 | */
22 | cfg: Base.extend({
23 | keySize: 128/32,
24 | hasher: SHA256,
25 | iterations: 250000
26 | }),
27 |
28 | /**
29 | * Initializes a newly created key derivation function.
30 | *
31 | * @param {Object} cfg (Optional) The configuration options to use for the derivation.
32 | *
33 | * @example
34 | *
35 | * var kdf = CryptoJS.algo.PBKDF2.create();
36 | * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
37 | * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
38 | */
39 | init: function (cfg) {
40 | this.cfg = this.cfg.extend(cfg);
41 | },
42 |
43 | /**
44 | * Computes the Password-Based Key Derivation Function 2.
45 | *
46 | * @param {WordArray|string} password The password.
47 | * @param {WordArray|string} salt A salt.
48 | *
49 | * @return {WordArray} The derived key.
50 | *
51 | * @example
52 | *
53 | * var key = kdf.compute(password, salt);
54 | */
55 | compute: function (password, salt) {
56 | // Shortcut
57 | var cfg = this.cfg;
58 |
59 | // Init HMAC
60 | var hmac = HMAC.create(cfg.hasher, password);
61 |
62 | // Initial values
63 | var derivedKey = WordArray.create();
64 | var blockIndex = WordArray.create([0x00000001]);
65 |
66 | // Shortcuts
67 | var derivedKeyWords = derivedKey.words;
68 | var blockIndexWords = blockIndex.words;
69 | var keySize = cfg.keySize;
70 | var iterations = cfg.iterations;
71 |
72 | // Generate key
73 | while (derivedKeyWords.length < keySize) {
74 | var block = hmac.update(salt).finalize(blockIndex);
75 | hmac.reset();
76 |
77 | // Shortcuts
78 | var blockWords = block.words;
79 | var blockWordsLength = blockWords.length;
80 |
81 | // Iterations
82 | var intermediate = block;
83 | for (var i = 1; i < iterations; i++) {
84 | intermediate = hmac.finalize(intermediate);
85 | hmac.reset();
86 |
87 | // Shortcut
88 | var intermediateWords = intermediate.words;
89 |
90 | // XOR intermediate with block
91 | for (var j = 0; j < blockWordsLength; j++) {
92 | blockWords[j] ^= intermediateWords[j];
93 | }
94 | }
95 |
96 | derivedKey.concat(block);
97 | blockIndexWords[0]++;
98 | }
99 | derivedKey.sigBytes = keySize * 4;
100 |
101 | return derivedKey;
102 | }
103 | });
104 |
105 | /**
106 | * Computes the Password-Based Key Derivation Function 2.
107 | *
108 | * @param {WordArray|string} password The password.
109 | * @param {WordArray|string} salt A salt.
110 | * @param {Object} cfg (Optional) The configuration options to use for this computation.
111 | *
112 | * @return {WordArray} The derived key.
113 | *
114 | * @static
115 | *
116 | * @example
117 | *
118 | * var key = CryptoJS.PBKDF2(password, salt);
119 | * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
120 | * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
121 | */
122 | C.PBKDF2 = function (password, salt, cfg) {
123 | return PBKDF2.create(cfg).compute(password, salt);
124 | };
125 | }());
126 |
--------------------------------------------------------------------------------
/src/enc-base64url.js:
--------------------------------------------------------------------------------
1 | (function () {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var WordArray = C_lib.WordArray;
6 | var C_enc = C.enc;
7 |
8 | /**
9 | * Base64url encoding strategy.
10 | */
11 | var Base64url = C_enc.Base64url = {
12 | /**
13 | * Converts a word array to a Base64url string.
14 | *
15 | * @param {WordArray} wordArray The word array.
16 | *
17 | * @param {boolean} urlSafe Whether to use url safe
18 | *
19 | * @return {string} The Base64url string.
20 | *
21 | * @static
22 | *
23 | * @example
24 | *
25 | * var base64String = CryptoJS.enc.Base64url.stringify(wordArray);
26 | */
27 | stringify: function (wordArray, urlSafe) {
28 | if (urlSafe === undefined) {
29 | urlSafe = true
30 | }
31 | // Shortcuts
32 | var words = wordArray.words;
33 | var sigBytes = wordArray.sigBytes;
34 | var map = urlSafe ? this._safe_map : this._map;
35 |
36 | // Clamp excess bits
37 | wordArray.clamp();
38 |
39 | // Convert
40 | var base64Chars = [];
41 | for (var i = 0; i < sigBytes; i += 3) {
42 | var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
43 | var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
44 | var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
45 |
46 | var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
47 |
48 | for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
49 | base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
50 | }
51 | }
52 |
53 | // Add padding
54 | var paddingChar = map.charAt(64);
55 | if (paddingChar) {
56 | while (base64Chars.length % 4) {
57 | base64Chars.push(paddingChar);
58 | }
59 | }
60 |
61 | return base64Chars.join('');
62 | },
63 |
64 | /**
65 | * Converts a Base64url string to a word array.
66 | *
67 | * @param {string} base64Str The Base64url string.
68 | *
69 | * @param {boolean} urlSafe Whether to use url safe
70 | *
71 | * @return {WordArray} The word array.
72 | *
73 | * @static
74 | *
75 | * @example
76 | *
77 | * var wordArray = CryptoJS.enc.Base64url.parse(base64String);
78 | */
79 | parse: function (base64Str, urlSafe) {
80 | if (urlSafe === undefined) {
81 | urlSafe = true
82 | }
83 |
84 | // Shortcuts
85 | var base64StrLength = base64Str.length;
86 | var map = urlSafe ? this._safe_map : this._map;
87 | var reverseMap = this._reverseMap;
88 |
89 | if (!reverseMap) {
90 | reverseMap = this._reverseMap = [];
91 | for (var j = 0; j < map.length; j++) {
92 | reverseMap[map.charCodeAt(j)] = j;
93 | }
94 | }
95 |
96 | // Ignore padding
97 | var paddingChar = map.charAt(64);
98 | if (paddingChar) {
99 | var paddingIndex = base64Str.indexOf(paddingChar);
100 | if (paddingIndex !== -1) {
101 | base64StrLength = paddingIndex;
102 | }
103 | }
104 |
105 | // Convert
106 | return parseLoop(base64Str, base64StrLength, reverseMap);
107 |
108 | },
109 |
110 | _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
111 | _safe_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_',
112 | };
113 |
114 | function parseLoop(base64Str, base64StrLength, reverseMap) {
115 | var words = [];
116 | var nBytes = 0;
117 | for (var i = 0; i < base64StrLength; i++) {
118 | if (i % 4) {
119 | var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
120 | var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
121 | var bitsCombined = bits1 | bits2;
122 | words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
123 | nBytes++;
124 | }
125 | }
126 | return WordArray.create(words, nBytes);
127 | }
128 | }());
129 |
--------------------------------------------------------------------------------
/test/test-build.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CryptoJS Test Suite
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
86 |
87 |
102 |
103 |
104 |
105 |
106 |
--------------------------------------------------------------------------------
/test/rabbit-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-rabbit-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'Rabbit',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('02f74a1c26456bf5ecd6a536f05457b1', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('00000000000000000000000000000000')).ciphertext.toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('3d02e0c730559112b473b790dee018df', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('c21fcf3881cd5ee8628accb0a9890df8')).ciphertext.toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('a3a97abb80393820b7e50c4abb53823d', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('1d272c6a2d8e3dfcac14056b78d633a0')).ciphertext.toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('75d186d6bc6905c64f1b2dfdd51f7bfc', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('0053a6f94c9ff24598eb3e91e4378add'), { iv: C.enc.Hex.parse('0d74db42a91077de') }).ciphertext.toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('476e2750c73856c93563b5f546f56a6a', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('0558abfe51a4f74a9df04396e93c8fe2'), { iv: C.enc.Hex.parse('167de44bb21980e7') }).ciphertext.toString());
25 | },
26 |
27 | testVector6: function () {
28 | Y.Assert.areEqual('921fcf4983891365a7dc901924b5e24b', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('0a5db00356a9fc4fa2f5489bee4194e7'), { iv: C.enc.Hex.parse('1f86ed54bb2289f0') }).ciphertext.toString());
29 | },
30 |
31 | testVector7: function () {
32 | Y.Assert.areEqual('613cb0ba96aff6cacf2a459a102a7f78', C.Rabbit.encrypt(C.enc.Hex.parse('00000000000000000000000000000000'), C.enc.Hex.parse('0f62b5085bae0154a7fa4da0f34699ec'), { iv: C.enc.Hex.parse('288ff65dc42b92f9') }).ciphertext.toString());
33 | },
34 |
35 | testMultiPart: function () {
36 | var rabbit = C.algo.Rabbit.createEncryptor(C.enc.Hex.parse('00000000000000000000000000000000'));
37 | var ciphertext1 = rabbit.process(C.enc.Hex.parse('000000000000'));
38 | var ciphertext2 = rabbit.process(C.enc.Hex.parse('0000000000'));
39 | var ciphertext3 = rabbit.process(C.enc.Hex.parse('0000000000'));
40 | var ciphertext4 = rabbit.finalize();
41 |
42 | Y.Assert.areEqual('02f74a1c26456bf5ecd6a536f05457b1', ciphertext1.concat(ciphertext2).concat(ciphertext3).concat(ciphertext4).toString());
43 | },
44 |
45 | testInputIntegrity: function () {
46 | var message = C.enc.Hex.parse('00000000000000000000000000000000');
47 | var key = C.enc.Hex.parse('00000000000000000000000000000000');
48 | var iv = C.enc.Hex.parse('0000000000000000');
49 |
50 | var expectedMessage = message.toString();
51 | var expectedKey = key.toString();
52 | var expectedIv = iv.toString();
53 |
54 | C.Rabbit.encrypt(message, key, { iv: iv });
55 |
56 | Y.Assert.areEqual(expectedMessage, message.toString());
57 | Y.Assert.areEqual(expectedKey, key.toString());
58 | Y.Assert.areEqual(expectedIv, iv.toString());
59 | },
60 |
61 | testHelper: function () {
62 | // Save original random method
63 | var random = C.lib.WordArray.random;
64 |
65 | // Replace random method with one that returns a predictable value
66 | C.lib.WordArray.random = function (nBytes) {
67 | var words = [];
68 | for (var i = 0; i < nBytes; i += 4) {
69 | words.push([0x11223344]);
70 | }
71 |
72 | return C.lib.WordArray.create(words, nBytes);
73 | };
74 |
75 | // Test
76 | Y.Assert.areEqual(C.algo.Rabbit.createEncryptor(C.MD5('Jefe')).finalize('Hi There').toString(), C.Rabbit.encrypt('Hi There', C.MD5('Jefe')).ciphertext.toString());
77 | Y.Assert.areEqual(C.lib.SerializableCipher.encrypt(C.algo.Rabbit, 'Hi There', C.MD5('Jefe')).toString(), C.Rabbit.encrypt('Hi There', C.MD5('Jefe')).toString());
78 | Y.Assert.areEqual(C.lib.PasswordBasedCipher.encrypt(C.algo.Rabbit, 'Hi There', 'Jefe').toString(), C.Rabbit.encrypt('Hi There', 'Jefe').toString());
79 |
80 | // Restore random method
81 | C.lib.WordArray.random = random;
82 | }
83 | }));
84 | }, '$Rev$');
85 |
--------------------------------------------------------------------------------
/test/pbkdf2-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-pbkdf2-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'PBKDF2',
6 |
7 | testKeySize128: function () {
8 | Y.Assert.areEqual('62929ab995a1111c75c37bc562261ea3', C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 128/32 }).toString());
9 | },
10 |
11 | testKeySize256: function () {
12 | Y.Assert.areEqual('62929ab995a1111c75c37bc562261ea3fb3cdc7e725c4ca87c03cec5bb7663e1', C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 256/32 }).toString());
13 | },
14 |
15 | testKeySize128Iterations2: function () {
16 | Y.Assert.areEqual('262fb72ea65b44ab5ceba7f8c8bfa781', C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 128/32, iterations: 2 }).toString());
17 | },
18 |
19 | testKeySize256Iterations2: function () {
20 | Y.Assert.areEqual('262fb72ea65b44ab5ceba7f8c8bfa7815ff9939204eb7357a59a75877d745777', C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 256/32, iterations: 2 }).toString());
21 | },
22 |
23 | testKeySize128Iterations1200: function () {
24 | Y.Assert.areEqual('c76a982415f1acc71dc197273c5b6ada', C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 128/32, iterations: 1200 }).toString());
25 | },
26 |
27 | testKeySize256Iterations1200: function () {
28 | Y.Assert.areEqual('c76a982415f1acc71dc197273c5b6ada32f62915ed461718aad32843762433fa', C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 256/32, iterations: 1200 }).toString());
29 | },
30 |
31 | testKeySize128Iterations5: function () {
32 | Y.Assert.areEqual('74e98b2e9eeddaab3113c1efc6d82b07', C.PBKDF2('password', C.enc.Hex.parse('1234567878563412'), { keySize: 128/32, iterations: 5 }).toString());
33 | },
34 |
35 | testKeySize256Iterations5: function () {
36 | Y.Assert.areEqual('74e98b2e9eeddaab3113c1efc6d82b073c4860195b3e0737fa21a4778f376321', C.PBKDF2('password', C.enc.Hex.parse('1234567878563412'), { keySize: 256/32, iterations: 5 }).toString());
37 | },
38 |
39 | testKeySize128Iterations1200PassPhraseEqualsBlockSize: function () {
40 | Y.Assert.areEqual('c1dfb29a4d2f2fb67c6f78d074d66367', C.PBKDF2('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'pass phrase equals block size', { keySize: 128/32, iterations: 1200 }).toString());
41 | },
42 |
43 | testKeySize256Iterations1200PassPhraseEqualsBlockSize: function () {
44 | Y.Assert.areEqual('c1dfb29a4d2f2fb67c6f78d074d663671e6fd4da1e598572b1fecf256cb7cf61', C.PBKDF2('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'pass phrase equals block size', { keySize: 256/32, iterations: 1200 }).toString());
45 | },
46 |
47 | testKeySize128Iterations1200PassPhraseExceedsBlockSize: function () {
48 | Y.Assert.areEqual('22344bc4b6e32675a8090f3ea80be01d', C.PBKDF2('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'pass phrase exceeds block size', { keySize: 128/32, iterations: 1200 }).toString());
49 | },
50 |
51 | testKeySize256Iterations1200PassPhraseExceedsBlockSize: function () {
52 | Y.Assert.areEqual('22344bc4b6e32675a8090f3ea80be01d5f95126a2cddc3facc4a5e6dca04ec58', C.PBKDF2('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'pass phrase exceeds block size', { keySize: 256/32, iterations: 1200 }).toString());
53 | },
54 |
55 | testKeySize128Iterations50: function () {
56 | Y.Assert.areEqual('44b0781253db3141ac4174af29325818', C.PBKDF2(C.enc.Hex.parse('f09d849e'), 'EXAMPLE.COMpianist', { keySize: 128/32, iterations: 50 }).toString());
57 | },
58 |
59 | testKeySize256Iterations50: function () {
60 | Y.Assert.areEqual('44b0781253db3141ac4174af29325818584698d507a79f9879033dec308a2b77', C.PBKDF2(C.enc.Hex.parse('f09d849e'), 'EXAMPLE.COMpianist', { keySize: 256/32, iterations: 50 }).toString());
61 | },
62 |
63 | testInputIntegrity: function () {
64 | var password = C.lib.WordArray.create([0x12345678]);
65 | var salt = C.lib.WordArray.create([0x12345678]);
66 |
67 | var expectedPassword = password.toString();
68 | var expectedSalt = salt.toString();
69 |
70 | C.PBKDF2(password, salt);
71 |
72 | Y.Assert.areEqual(expectedPassword, password.toString());
73 | Y.Assert.areEqual(expectedSalt, salt.toString());
74 | },
75 |
76 | testHelper: function () {
77 | Y.Assert.areEqual(C.algo.PBKDF2.create({ keySize: 128/32 }).compute('password', 'ATHENA.MIT.EDUraeburn').toString(), C.PBKDF2('password', 'ATHENA.MIT.EDUraeburn', { keySize: 128/32 }).toString());
78 | }
79 | }));
80 | }, '$Rev$');
81 |
--------------------------------------------------------------------------------
/test/aes-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-aes-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'AES',
6 |
7 | testEncryptKeySize128: function () {
8 | Y.Assert.areEqual('69c4e0d86a7b0430d8cdb78070b4c55a', C.AES.encrypt(C.enc.Hex.parse('00112233445566778899aabbccddeeff'), C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
9 | },
10 |
11 | testEncryptKeySize192: function () {
12 | Y.Assert.areEqual('dda97ca4864cdfe06eaf70a0ec0d7191', C.AES.encrypt(C.enc.Hex.parse('00112233445566778899aabbccddeeff'), C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f1011121314151617'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
13 | },
14 |
15 | testEncryptKeySize256: function () {
16 | Y.Assert.areEqual('8ea2b7ca516745bfeafc49904b496089', C.AES.encrypt(C.enc.Hex.parse('00112233445566778899aabbccddeeff'), C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
17 | },
18 |
19 | testDecryptKeySize128: function () {
20 | Y.Assert.areEqual('00112233445566778899aabbccddeeff', C.AES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('69c4e0d86a7b0430d8cdb78070b4c55a') }), C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
21 | },
22 |
23 | testDecryptKeySize192: function () {
24 | Y.Assert.areEqual('00112233445566778899aabbccddeeff', C.AES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('dda97ca4864cdfe06eaf70a0ec0d7191') }), C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f1011121314151617'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
25 | },
26 |
27 | testDecryptKeySize256: function () {
28 | Y.Assert.areEqual('00112233445566778899aabbccddeeff', C.AES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('8ea2b7ca516745bfeafc49904b496089') }), C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
29 | },
30 |
31 | testMultiPart: function () {
32 | var aes = C.algo.AES.createEncryptor(C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f'), { mode: C.mode.ECB, padding: C.pad.NoPadding });
33 | var ciphertext1 = aes.process(C.enc.Hex.parse('001122334455'));
34 | var ciphertext2 = aes.process(C.enc.Hex.parse('66778899aa'));
35 | var ciphertext3 = aes.process(C.enc.Hex.parse('bbccddeeff'));
36 | var ciphertext4 = aes.finalize();
37 |
38 | Y.Assert.areEqual('69c4e0d86a7b0430d8cdb78070b4c55a', ciphertext1.concat(ciphertext2).concat(ciphertext3).concat(ciphertext4).toString());
39 | },
40 |
41 | testInputIntegrity: function () {
42 | var message = C.enc.Hex.parse('00112233445566778899aabbccddeeff');
43 | var key = C.enc.Hex.parse('000102030405060708090a0b0c0d0e0f');
44 | var iv = C.enc.Hex.parse('101112131415161718191a1b1c1d1e1f');
45 |
46 | var expectedMessage = message.toString();
47 | var expectedKey = key.toString();
48 | var expectedIv = iv.toString();
49 |
50 | C.AES.encrypt(message, key, { iv: iv });
51 |
52 | Y.Assert.areEqual(expectedMessage, message.toString());
53 | Y.Assert.areEqual(expectedKey, key.toString());
54 | Y.Assert.areEqual(expectedIv, iv.toString());
55 | },
56 |
57 | testHelper: function () {
58 | // Save original random method
59 | var random = C.lib.WordArray.random;
60 |
61 | // Replace random method with one that returns a predictable value
62 | C.lib.WordArray.random = function (nBytes) {
63 | var words = [];
64 | for (var i = 0; i < nBytes; i += 4) {
65 | words.push([0x11223344]);
66 | }
67 |
68 | return C.lib.WordArray.create(words, nBytes);
69 | };
70 |
71 | // Test
72 | Y.Assert.areEqual(C.algo.AES.createEncryptor(C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).finalize('Hi There').toString(), C.AES.encrypt('Hi There', C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
73 | Y.Assert.areEqual(C.lib.SerializableCipher.encrypt(C.algo.AES, 'Hi There', C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString(), C.AES.encrypt('Hi There', C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
74 | Y.Assert.areEqual(C.lib.PasswordBasedCipher.encrypt(C.algo.AES, 'Hi There', 'Jefe', { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString(), C.AES.encrypt('Hi There', 'Jefe', { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
75 |
76 | // Restore random method
77 | C.lib.WordArray.random = random;
78 | }
79 | }));
80 | }, '$Rev$');
81 |
--------------------------------------------------------------------------------
/test/sha3-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-sha3-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'SHA3',
6 |
7 | testVector1: function () {
8 | Y.Assert.areEqual('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e', C.SHA3('', { outputLength: 512 }).toString());
9 | },
10 |
11 | testVector2: function () {
12 | Y.Assert.areEqual('81950e7096d31d4f22e3db71cac725bf59e81af54c7ca9e6aeee71c010fc5467466312a01aa5c137cfb140646941556796f612c9351268737c7e9a2b9631d1fa', C.SHA3(C.enc.Hex.parse('3a3a819c48efde2ad914fbf00e18ab6bc4f14513ab27d0c178a188b61431e7f5623cb66b23346775d386b50e982c493adbbfc54b9a3cd383382336a1a0b2150a15358f336d03ae18f666c7573d55c4fd181c29e6ccfde63ea35f0adf5885cfc0a3d84a2b2e4dd24496db789e663170cef74798aa1bbcd4574ea0bba40489d764b2f83aadc66b148b4a0cd95246c127d5871c4f11418690a5ddf01246a0c80a43c70088b6183639dcfda4125bd113a8f49ee23ed306faac576c3fb0c1e256671d817fc2534a52f5b439f72e424de376f4c565cca82307dd9ef76da5b7c4eb7e085172e328807c02d011ffbf33785378d79dc266f6a5be6bb0e4a92eceebaeb1'), { outputLength: 512 }).toString());
13 | },
14 |
15 | testVector3: function () {
16 | Y.Assert.areEqual('2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff', C.SHA3('', { outputLength: 384 }).toString());
17 | },
18 |
19 | testVector4: function () {
20 | Y.Assert.areEqual('6bff1c8405a3fe594e360e3bccea1ebcd509310dc79b9e45c263783d7a5dd662c6789b18bd567dbdda1554f5bee6a860', C.SHA3(C.enc.Hex.parse('3a3a819c48efde2ad914fbf00e18ab6bc4f14513ab27d0c178a188b61431e7f5623cb66b23346775d386b50e982c493adbbfc54b9a3cd383382336a1a0b2150a15358f336d03ae18f666c7573d55c4fd181c29e6ccfde63ea35f0adf5885cfc0a3d84a2b2e4dd24496db789e663170cef74798aa1bbcd4574ea0bba40489d764b2f83aadc66b148b4a0cd95246c127d5871c4f11418690a5ddf01246a0c80a43c70088b6183639dcfda4125bd113a8f49ee23ed306faac576c3fb0c1e256671d817fc2534a52f5b439f72e424de376f4c565cca82307dd9ef76da5b7c4eb7e085172e328807c02d011ffbf33785378d79dc266f6a5be6bb0e4a92eceebaeb1'), { outputLength: 384 }).toString());
21 | },
22 |
23 | testVector5: function () {
24 | Y.Assert.areEqual('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470', C.SHA3('', { outputLength: 256 }).toString());
25 | },
26 |
27 | testVector6: function () {
28 | Y.Assert.areEqual('348fb774adc970a16b1105669442625e6adaa8257a89effdb5a802f161b862ea', C.SHA3(C.enc.Hex.parse('3a3a819c48efde2ad914fbf00e18ab6bc4f14513ab27d0c178a188b61431e7f5623cb66b23346775d386b50e982c493adbbfc54b9a3cd383382336a1a0b2150a15358f336d03ae18f666c7573d55c4fd181c29e6ccfde63ea35f0adf5885cfc0a3d84a2b2e4dd24496db789e663170cef74798aa1bbcd4574ea0bba40489d764b2f83aadc66b148b4a0cd95246c127d5871c4f11418690a5ddf01246a0c80a43c70088b6183639dcfda4125bd113a8f49ee23ed306faac576c3fb0c1e256671d817fc2534a52f5b439f72e424de376f4c565cca82307dd9ef76da5b7c4eb7e085172e328807c02d011ffbf33785378d79dc266f6a5be6bb0e4a92eceebaeb1'), { outputLength: 256 }).toString());
29 | },
30 |
31 | testVector7: function () {
32 | Y.Assert.areEqual('f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd', C.SHA3('', { outputLength: 224 }).toString());
33 | },
34 |
35 | testVector8: function () {
36 | Y.Assert.areEqual('5af56987ea9cf11fcd0eac5ebc14b037365e9b1123e31cb2dfc7929a', C.SHA3(C.enc.Hex.parse('3a3a819c48efde2ad914fbf00e18ab6bc4f14513ab27d0c178a188b61431e7f5623cb66b23346775d386b50e982c493adbbfc54b9a3cd383382336a1a0b2150a15358f336d03ae18f666c7573d55c4fd181c29e6ccfde63ea35f0adf5885cfc0a3d84a2b2e4dd24496db789e663170cef74798aa1bbcd4574ea0bba40489d764b2f83aadc66b148b4a0cd95246c127d5871c4f11418690a5ddf01246a0c80a43c70088b6183639dcfda4125bd113a8f49ee23ed306faac576c3fb0c1e256671d817fc2534a52f5b439f72e424de376f4c565cca82307dd9ef76da5b7c4eb7e085172e328807c02d011ffbf33785378d79dc266f6a5be6bb0e4a92eceebaeb1'), { outputLength: 224 }).toString());
37 | },
38 |
39 | testDefaultOutputLength: function () {
40 | Y.Assert.areEqual('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e', C.SHA3('').toString());
41 | },
42 |
43 | testClone: function () {
44 | var sha3 = C.algo.SHA3.create();
45 |
46 | Y.Assert.areEqual(C.SHA3('a').toString(), sha3.update('a').clone().finalize().toString());
47 | Y.Assert.areEqual(C.SHA3('ab').toString(), sha3.update('b').clone().finalize().toString());
48 | Y.Assert.areEqual(C.SHA3('abc').toString(), sha3.update('c').clone().finalize().toString());
49 | },
50 |
51 | testInputIntegrity: function () {
52 | var message = C.lib.WordArray.create([0x12345678]);
53 |
54 | var expected = message.toString();
55 |
56 | C.SHA3(message);
57 |
58 | Y.Assert.areEqual(expected, message.toString());
59 | },
60 |
61 | testHelper: function () {
62 | Y.Assert.areEqual(C.algo.SHA3.create({ outputLength: 256 }).finalize('').toString(), C.SHA3('', { outputLength: 256 }).toString());
63 | },
64 |
65 | testHmacHelper: function () {
66 | Y.Assert.areEqual(C.algo.HMAC.create(C.algo.SHA3, C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).finalize('Hi There').toString(), C.HmacSHA3('Hi There', C.enc.Hex.parse('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b')).toString());
67 | }
68 | }));
69 | }, '$Rev$');
70 |
--------------------------------------------------------------------------------
/src/sha256.js:
--------------------------------------------------------------------------------
1 | (function (Math) {
2 | // Shortcuts
3 | var C = CryptoJS;
4 | var C_lib = C.lib;
5 | var WordArray = C_lib.WordArray;
6 | var Hasher = C_lib.Hasher;
7 | var C_algo = C.algo;
8 |
9 | // Initialization and round constants tables
10 | var H = [];
11 | var K = [];
12 |
13 | // Compute constants
14 | (function () {
15 | function isPrime(n) {
16 | var sqrtN = Math.sqrt(n);
17 | for (var factor = 2; factor <= sqrtN; factor++) {
18 | if (!(n % factor)) {
19 | return false;
20 | }
21 | }
22 |
23 | return true;
24 | }
25 |
26 | function getFractionalBits(n) {
27 | return ((n - (n | 0)) * 0x100000000) | 0;
28 | }
29 |
30 | var n = 2;
31 | var nPrime = 0;
32 | while (nPrime < 64) {
33 | if (isPrime(n)) {
34 | if (nPrime < 8) {
35 | H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
36 | }
37 | K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
38 |
39 | nPrime++;
40 | }
41 |
42 | n++;
43 | }
44 | }());
45 |
46 | // Reusable object
47 | var W = [];
48 |
49 | /**
50 | * SHA-256 hash algorithm.
51 | */
52 | var SHA256 = C_algo.SHA256 = Hasher.extend({
53 | _doReset: function () {
54 | this._hash = new WordArray.init(H.slice(0));
55 | },
56 |
57 | _doProcessBlock: function (M, offset) {
58 | // Shortcut
59 | var H = this._hash.words;
60 |
61 | // Working variables
62 | var a = H[0];
63 | var b = H[1];
64 | var c = H[2];
65 | var d = H[3];
66 | var e = H[4];
67 | var f = H[5];
68 | var g = H[6];
69 | var h = H[7];
70 |
71 | // Computation
72 | for (var i = 0; i < 64; i++) {
73 | if (i < 16) {
74 | W[i] = M[offset + i] | 0;
75 | } else {
76 | var gamma0x = W[i - 15];
77 | var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
78 | ((gamma0x << 14) | (gamma0x >>> 18)) ^
79 | (gamma0x >>> 3);
80 |
81 | var gamma1x = W[i - 2];
82 | var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
83 | ((gamma1x << 13) | (gamma1x >>> 19)) ^
84 | (gamma1x >>> 10);
85 |
86 | W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
87 | }
88 |
89 | var ch = (e & f) ^ (~e & g);
90 | var maj = (a & b) ^ (a & c) ^ (b & c);
91 |
92 | var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
93 | var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
94 |
95 | var t1 = h + sigma1 + ch + K[i] + W[i];
96 | var t2 = sigma0 + maj;
97 |
98 | h = g;
99 | g = f;
100 | f = e;
101 | e = (d + t1) | 0;
102 | d = c;
103 | c = b;
104 | b = a;
105 | a = (t1 + t2) | 0;
106 | }
107 |
108 | // Intermediate hash value
109 | H[0] = (H[0] + a) | 0;
110 | H[1] = (H[1] + b) | 0;
111 | H[2] = (H[2] + c) | 0;
112 | H[3] = (H[3] + d) | 0;
113 | H[4] = (H[4] + e) | 0;
114 | H[5] = (H[5] + f) | 0;
115 | H[6] = (H[6] + g) | 0;
116 | H[7] = (H[7] + h) | 0;
117 | },
118 |
119 | _doFinalize: function () {
120 | // Shortcuts
121 | var data = this._data;
122 | var dataWords = data.words;
123 |
124 | var nBitsTotal = this._nDataBytes * 8;
125 | var nBitsLeft = data.sigBytes * 8;
126 |
127 | // Add padding
128 | dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
129 | dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
130 | dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
131 | data.sigBytes = dataWords.length * 4;
132 |
133 | // Hash final blocks
134 | this._process();
135 |
136 | // Return final computed hash
137 | return this._hash;
138 | },
139 |
140 | clone: function () {
141 | var clone = Hasher.clone.call(this);
142 | clone._hash = this._hash.clone();
143 |
144 | return clone;
145 | }
146 | });
147 |
148 | /**
149 | * Shortcut function to the hasher's object interface.
150 | *
151 | * @param {WordArray|string} message The message to hash.
152 | *
153 | * @return {WordArray} The hash.
154 | *
155 | * @static
156 | *
157 | * @example
158 | *
159 | * var hash = CryptoJS.SHA256('message');
160 | * var hash = CryptoJS.SHA256(wordArray);
161 | */
162 | C.SHA256 = Hasher._createHelper(SHA256);
163 |
164 | /**
165 | * Shortcut function to the HMAC's object interface.
166 | *
167 | * @param {WordArray|string} message The message to hash.
168 | * @param {WordArray|string} key The secret key.
169 | *
170 | * @return {WordArray} The HMAC.
171 | *
172 | * @static
173 | *
174 | * @example
175 | *
176 | * var hmac = CryptoJS.HmacSHA256(message, key);
177 | */
178 | C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
179 | }(Math));
180 |
--------------------------------------------------------------------------------
/test/test.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CryptoJS Test Suite
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
119 |
120 |
135 |
136 |
137 |
138 |
139 |
--------------------------------------------------------------------------------
/test/des-test.js:
--------------------------------------------------------------------------------
1 | YUI.add('algo-des-test', function (Y) {
2 | var C = CryptoJS;
3 |
4 | Y.Test.Runner.add(new Y.Test.Case({
5 | name: 'DES',
6 |
7 | testEncrypt1: function () {
8 | Y.Assert.areEqual('95a8d72813daa94d', C.DES.encrypt(C.enc.Hex.parse('0000000000000000'), C.enc.Hex.parse('8000000000000000'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
9 | },
10 |
11 | testEncrypt2: function () {
12 | Y.Assert.areEqual('1de5279dae3bed6f', C.DES.encrypt(C.enc.Hex.parse('0000000000000000'), C.enc.Hex.parse('0000000000002000'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
13 | },
14 |
15 | testEncrypt3: function () {
16 | Y.Assert.areEqual('1d1ca853ae7c0c5f', C.DES.encrypt(C.enc.Hex.parse('0000000000002000'), C.enc.Hex.parse('0000000000000000'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
17 | },
18 |
19 | testEncrypt4: function () {
20 | Y.Assert.areEqual('ac978c247863388f', C.DES.encrypt(C.enc.Hex.parse('3232323232323232'), C.enc.Hex.parse('3232323232323232'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
21 | },
22 |
23 | testEncrypt5: function () {
24 | Y.Assert.areEqual('3af1703d76442789', C.DES.encrypt(C.enc.Hex.parse('6464646464646464'), C.enc.Hex.parse('6464646464646464'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
25 | },
26 |
27 | testEncrypt6: function () {
28 | Y.Assert.areEqual('a020003c5554f34c', C.DES.encrypt(C.enc.Hex.parse('9696969696969696'), C.enc.Hex.parse('9696969696969696'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
29 | },
30 |
31 | testDecrypt1: function () {
32 | Y.Assert.areEqual('0000000000000000', C.DES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('95a8d72813daa94d') }), C.enc.Hex.parse('8000000000000000'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
33 | },
34 |
35 | testDecrypt2: function () {
36 | Y.Assert.areEqual('0000000000000000', C.DES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('1de5279dae3bed6f') }), C.enc.Hex.parse('0000000000002000'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
37 | },
38 |
39 | testDecrypt3: function () {
40 | Y.Assert.areEqual('0000000000002000', C.DES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('1d1ca853ae7c0c5f') }), C.enc.Hex.parse('0000000000000000'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
41 | },
42 |
43 | testDecrypt4: function () {
44 | Y.Assert.areEqual('3232323232323232', C.DES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('ac978c247863388f') }), C.enc.Hex.parse('3232323232323232'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
45 | },
46 |
47 | testDecrypt5: function () {
48 | Y.Assert.areEqual('6464646464646464', C.DES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('3af1703d76442789') }), C.enc.Hex.parse('6464646464646464'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
49 | },
50 |
51 | testDecrypt6: function () {
52 | Y.Assert.areEqual('9696969696969696', C.DES.decrypt(C.lib.CipherParams.create({ ciphertext: C.enc.Hex.parse('a020003c5554f34c') }), C.enc.Hex.parse('9696969696969696'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
53 | },
54 |
55 | testMultiPart: function () {
56 | var des = C.algo.DES.createEncryptor(C.enc.Hex.parse('0123456789abcdef'), { mode: C.mode.ECB, padding: C.pad.NoPadding });
57 | var ciphertext1 = des.process(C.enc.Hex.parse('001122334455'));
58 | var ciphertext2 = des.process(C.enc.Hex.parse('66778899aa'));
59 | var ciphertext3 = des.process(C.enc.Hex.parse('bbccddeeff'));
60 | var ciphertext4 = des.finalize();
61 |
62 | Y.Assert.areEqual(C.DES.encrypt(C.enc.Hex.parse('00112233445566778899aabbccddeeff'), C.enc.Hex.parse('0123456789abcdef'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString(), ciphertext1.concat(ciphertext2).concat(ciphertext3).concat(ciphertext4).toString());
63 | },
64 |
65 | testInputIntegrity: function () {
66 | var message = C.enc.Hex.parse('00112233445566778899aabbccddeeff');
67 | var key = C.enc.Hex.parse('0001020304050607');
68 | var iv = C.enc.Hex.parse('08090a0b0c0d0e0f');
69 |
70 | var expectedMessage = message.toString();
71 | var expectedKey = key.toString();
72 | var expectedIv = iv.toString();
73 |
74 | C.DES.encrypt(message, key, { iv: iv });
75 |
76 | Y.Assert.areEqual(expectedMessage, message.toString());
77 | Y.Assert.areEqual(expectedKey, key.toString());
78 | Y.Assert.areEqual(expectedIv, iv.toString());
79 | },
80 |
81 | testHelper: function () {
82 | // Save original random method
83 | var random = C.lib.WordArray.random;
84 |
85 | // Replace random method with one that returns a predictable value
86 | C.lib.WordArray.random = function (nBytes) {
87 | var words = [];
88 | for (var i = 0; i < nBytes; i += 4) {
89 | words.push([0x11223344]);
90 | }
91 |
92 | return C.lib.WordArray.create(words, nBytes);
93 | };
94 |
95 | // Test
96 | Y.Assert.areEqual(C.algo.DES.createEncryptor(C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).finalize('Hi There').toString(), C.DES.encrypt('Hi There', C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).ciphertext.toString());
97 | Y.Assert.areEqual(C.lib.SerializableCipher.encrypt(C.algo.DES, 'Hi There', C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString(), C.DES.encrypt('Hi There', C.SHA256('Jefe'), { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
98 | Y.Assert.areEqual(C.lib.PasswordBasedCipher.encrypt(C.algo.DES, 'Hi There', 'Jefe', { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString(), C.DES.encrypt('Hi There', 'Jefe', { mode: C.mode.ECB, padding: C.pad.NoPadding }).toString());
99 |
100 | // Restore random method
101 | C.lib.WordArray.random = random;
102 | }
103 | }));
104 | }, '$Rev$');
105 |
--------------------------------------------------------------------------------