blob: 995ed9f31b1b9f282d2bd8740310ff3e5ff9a138 [file] [log] [blame]
Alex Gaynor5f3db272013-10-29 10:56:35 -07001.. danger::
2
3 This is a "Hazardous Materials" module. You should **ONLY** use it if
4 you're 100% absolutely sure that you know what you're doing because this
5 module is full of land mines, dragons, and dinosaurs with laser guns.
6
7
8Padding
9=======
10
11
12Padding is a way to take data that may or may not be be a multiple of the block
13size for a cipher and extend it out so that it is. This is required for many
14block cipher modes as they require the data to be encrypted to be an exact
15multiple of the block size.
16
17
18.. class:: cryptography.primitives.padding.PKCS7(block_size)
19
20 PKCS7 padding is a generalization of PKCS5 padding (also known as standard
21 padding). PKCS7 padding works by appending ``N`` bytes with the value of
22 ``chr(N)``, where ``N`` is the number of bytes required to make the final
23 block of data the same size as the block size. A simple example of padding
24 is:
25
26 .. doctest::
27
28 >>> from cryptography.primitives import padding
29 >>> padder = padding.PKCS7(128)
30 >>> padder.pad(b"1111111111")
31 '1111111111\x06\x06\x06\x06\x06\x06'
32
33 :param block_size: The size of the block in bits that the data is being
34 padded to.
35
36 .. method:: pad(data)
37
38 :param data: The data that should be padded.
39 :rtype bytes: The padded data.
40
41 .. method:: unpad(data)
42
43 :param data: The data that should be unpadded.
44 :rtype bytes: The unpadded data.