cpt

package
v1.2.21 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

View Source
const (
	SecretChars = str.Base64URL
)

Variables

View Source
var (
	ErrTokenLength    = errors.New("invalid token length")
	ErrTokenTimestamp = errors.New("invalid token timestamp")
)
View Source
var (
	ErrCipherBytesTooShort = errors.New("cipher bytes too short")
)

Functions

func Pkcs7Pad

func Pkcs7Pad(data []byte, size int) []byte

Pkcs7Pad 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.

func Pkcs7Unpad

func Pkcs7Unpad(data []byte) []byte

Pkcs7Unpad 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}

func Salt

func Salt(chars, salt, src string) string

func Unsalt

func Unsalt(chars, salt, src string) string

Types

type Blocker added in v1.2.20

type Blocker interface {
	EncryptBlocks(src []byte) ([]byte, error)

	DecryptBlocks(src []byte) ([]byte, error)
}

type Cryptor

type Cryptor interface {
	Encryptor
	Decryptor
}

Cryptor concurrent safe cryptor

func NewAes128CBCCryptor added in v1.2.20

func NewAes128CBCCryptor(secret, hkdfinfo string) (Cryptor, error)

func NewAes128GCMCryptor added in v1.2.20

func NewAes128GCMCryptor(secret, hkdfinfo string) (Cryptor, error)

func NewAes192CBCCryptor added in v1.2.20

func NewAes192CBCCryptor(secret, hkdfinfo string) (Cryptor, error)

func NewAes192GCMCryptor added in v1.2.20

func NewAes192GCMCryptor(secret, hkdfinfo string) (Cryptor, error)

func NewAes256CBCCryptor added in v1.2.20

func NewAes256CBCCryptor(secret, hkdfinfo string) (Cryptor, error)

func NewAes256GCMCryptor added in v1.2.20

func NewAes256GCMCryptor(secret, hkdfinfo string) (Cryptor, error)

func NewAesCBCCryptor added in v1.2.20

func NewAesCBCCryptor(secret, hkdfinfo string, bits int) (Cryptor, error)

func NewAesGCMCryptor added in v1.2.20

func NewAesGCMCryptor(secret, hkdfinfo string, bits int) (Cryptor, error)

type Decryptor added in v1.0.23

type Decryptor interface {
	DecryptString(string) (string, error)
	DecryptBytes([]byte) ([]byte, error)
}

Decryptor concurrent safe decryptor

type Encryptor added in v1.0.23

type Encryptor interface {
	EncryptString(string) (string, error)
	EncryptBytes([]byte) ([]byte, error)
}

Encryptor concurrent safe encryptor

type Padder added in v1.0.23

type Padder int

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

func (Padder) Pad added in v1.0.23

func (p Padder) Pad(data []byte) []byte

func (Padder) Unpad added in v1.0.23

func (p Padder) Unpad(data []byte) ([]byte, error)

type Padding added in v1.0.23

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

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

func NewPkcs5Padding added in v1.0.23

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 added in v1.0.23

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).

type Token

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

func ParseToken

func ParseToken(token string) (t *Token, err error)

func RandomToken added in v1.0.27

func RandomToken() *Token

RandomToken create a random `randLenth` secret token.

func SecretToken added in v1.0.27

func SecretToken(secret string) *Token

SecretToken create a token with secret.

func (*Token) Refresh

func (t *Token) Refresh()

func (*Token) Salt

func (t *Token) Salt() string

func (*Token) Secret

func (t *Token) Secret() string

func (*Token) String

func (t *Token) String() string

func (*Token) Timestamp

func (t *Token) Timestamp() time.Time

func (*Token) Token

func (t *Token) Token() string

type Tokener

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

func NewTokener

func NewTokener(saltLength, randLength int) *Tokener

NewTokener create a tokener.

func (*Tokener) ParseToken

func (tr *Tokener) ParseToken(token string) (*Token, error)

func (*Tokener) RandomToken added in v1.0.27

func (tr *Tokener) RandomToken() *Token

RandomToken create a random `randLenth` secret token.

func (*Tokener) SecretToken added in v1.0.27

func (tr *Tokener) SecretToken(secret string) *Token

SecretToken create a token with secret. If secret is empty, a random `randLenth` secret will be used.

Jump to

Keyboard shortcuts

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