crypto

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultHashCost represents the default
	// hashing cost for any hashing algorithm.
	DefaultHashCost HashCost = iota

	// QuickHashCosts represents the quickest
	// hashing cost for any hashing algorithm,
	// useful for tests only.
	QuickHashCost HashCost = iota

	Argon2Prefix         = "$argon2"
	FirebaseScryptPrefix = "$fbscrypt"
	FirebaseScryptKeyLen = 32 // Firebase uses AES-256 which requires 32 byte keys: https://pkg.go.dev/golang.org/x/crypto/scrypt#Key
)

Variables

View Source
var (
	ErrRefreshTokenLength          = errors.New("crypto: refresh token length is not valid")
	ErrRefreshTokenUnknownVersion  = errors.New("crypto: refresh token version is not 0")
	ErrRefreshTokenChecksumInvalid = errors.New("crypto: refresh token checksum is not valid")
	ErrRefreshTokenCounterInvalid  = errors.New("crypto: refresh token's counter is not valid")
)
View Source
var ErrArgon2MismatchedHashAndPassword = errors.New("crypto: argon2 hash and password mismatch")
View Source
var ErrScryptMismatchedHashAndPassword = errors.New("crypto: fbscrypt hash and password mismatch")
View Source
var PasswordHashCost = DefaultHashCost

PasswordHashCost is the current pasword hashing cost for all new hashes generated with GenerateHashFromPassword.

Functions

func CompareHashAndPassword

func CompareHashAndPassword(ctx context.Context, hash, password string) error

CompareHashAndPassword compares the hash and password, returns nil if equal otherwise an error. Context can be used to cancel the hashing if the algorithm supports it.

func GenerateFromPassword

func GenerateFromPassword(ctx context.Context, password string) (string, error)

GenerateFromPassword generates a password hash from a password, using PasswordHashCost. Context can be used to cancel the hashing if the algorithm supports it.

func GenerateOtp

func GenerateOtp(digits int) string

GenerateOtp generates a random n digit otp

func GeneratePassword

func GeneratePassword(requiredChars []string, length int) string

func GenerateRefreshTokenHmacKey

func GenerateRefreshTokenHmacKey() []byte

func GenerateTokenHash

func GenerateTokenHash(emailOrPhone, otp string) string

func SecureAlphanumeric

func SecureAlphanumeric(length int) string

SecureAlphanumeric generates a secure random alphanumeric string using standard library

Types

type Argon2HashInput

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

func ParseArgon2Hash

func ParseArgon2Hash(hash string) (*Argon2HashInput, error)

type EncryptedString

type EncryptedString struct {
	KeyID     string `json:"key_id"`
	Algorithm string `json:"alg"`
	Data      []byte `json:"data"`
	Nonce     []byte `json:"nonce,omitempty"`
}

func NewEncryptedString

func NewEncryptedString(id string, data []byte, keyID string, keyBase64URL string) (*EncryptedString, error)

func ParseEncryptedString

func ParseEncryptedString(str string) *EncryptedString

func (*EncryptedString) Decrypt

func (es *EncryptedString) Decrypt(id string, decryptionKeys map[string]string) ([]byte, error)

func (*EncryptedString) IsValid

func (es *EncryptedString) IsValid() bool

func (*EncryptedString) ShouldReEncrypt

func (es *EncryptedString) ShouldReEncrypt(encryptionKeyID string) bool

ShouldReEncrypt tells you if the value encrypted needs to be encrypted again with a newer key.

func (*EncryptedString) String

func (es *EncryptedString) String() string

type FirebaseScryptHashInput

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

func ParseFirebaseScryptHash

func ParseFirebaseScryptHash(hash string) (*FirebaseScryptHashInput, error)

See: https://github.com/firebase/scrypt for implementation

type HashCost

type HashCost = int

type RefreshToken

type RefreshToken struct {
	Raw []byte

	Version   byte
	SessionID uuid.UUID
	Counter   int64
	Signature []byte
}

RefreshToken is an object that encodes a cryptographically authenticated (signed) message containing a version, session ID and monotonically increasing non-negative counter.

The signature is a truncated (first 128 bits) of HMAC-SHA-256, which saves on encoded length without sacrificing security. The checksum of 4 bytes at the end is to lessen the load on the server with invalid strings (those that are not likely to be a proper refresh token).

func ParseRefreshToken

func ParseRefreshToken(token string) (*RefreshToken, error)

func (*RefreshToken) CheckSignature

func (r *RefreshToken) CheckSignature(hmacSha256Key []byte) bool

func (*RefreshToken) Encode

func (r *RefreshToken) Encode(hmacSha256Key []byte) string

func (RefreshToken) TableName

func (RefreshToken) TableName() string

Jump to

Keyboard shortcuts

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