lightsword
Version:
LightSword Secure SOCKS5 Proxy / iOS VPN Server
84 lines (62 loc) • 1.33 kB
text/typescript
/*
* pkcs7.pad
* https://github.com/brightcove/pkcs7
*
* Copyright (c) 2014 Brightcove
* Licensed under the apache2 license.
*/
// Pre-define the padding values
const 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]
];
export function pad(plaintext: Buffer | Uint8Array | Array<number>): Uint8Array {
var padding = PADDING[(plaintext.length % 16) || 0],
result = new Uint8Array(plaintext.length + padding.length);
result.set(<ArrayLike<number>>plaintext);
result.set(padding, plaintext.length);
return result;
};
export function unpad(padded: Buffer | Uint8Array): Uint8Array {
return padded.subarray(0, padded.length - padded[padded.length - 1]);
};
export const PKCS7Size = 16;