Add padding to the key.
(key)
| 62 | |
| 63 | |
| 64 | def pad(key): |
| 65 | """Add padding to the key.""" |
| 66 | |
| 67 | if isinstance(key, str): |
| 68 | key = key.encode() |
| 69 | |
| 70 | # Key must be maximum 32 bytes long, so take first 32 bytes |
| 71 | key = key[:32] |
| 72 | |
| 73 | # If key size is 16, 24 or 32 bytes then padding is not required |
| 74 | if len(key) in (16, 24, 32): |
| 75 | return key |
| 76 | |
| 77 | # Add padding to make key 32 bytes long |
| 78 | return key.ljust(32, padding_string) |