Skip to main content

Padding package when using block cryptography

Project description

# PyPadding

[![Build Status](https://travis-ci.org/blinglnav/pypadding.svg?branch=master)](https://travis-ci.org/blinglnav/pypadding)

This package implements padding methods to use block crypto function.

## Dependencies

* Python 3+

## Usage

### PKCS#5 / PKCS#7
* pypadding.pkcs
* Fill using number of padding number
* e.g. When block size is 8, b'hello' need 3 bytes to multiple of block size --> fill using `b'\x03'` (`b'hello\x03\x03\x03'`)

```python
>>> from pypadding import pkcs
>>> encoder = pkcs.Encoder()
>>> encoder.encode(b'hello')
b'hello\x03\x03\x03'
>>> encoder.decode(b'hello\x03\x03\x03')
b'hello'
```

### ANSI x923
* pypadding.x923
* Fill using zero(`b'\x00'`) and last byte set to length of padding
* e.g. When block size is 8, b'hello' need 3 bytes to multiple of block size --> fill using `b'\x00'` and last byte set to `b'\x03'` (`b'hello\x00\x00\x03'`)

```python
>>> from pypadding import x923
>>> encoder = x923.Encoder()
>>> encoder.encode(b'hello')
b'hello\x00\x00\x03'
>>> encoder.decode(b'hello\x00\x00\x03')
b'hello'
```

### ISO 10126
* pypadding.iso10126
* Fill using random byte and last byte set to length of padding
* e.g. When block size is 8, b'hello' need 3 bytes to multiple of block size --> fill random byte and last byte set to `b'\x03'` (`b'hello\x85\xaa\x03'`)

```python
>>> from pypadding import iso10126
>>> encoder = iso10126.Encoder()
>>> encoder.encode(b'hello')
b'hello\x85\xaa\x03'
>>> encoder.decode(b'hello\x85\xaa\x03')
b'hello'
```

### ISO/IEC 7816-4
* pypadding.iso7816_4
* Padding starts with `b'\x80'` and fill using zero `b'\x00'`
* e.g. When block size is 8, b'hello' need 3 bytes to multiple of block size --> fill first byte to `b'\x80'` then fill to `b'\x00'` (`b'hello\x80\x00\x00'`)

```python
>>> from pypadding import iso7816_4
>>> encoder = iso7816_4.Encoder()
>>> encoder.encode(b'hello')
b'hello\x80\x00\x00'
>>> encoder.decode(b'hello\x80\x00\x00')
b'hello'
```

## Set Block Size

```python
>>> from pypadding import pkcs
>>> encoder = pkcs.Encoder(16)
>>> encoder.encode(b'blackjack')
b'blackjack\x07\x07\x07\x07\x07\x07\x07'
>>> encoder.decode(b'blackjack\x07\x07\x07\x07\x07\x07\x07')
b'blackjack'
```

or

```python
>>> from pypadding import pkcs
>>> encoder = pkcs.Encoder(block_size=16)
>>> encoder.encode(b'blackjack')
b'blackjack\x07\x07\x07\x07\x07\x07\x07'
>>> encoder.decode(b'blackjack\x07\x07\x07\x07\x07\x07\x07')
b'blackjack'
```


## Note
* All encoded data has padding even though length of original data is multiple of block size
* e.g. block size = 8, encoding w/ pkcs, `encode('computer')` --> `b'computer\x08\x08\x08\x08\x08\x08\x08\x08'`


## Reference
* https://en.wikipedia.org/wiki/Padding_(cryptography)
* This package implement reversible methods only


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyPadding-1.0.3.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

PyPadding-1.0.3-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file PyPadding-1.0.3.tar.gz.

File metadata

  • Download URL: PyPadding-1.0.3.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.4.6

File hashes

Hashes for PyPadding-1.0.3.tar.gz
Algorithm Hash digest
SHA256 2ace46ff12d9fe936e7b3ccbd2c19966b3b0d52b92a231493a01b81a44111c66
MD5 4958eabb7cfd7db78f981bc180a54150
BLAKE2b-256 f538b1ddc4ce2161b5217acb8ed32aa7f3c914263c8a328c3a3fef6b3ec73c59

See more details on using hashes here.

File details

Details for the file PyPadding-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: PyPadding-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.4.6

File hashes

Hashes for PyPadding-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a4fba74b897a071d1d3f9822b1a9772a0e86dc9b9fbeaef176f5961d5ffb323b
MD5 5484d949f4ed2d1e995539aefcb81c8c
BLAKE2b-256 e39439e3a0a720e2df5b619e1874c765a017a3aca84c5e7037a40c123c55fe49

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page