Documentation
¶
Index ¶
- Constants
- Variables
- func CompareHashAndPassword(ctx context.Context, hash, password string) error
- func GenerateFromPassword(ctx context.Context, password string) (string, error)
- func GenerateOtp(digits int) string
- func GeneratePassword(requiredChars []string, length int) string
- func GenerateRefreshTokenHmacKey() []byte
- func GenerateTokenHash(emailOrPhone, otp string) string
- func SecureAlphanumeric(length int) string
- type Argon2HashInput
- type EncryptedString
- type FirebaseScryptHashInput
- type HashCost
- type RefreshToken
Constants ¶
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 ¶
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") )
var ErrArgon2MismatchedHashAndPassword = errors.New("crypto: argon2 hash and password mismatch")
var ErrScryptMismatchedHashAndPassword = errors.New("crypto: fbscrypt hash and password mismatch")
var PasswordHashCost = DefaultHashCost
PasswordHashCost is the current pasword hashing cost for all new hashes generated with GenerateHashFromPassword.
Functions ¶
func CompareHashAndPassword ¶
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 ¶
GenerateFromPassword generates a password hash from a password, using PasswordHashCost. Context can be used to cancel the hashing if the algorithm supports it.
func GeneratePassword ¶
func GenerateRefreshTokenHmacKey ¶
func GenerateRefreshTokenHmacKey() []byte
func GenerateTokenHash ¶
func SecureAlphanumeric ¶
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 ParseEncryptedString ¶
func ParseEncryptedString(str string) *EncryptedString
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 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