Skip to content

Commit 06967b6

Browse files
committed
Merge branch 'rewrite' of https://github.com/Alanscut/crypto-js into rewrite
2 parents c10c3b5 + 4953112 commit 06967b6

File tree

4 files changed

+66
-65
lines changed

4 files changed

+66
-65
lines changed

src/a.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/core/cipher-core.js

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
EvpKDFAlgo
1313
} from '../encryption/evpkdf.js';
1414
import { isString } from '../utils';
15+
import { Pkcs7 } from '../pad/pad-pkcs7';
16+
1517

1618
/**
1719
* Abstract base cipher template.
@@ -357,68 +359,6 @@ CBC.Decryptor = class extends CBC {
357359
}
358360
};
359361

360-
/**
361-
* PKCS #5/7 padding strategy.
362-
*/
363-
export const Pkcs7 = {
364-
/**
365-
* Pads data using the algorithm defined in PKCS #5/7.
366-
*
367-
* @param {WordArray} data The data to pad.
368-
* @param {number} blockSize The multiple that the data should be padded to.
369-
*
370-
* @static
371-
*
372-
* @example
373-
*
374-
* CryptoJS.pad.Pkcs7.pad(wordArray, 4);
375-
*/
376-
pad(data, blockSize) {
377-
// Shortcut
378-
const blockSizeBytes = blockSize * 4;
379-
380-
// Count padding bytes
381-
const nPaddingBytes = blockSizeBytes - (data.sigBytes % blockSizeBytes);
382-
383-
// Create padding word
384-
const paddingWord = (nPaddingBytes << 24) |
385-
(nPaddingBytes << 16) |
386-
(nPaddingBytes << 8) |
387-
nPaddingBytes;
388-
389-
// Create padding
390-
const paddingWords = [];
391-
for (let i = 0; i < nPaddingBytes; i += 4) {
392-
paddingWords.push(paddingWord);
393-
}
394-
const padding = new WordArray(paddingWords, nPaddingBytes);
395-
396-
// Add padding
397-
data.concat(padding);
398-
},
399-
400-
/**
401-
* Unpads data that had been padded using the algorithm defined in PKCS #5/7.
402-
*
403-
* @param {WordArray} data The data to unpad.
404-
*
405-
* @static
406-
*
407-
* @example
408-
*
409-
* CryptoJS.pad.Pkcs7.unpad(wordArray);
410-
*/
411-
unpad(data) {
412-
const _data = data;
413-
414-
// Get number of padding bytes from last byte
415-
const nPaddingBytes = _data.words[(_data.sigBytes - 1) >>> 2] & 0xff;
416-
417-
// Remove padding
418-
_data.sigBytes -= nPaddingBytes;
419-
}
420-
};
421-
422362
/**
423363
* Abstract base block cipher template.
424364
*

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
StreamCipher,
1414
BlockCipherMode,
1515
CBC,
16-
Pkcs7,
1716
BlockCipher,
1817
CipherParams,
1918
OpenSSLFormatter,
@@ -58,6 +57,7 @@ import { CTR } from './mode/mode-ctr.js';
5857
import { CTRGladman } from './mode/mode-ctr-gladman.js';
5958
import { ECB } from './mode/mode-ecb.js';
6059
import { OFB } from './mode/mode-ofb.js';
60+
import { Pkcs7 } from './pad/pad-pkcs7.js';
6161
import { AnsiX923 } from './pad/pad-ansix923.js';
6262
import { Iso10126 } from './pad/pad-iso10126.js';
6363
import { Iso97971 } from './pad/pad-iso97971.js';

src/pad/pad-pkcs7.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { WordArray } from '../core/core';
2+
3+
/**
4+
* PKCS #5/7 padding strategy.
5+
*/
6+
export const Pkcs7 = {
7+
/**
8+
* Pads data using the algorithm defined in PKCS #5/7.
9+
*
10+
* @param {WordArray} data The data to pad.
11+
* @param {number} blockSize The multiple that the data should be padded to.
12+
*
13+
* @static
14+
*
15+
* @example
16+
*
17+
* CryptoJS.pad.Pkcs7.pad(wordArray, 4);
18+
*/
19+
pad(data, blockSize) {
20+
// Shortcut
21+
const blockSizeBytes = blockSize * 4;
22+
23+
// Count padding bytes
24+
const nPaddingBytes = blockSizeBytes - (data.sigBytes % blockSizeBytes);
25+
26+
// Create padding word
27+
const paddingWord = (nPaddingBytes << 24) |
28+
(nPaddingBytes << 16) |
29+
(nPaddingBytes << 8) |
30+
nPaddingBytes;
31+
32+
// Create padding
33+
const paddingWords = [];
34+
for (let i = 0; i < nPaddingBytes; i += 4) {
35+
paddingWords.push(paddingWord);
36+
}
37+
const padding = new WordArray(paddingWords, nPaddingBytes);
38+
39+
// Add padding
40+
data.concat(padding);
41+
},
42+
43+
/**
44+
* Unpads data that had been padded using the algorithm defined in PKCS #5/7.
45+
*
46+
* @param {WordArray} data The data to unpad.
47+
*
48+
* @static
49+
*
50+
* @example
51+
*
52+
* CryptoJS.pad.Pkcs7.unpad(wordArray);
53+
*/
54+
unpad(data) {
55+
const _data = data;
56+
57+
// Get number of padding bytes from last byte
58+
const nPaddingBytes = _data.words[(_data.sigBytes - 1) >>> 2] & 0xff;
59+
60+
// Remove padding
61+
_data.sigBytes -= nPaddingBytes;
62+
}
63+
};

0 commit comments

Comments
 (0)