types

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package types implements the functions, types, and interfaces for the module.

Index

Constants

View Source
const (
	// ENV is the environment variable name for hash type.
	ENV = "ORIGADMIN_HASH_TYPE"
	// DefaultSpec is the default hash type.
	DefaultSpec = "argon2"
	// DefaultVersion is the default hash version.
	DefaultVersion = "v2.0.0"
	// DefaultSaltLength is the default salt length.
	DefaultSaltLength = 16

	// ParamSeparator is the separator for parameters in a hash string.
	ParamSeparator = ","
	// ParamValueSeparator is the separator for parameter key-value pairs.
	ParamValueSeparator = ":"
	// CodecSeparator is the separator used in the encoded hash string.
	CodecSeparator = "$"
)
View Source
const (
	// UNKNOWN represents an unknown algorithm.
	UNKNOWN = "unknown"

	// MD5 is the MD5 hash algorithm.
	MD5 = "md5"
	// SHA1 is the SHA-1 hash algorithm.
	SHA1 = "sha1"
	// SHA224 is the SHA-224 hash algorithm.
	SHA224 = "sha224"
	// SHA256 is the SHA-256 hash algorithm.
	SHA256 = "sha256"
	// SHA384 is the SHA-384 hash algorithm.
	SHA384 = "sha384"
	// SHA512 is the SHA-512 hash algorithm.
	SHA512 = "sha512"

	// SHA3 is the SHA-3 hash algorithm.
	SHA3 = "sha3"
	// SHA3_224 is the SHA-3-224 hash algorithm.
	SHA3_224 = "sha3-224"
	// SHA3_256 is the SHA-3-256 hash algorithm.
	SHA3_256 = "sha3-256"
	// SHA3_384 is the SHA-3-384 hash algorithm.
	SHA3_384 = "sha3-384"
	// SHA3_512 is the SHA-3-512 hash algorithm.
	SHA3_512 = "sha3-512"
	// SHA3_512_224 is the SHA-3-512/224 hash algorithm.
	SHA3_512_224 = "sha3-512-224"
	// SHA3_512_256 is the SHA-3-512/256 hash algorithm.
	SHA3_512_256 = "sha3-512-256"

	// SHA512_224 is the truncated SHA512/224 hash algorithm.
	SHA512_224 = "sha512/224"
	// SHA512_256 is the truncated SHA512/256 hash algorithm.
	SHA512_256 = "sha512/256"

	// BLAKE2b is the BLAKE2b hash algorithm.
	BLAKE2b = "blake2b"
	// BLAKE2s is the BLAKE2s hash algorithm.
	BLAKE2s = "blake2s"
	// BLAKE2b_256 is the BLAKE2b-256 hash algorithm.
	BLAKE2b_256 = "blake2b-256"
	// BLAKE2b_384 is the BLAKE2b-384 hash algorithm.
	BLAKE2b_384 = "blake2b-384"
	// BLAKE2b_512 is the BLAKE2b-512 hash algorithm.
	BLAKE2b_512 = "blake2b-512"
	// BLAKE2s_128 is the BLAKE2s-128 hash algorithm.
	BLAKE2s_128 = "blake2s-128"
	// BLAKE2s_256 is the BLAKE2s-256 hash algorithm.
	BLAKE2s_256 = "blake2s-256"
	// DefaultBLAKE2b is the default BLAKE2b hash algorithm.
	DefaultBLAKE2b = BLAKE2b_512
	// DefaultBLAKE2s is the default BLAKE2s hash algorithm.
	DefaultBLAKE2s = BLAKE2s_256

	// ARGON2 is the Argon2 password hashing algorithm.
	ARGON2 = "argon2"
	// ARGON2i is the Argon2i password hashing algorithm.
	ARGON2i = "argon2i"
	// ARGON2id is the Argon2id password hashing algorithm.
	ARGON2id = "argon2id"
	// BCRYPT is the Bcrypt password hashing algorithm.
	BCRYPT = "bcrypt"
	// SCRYPT is the Scrypt password hashing algorithm.
	SCRYPT = "scrypt"

	// HMAC is the HMAC message authentication code algorithm.
	HMAC = "hmac"
	// PBKDF2 is the PBKDF2 key derivation function.
	PBKDF2 = "pbkdf2"

	// RIPEMD is the RIPEMD hash algorithm family.
	RIPEMD = "ripemd"
	// RIPEMD160 is the RIPEMD-160 hash algorithm.
	RIPEMD160 = "ripemd-160"
	// CRC32 is the CRC32 checksum algorithm.
	CRC32 = "crc32"
	// CRC32_ISO is the CRC32-ISO checksum algorithm.
	CRC32_ISO = "crc32-iso"
	// CRC32_CAST is the CRC32-CAST checksum algorithm.
	CRC32_CAST = "crc32-cast"
	// CRC32_KOOP is the CRC32-KOOP checksum algorithm.
	CRC32_KOOP = "crc32-koop"
	// CRC64 is the CRC64 checksum algorithm.
	CRC64 = "crc64"
	// CRC64_ISO is the CRC64-ISO checksum algorithm.
	CRC64_ISO = "crc64-iso"
	// CRC64_ECMA is the CRC64-ECMA checksum algorithm.
	CRC64_ECMA = "crc64-ecma"
)
View Source
const (
	// HMAC_SHA1 is the HMAC-SHA1 composite algorithm.
	HMAC_SHA1 = HMAC + "-" + SHA1
	// HMAC_SHA256 is the HMAC-SHA256 composite algorithm.
	HMAC_SHA256 = HMAC + "-" + SHA256
	// HMAC_SHA384 is the HMAC-SHA384 composite algorithm.
	HMAC_SHA384 = HMAC + "-" + SHA384
	// HMAC_SHA512 is the HMAC-SHA512 composite algorithm.
	HMAC_SHA512 = HMAC + "-" + SHA512
	// HMAC_SHA3_224 is the HMAC-SHA3-224 composite algorithm.
	HMAC_SHA3_224 = HMAC + "-" + SHA3_224
	// HMAC_SHA3_256 is the HMAC-SHA3-256 composite algorithm.
	HMAC_SHA3_256 = HMAC + "-" + SHA3_256
	// HMAC_SHA3_384 is the HMAC-SHA3-384 composite algorithm.
	HMAC_SHA3_384 = HMAC + "-" + SHA3_384
	// HMAC_SHA3_512 is the HMAC-SHA3-512 composite algorithm.
	HMAC_SHA3_512 = HMAC + "-" + SHA3_512
	// DefaultHMAC is the default HMAC composite algorithm.
	DefaultHMAC = HMAC_SHA256
	// HMAC_PREFIX is the prefix for HMAC composite algorithms.
	HMAC_PREFIX = HMAC + "-"

	// PBKDF2_SHA1 is the PBKDF2-SHA1 composite algorithm.
	PBKDF2_SHA1 = PBKDF2 + "-" + SHA1
	// PBKDF2_SHA256 is the PBKDF2-SHA256 composite algorithm.
	PBKDF2_SHA256 = PBKDF2 + "-" + SHA256
	// PBKDF2_SHA384 is the PBKDF2-SHA384 composite algorithm.
	PBKDF2_SHA384 = PBKDF2 + "-" + SHA384
	// PBKDF2_SHA512 is the PBKDF2-SHA512 composite algorithm.
	PBKDF2_SHA512 = PBKDF2 + "-" + SHA512
	// PBKDF2_SHA3_224 is the PBKDF2-SHA3-224 composite algorithm.
	PBKDF2_SHA3_224 = PBKDF2 + "-" + SHA3_224
	// PBKDF2_SHA3_256 is the PBKDF2-SHA3-256 composite algorithm.
	PBKDF2_SHA3_256 = PBKDF2 + "-" + SHA3_256
	// PBKDF2_SHA3_384 is the PBKDF2-SHA3-384 composite algorithm.
	PBKDF2_SHA3_384 = PBKDF2 + "-" + SHA3_384
	// PBKDF2_SHA3_512 is the PBKDF2-SHA3-512 composite algorithm.
	PBKDF2_SHA3_512 = PBKDF2 + "-" + SHA3_512
	// DefaultPBKDF2 is the default PBKDF2 composite algorithm.
	DefaultPBKDF2 = PBKDF2_SHA256
	// PBKDF2_PREFIX is the prefix for PBKDF2 composite algorithms.
	PBKDF2_PREFIX = PBKDF2 + "-"
)

Variables

This section is empty.

Functions

func Hash added in v1.1.0

func Hash(alg string) (stdhash.Hash, error)

Hash is a helper function that might need to be refactored depending on how stdhash.ParseHash is updated to handle the new Spec struct. For now, it assumes subAlg is a simple string.

Types

type Config

type Config struct {
	SaltLength int               `env:"HASH_SALTLENGTH"`
	Params     map[string]string `env:"HASH_PARAMS"`
}

Config represents the configuration for hash algorithms

func ConfigFromHashParts added in v1.2.0

func ConfigFromHashParts(parts *HashParts) *Config

ConfigFromHashParts creates a Config from a HashParts object.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig return to the default configuration

func (*Config) String added in v1.1.0

func (c *Config) String() string

type HashParts

type HashParts struct {
	// Spec is the structured identifier for the hash algorithm (e.g., {Name: "pbkdf2", Underlying: "sha256"}).
	// It is of type Spec, representing the parsed algorithm definition.
	Spec Spec `json:"spec"`

	// Version indicates a specific version of the algorithm or its parameters, if applicable.
	Version string `json:"version,omitempty"`

	// Params holds algorithm-specific parameters, such as cost, rounds, or memory usage.
	Params map[string]string `json:"params,omitempty"`

	// Hash is the raw computed hash value.
	Hash []byte `json:"hash,omitempty"`

	// Salt is the salt used during the hashing process.
	Salt []byte `json:"salt,omitempty"`
}

HashParts represents the parts of a hash, designed to be a portable data container that stores parsed algorithm information. It is suitable for serialization (e.g., to JSON) for debugging or transfer.

func FromJSON added in v1.2.0

func FromJSON(jsonStr string) (*HashParts, error)

FromJSON creates a HashParts instance from a JSON string.

func NewHashParts added in v0.3.18

func NewHashParts(spec Spec, hash, salt []byte, params ParamContainer) *HashParts

NewHashParts is the primary constructor for creating a fully-formed HashParts object. It accepts a ParamContainer interface and handles the conversion to a map internally.

func NewHashPartsWithSpec added in v1.2.0

func NewHashPartsWithSpec(spec Spec) *HashParts

func (*HashParts) AddParam added in v1.2.0

func (h *HashParts) AddParam(key, value string) *HashParts

AddParam adds a single parameter to the hash parts and returns the modified HashParts instance.

func (*HashParts) Clone added in v1.2.0

func (h *HashParts) Clone() *HashParts

func (*HashParts) DeleteParam added in v1.2.0

func (h *HashParts) DeleteParam(key string) *HashParts

DeleteParam removes a parameter from the hash parts and returns the modified HashParts instance.

func (*HashParts) IsEmpty added in v1.2.0

func (h *HashParts) IsEmpty() bool

func (*HashParts) ToJSON added in v1.2.0

func (h *HashParts) ToJSON() (string, error)

ToJSON returns the JSON string representation of HashParts. If marshaling fails, it returns an error message indicating the failure.

func (*HashParts) WithHash added in v0.3.18

func (h *HashParts) WithHash(hash []byte) *HashParts

WithHash sets the hash bytes for the hash parts and returns the modified HashParts instance.

func (*HashParts) WithHashSalt added in v0.3.18

func (h *HashParts) WithHashSalt(hash []byte, salt []byte) *HashParts

WithHashSalt sets both the hash and salt bytes for the hash parts and returns the modified HashParts instance.

func (*HashParts) WithMapParams added in v1.2.0

func (h *HashParts) WithMapParams(params map[string]string) *HashParts

func (*HashParts) WithParams added in v0.3.18

func (h *HashParts) WithParams(params ParamContainer) *HashParts

WithParams sets the parameters for the hash parts and returns the modified HashParts instance.

func (*HashParts) WithSalt added in v0.3.18

func (h *HashParts) WithSalt(salt []byte) *HashParts

WithSalt sets the salt bytes for the hash parts and returns the modified HashParts instance.

func (*HashParts) WithSpec added in v1.1.0

func (h *HashParts) WithSpec(spec Spec) *HashParts

WithSpec sets the algorithm Spec for the hash parts and returns the modified HashParts instance.

func (*HashParts) WithVersion added in v0.3.18

func (h *HashParts) WithVersion(version string) *HashParts

WithVersion sets the version string for the hash parts and returns the modified HashParts instance.

type ParamContainer added in v1.2.0

type ParamContainer interface {
	ToMap() map[string]string
	IsNil() bool
}

ParamContainer defines the interface for any object that can be converted into a parameter map. This avoids a circular dependency on the validator package, as any type that implements these methods will implicitly satisfy the interface.

type Spec added in v1.1.0

type Spec struct {
	// Name is the main algorithm's name, e.g., "hmac", "pbkdf2", "sha256".
	// This is the key field for logical dispatch.
	Name string

	// Underlying is the full string representation of the underlying hash algorithm.
	// For simple hashes, this field is empty.
	// For composite hashes, it specifies the hash function to be used,
	// e.g., "sha256" for "hmac-sha256", or "sha3-512" for "pbkdf2-sha3-512".
	Underlying string
}

Spec represents a structured hash algorithm specification. It separates the main algorithm from its underlying hash function, allowing for clear and extensible handling of composite algorithms like HMAC and PBKDF2.

func New added in v1.1.0

func New(name string, underlying ...string) Spec

New creates a new Spec instance with the specified name and underlying hash.

func Parse added in v1.1.0

func Parse(algorithm string) (Spec, error)

Parse parses an algorithm string into its structured Spec. It handles common aliases and composite algorithm formats.

func (Spec) Is added in v1.1.0

func (s Spec) Is(s2 Spec) bool

Is compares two Spec instances for equality.

func (Spec) String added in v1.1.0

func (s Spec) String() string

String returns the string representation of the spec.

Jump to

Keyboard shortcuts

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