padding

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2024 License: BSD-3-Clause Imports: 2 Imported by: 33

Documentation

Overview

Package padding provides functions for padding blocks of plain text in the context of block cipher mode of encryption like ECB or CBC.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Padder

type Padder struct {
	// contains filtered or unexported fields
}

Padder struct embeds attributes necessary for the padding calculation (e.g. block size). It implements the Padding interface.

func (*Padder) Pad

func (p *Padder) Pad(buf []byte) ([]byte, error)

Pad returns the byte array passed as a parameter padded with bytes such that the new byte array will be an exact multiple of the expected block size. For example, if the expected block size is 8 bytes (e.g. PKCS #5) and that the initial byte array is:

[]byte{0x0A, 0x0B, 0x0C, 0x0D}

the returned array will be:

[]byte{0x0A, 0x0B, 0x0C, 0x0D, 0x04, 0x04, 0x04, 0x04}

The value of each octet of the padding is the size of the padding. If the array passed as a parameter is already an exact multiple of the block size, the original array will be padded with a full block.

Example
p := []byte{0x0A, 0x0B, 0x0C, 0x0D}
fmt.Printf("%X\n", p)
padder := NewPkcs5Padding()
p, err := padder.Pad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%X\n", p)
Output:

0A0B0C0D
0A0B0C0D04040404
Example (Second)
p := []byte{0x0A, 0x0B, 0x0C, 0x0D, 0x0A, 0x0B, 0x0C, 0x0D}
fmt.Printf("%X\n", p)
padder := NewPkcs5Padding()
p, err := padder.Pad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%X\n", p)
Output:

0A0B0C0D0A0B0C0D
0A0B0C0D0A0B0C0D0808080808080808
Example (Third)
p := []byte{0x0A, 0x0B, 0x0C, 0x0D}
fmt.Printf("%X\n", p)
padder := NewPkcs7Padding(16) // 16-byte block size
p, err := padder.Pad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%X\n", p)
Output:

0A0B0C0D
0A0B0C0D0C0C0C0C0C0C0C0C0C0C0C0C

func (*Padder) Unpad

func (p *Padder) Unpad(buf []byte) ([]byte, error)

Unpad removes the padding of a given byte array, according to the same rules as described in the Pad function. For example if the byte array passed as a parameter is:

[]byte{0x0A, 0x0B, 0x0C, 0x0D, 0x04, 0x04, 0x04, 0x04}

the returned array will be:

[]byte{0x0A, 0x0B, 0x0C, 0x0D}
Example
p := []byte{0x0A, 0x0B, 0x0C, 0x0D, 0x04, 0x04, 0x04, 0x04}
fmt.Printf("%X\n", p)
padder := NewPkcs5Padding()
p, err := padder.Unpad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%X\n", p)
Output:

0A0B0C0D04040404
0A0B0C0D
Example (Empty)
p := []byte{0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08}
fmt.Printf("%X\n", p)
padder := NewPkcs7Padding(8) // 8-byte block size
p, err := padder.Unpad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%v\n", p)
Output:

0808080808080808
[]
Example (Lastzero)
p := []byte{0x0A, 0x0B, 0x0C, 0x0D, 0x04, 0x04, 0x04, 0x00}
fmt.Printf("%X\n", p)
padder := NewPkcs5Padding()
p, err := padder.Unpad(p)
if err != nil {
	fmt.Println(err)
} else {
	fmt.Printf("%X\n", p)
}
Output:

0A0B0C0D04040400
crypto/padding: invalid last byte of padding
Example (Second)
p := []byte{0x0A, 0x0B, 0x0C, 0x0D, 0x0A, 0x0B, 0x0C, 0x0D,
	0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08}
fmt.Printf("%X\n", p)
padder := NewPkcs5Padding()
p, err := padder.Unpad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%X\n", p)
Output:

0A0B0C0D0A0B0C0D0808080808080808
0A0B0C0D0A0B0C0D
Example (Third)
p := []byte{0x0A, 0x0B, 0x0C, 0x0D, 0x0C, 0x0C, 0x0C, 0x0C,
	0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C}
fmt.Printf("%X\n", p)
padder := NewPkcs7Padding(16) // 16-byte block size
p, err := padder.Unpad(p)
if err != nil {
	panic(err.Error())
}
fmt.Printf("%X\n", p)
Output:

0A0B0C0D0C0C0C0C0C0C0C0C0C0C0C0C
0A0B0C0D

type Padding

type Padding interface {
	Pad(p []byte) ([]byte, error)
	Unpad(p []byte) ([]byte, error)
}

Padding interface defines functions Pad and Unpad implemented for PKCS #5 and PKCS #7 types of padding.

func NewPkcs5Padding

func NewPkcs5Padding() Padding

NewPkcs5Padding returns a PKCS5 padding type structure. The blocksize defaults to 8 bytes (64-bit). See https://tools.ietf.org/html/rfc2898 PKCS #5: Password-Based Cryptography. Specification Version 2.0

func NewPkcs7Padding

func NewPkcs7Padding(blockSize int) Padding

NewPkcs7Padding returns a PKCS7 padding type structure. The blocksize is passed as a parameter. See https://tools.ietf.org/html/rfc2315 PKCS #7: Cryptographic Message Syntax Version 1.5. For example the block size for AES is 16 bytes (128 bits).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL