pad.js
1.33 KB
1
2
3
4
5
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
/*
* pkcs7.pad
* https://github.com/brightcove/pkcs7
*
* Copyright (c) 2014 Brightcove
* Licensed under the apache2 license.
*/
let PADDING;
/**
* Returns a new Uint8Array that is padded with PKCS#7 padding.
*
* @param plaintext {Uint8Array} the input bytes before encryption
* @return {Uint8Array} the padded bytes
* @see http://tools.ietf.org/html/rfc5652
*/
export default function pad(plaintext) {
const padding = PADDING[(plaintext.byteLength % 16) || 0];
const result = new Uint8Array(plaintext.byteLength + padding.length);
result.set(plaintext);
result.set(padding, plaintext.byteLength);
return result;
}
// pre-define the padding values
PADDING = [
[16, 16, 16, 16,
16, 16, 16, 16,
16, 16, 16, 16,
16, 16, 16, 16],
[15, 15, 15, 15,
15, 15, 15, 15,
15, 15, 15, 15,
15, 15, 15],
[14, 14, 14, 14,
14, 14, 14, 14,
14, 14, 14, 14,
14, 14],
[13, 13, 13, 13,
13, 13, 13, 13,
13, 13, 13, 13,
13],
[12, 12, 12, 12,
12, 12, 12, 12,
12, 12, 12, 12],
[11, 11, 11, 11,
11, 11, 11, 11,
11, 11, 11],
[10, 10, 10, 10,
10, 10, 10, 10,
10, 10],
[9, 9, 9, 9,
9, 9, 9, 9,
9],
[8, 8, 8, 8,
8, 8, 8, 8],
[7, 7, 7, 7,
7, 7, 7],
[6, 6, 6, 6,
6, 6],
[5, 5, 5, 5,
5],
[4, 4, 4, 4],
[3, 3, 3],
[2, 2],
[1]
];