crypto

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2024 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 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, error)

GenerateOtp generates a random n digit otp

func GeneratePassword

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

func GenerateSignatures

func GenerateSignatures(secrets []string, msgID uuid.UUID, currentTime time.Time, inputPayload []byte) ([]string, error)

func GenerateTokenHash

func GenerateTokenHash(emailOrPhone, otp string) string

func SecureToken

func SecureToken(options ...int) string

SecureToken creates a new random token

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

Jump to

Keyboard shortcuts

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