internal

package
v0.0.0-alpha.14 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBlockSize   = errors.New("error padding. invalid block size")
	ErrDataLength  = errors.New("error padding. invalid data length")
	ErrPaddingSize = errors.New("error padding. invalid padding size")
)

Functions

func Copy

func Copy(s cipher.Stream, dst io.Writer, src io.Reader) error

Copy copies from src to dst. If src is encrypted, then decrypted bytes are written into the dst. If src is not encrypted, then encrypted bytes are written into the dst. It uses 1 kiB internal buffer. s, dst, src must not be nil, otherwise it panics.

func MustNil

func MustNil(err error)

MustNil panics if err is not nil. MustNil is a defensive assertion used primarily in security-sensitive areas. It ensures that an error, which "should never" occur, is truly absent. This acts as a safeguard against future changes in upstream APIs.

This is intended to be used with crypto/rand.Read. As noted in the documentation for crypto/rand.Read, it does not return a non-nil error. While we believe this interface will never change, we include this check just to be absolutely sure — for security reasons.

Example:

buf := make([]byte, 10)
_, err := rand.Read(buf) // Never returns a non-nil error.
internal.MustNil(err)    // But we check anyway, just in case.

func PadISO10126

func PadISO10126(blockSize int, data []byte) ([]byte, error)

PadISO10126 appends padding bytes to given data and returns resulting slice. The given blockSize must be in a range of 1-255, otherwise ErrBlockSize is returned. For example, when blockSize is 6 and the input data is {0x61, 0x61, 0x63}, then returned data will be {0x61, 0x61, 0x63, 0xAA, 0xBB, 0x03}. The last byte (0x03) represents the length of the padding, and the preceding padding bytes (0xAA, 0xBB) are random.

func PadISO7816

func PadISO7816(blockSize int, data []byte) ([]byte, error)

PadISO7816 appends padding bytes to the given data and returns the resulting slice. The given blockSize must be in the range 1–255; otherwise, ErrBlockSize is returned. For example, when blockSize is 6 and the input data is {0x61, 0x61, 0x63}, the returned data will be {0x61, 0x61, 0x63, 0x80, 0x00, 0x00}. The byte 0x80 marks the end of the actual data, and the remaining bytes (0x00) are padding.

func PadPKCS7

func PadPKCS7(blockSize int, data []byte) ([]byte, error)

PadPKCS7 appends padding bytes to the given data and returns the resulting slice. This function can also be used for PKCS#5 padding, as PKCS#5 is a subset of PKCS#7. PKCS#7 padding values range from 0x01 to 0x10 (1 to 16 in decimal), while PKCS#5 is defined specifically for a block size of 8 bytes (0x08). The given blockSize must be in the range 1–255; otherwise, ErrBlockSize is returned. For example, when blockSize is 6 and the input data is {0x61, 0x61, 0x63}, the returned data will be {0x61, 0x61, 0x63, 0x03, 0x03, 0x03}.

func UnpadISO10126

func UnpadISO10126(blockSize int, data []byte) ([]byte, error)

UnpadISO10126 removes ISO10126 padding from the given data. It may return the following errors:

  • ErrBlockSize: the given blockSize is not in the range 1–255.
  • ErrDataLength: the length of the input data is invalid; it must be a multiple of blockSize.
  • ErrPaddingSize: the padding size embedded in the data is invalid.

func UnpadISO7816

func UnpadISO7816(blockSize int, data []byte) ([]byte, error)

UnpadISO7816 removes ISO7816 padding from the given data. It may return the following errors:

  • ErrBlockSize: the given blockSize is not in the range 1–255.
  • ErrDataLength: the length of the input data is invalid; it must be a multiple of blockSize.
  • ErrPaddingSize: the padding size in the data is invalid.

func UnpadPKCS7

func UnpadPKCS7(blockSize int, data []byte) ([]byte, error)

UnpadPKCS7 removes PKCS7 padding from the given data. It may return the following errors:

  • ErrBlockSize: the given blockSize is not in the range 1–255.
  • ErrDataLength: the length of the input data is invalid; it must be a multiple of blockSize.
  • ErrPaddingSize: the padding size in the data is invalid.

Types

type CompareSumFunc

type CompareSumFunc func(b, sum []byte) error

CompareSumFunc returns if the sum and the hash of b matches or not. If matches, it returns nil error. If not matches, it returns non-nil error.

type DecryptFunc

type DecryptFunc func(key []byte, ciphertext []byte) (plaintext []byte, err error)

DecryptFunc is the type of function that decrypts the given ciphertext.

type EncryptFunc

type EncryptFunc func(key []byte, plaintext []byte) (ciphertext []byte, err error)

EncryptFunc is the type of function that encrypts the given plaintext.

type EqualSumFunc

type EqualSumFunc func(b, sum []byte) bool

EqualSumFunc returns if the sum and the hash of b matches or not. Unlike CompareSumFunc, it returns true or false.

type HMACCompareSumFunc

type HMACCompareSumFunc func(msg, key, sum []byte) error

HMACCompareSumFunc returns if the sum and the HMAC of msg matches or not. If matches, it returns nil error. If not matches, it returns non-nil error.

type HMACEqualSumFunc

type HMACEqualSumFunc func(msg, key, sum []byte) bool

HMACEqualSumFunc returns if the sum and the HMAC of msg matches or not. Unlike HMACCompareSumFunc, it returns true or false.

type HMACSumFunc

type HMACSumFunc func(msg, key []byte) []byte

HMACSumFunc returns HMAC of the msg.

type PWHasher

type PWHasher interface {
	Sum(password []byte) ([]byte, error)
	Equal(hashedPW, pw []byte) bool
	Compare(hashedPW, pw []byte) error
}

PWHasher is the interface for password hash.

type SumFunc

type SumFunc func(b []byte) []byte

SumFunc returns hash sum of the b.

Jump to

Keyboard shortcuts

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