Documentation
¶
Index ¶
- Constants
- Variables
- func CreateArgon2(cfg Argon2, password string) (string, error)
- func CreateBcrypt(cost int, password string) (string, error)
- func CreateMD5(str string) string
- func CreatePBKDF2(cfg PBKDF2, password string) (string, error)
- func CreateScrypt(cfg Scrypt, password string) (string, error)
- func Hash(password string, opts ...Option) (string, error)
- func NeedsRehash(encodedHash string, policy Policy) (bool, error)
- func Verify(password, encodedHash string) (bool, error)
- func VerifyAndUpgrade(password, encodedHash string, policy Policy) (ok bool, newHash string, upgraded bool, err error)
- func VerifyArgon2(password, storedHash string) (bool, error)
- func VerifyBcrypt(password, storedHash string) bool
- func VerifyPBKDF2(password string, storedStr string) (bool, error)
- func VerifyScrypt(password, storedHash string) (bool, error)
- type Algorithm
- type Argon2
- type Option
- type Options
- type PBKDF2
- type Policy
- type Scrypt
Examples ¶
Constants ¶
const DefaultBcryptCost = 12
const DefaultSalt = "AwesomeGolangCrypto"
DefaultSalt is a package-wide password suffix retained for backwards compatibility. It behaves closer to a global pepper than to a per-hash random salt.
Variables ¶
var ( ErrInvalidHashFormat = errors.New("easyhash: invalid hash format") ErrUnsupportedAlgorithm = errors.New("easyhash: unsupported algorithm") )
Functions ¶
func CreateArgon2 ¶
CreateArgon2 generates an Argon2id hash for the given password using the provided configuration. Returns a base64-encoded string in format: salt:time:memory:threads:hash
func CreateBcrypt ¶
CreateBcrypt generates a bcrypt hash for the given password with the specified cost. Cost should be between 4 and 31, with 12-14 being recommended for most applications.
func CreateMD5 ¶
CreateMD5 creates an MD5 hash of the input string. WARNING: MD5 is cryptographically broken and should not be used for password hashing. This function is provided for legacy compatibility only.
func CreatePBKDF2 ¶
CreatePBKDF2 generates a PBKDF2 hash for the given password using the provided configuration. Returns a base64-encoded string in format: salt:iterations:hash
func CreateScrypt ¶
CreateScrypt generates a scrypt hash for the given password using the provided configuration. Returns a base64-encoded string in format: salt:N:r:p:hash
func Hash ¶
Hash hashes a password using the selected high-level algorithm. The default algorithm is PBKDF2-SHA256.
Example ¶
package main
import (
"fmt"
"github.com/gofurry/easyhash"
)
func main() {
hash, err := easyhash.Hash("12345678")
if err != nil {
fmt.Println("error")
return
}
algorithm, err := easyhash.Identify(hash)
if err != nil {
fmt.Println("error")
return
}
fmt.Println(algorithm)
}
Output: pbkdf2-sha256
func NeedsRehash ¶
NeedsRehash reports whether a stored hash should be upgraded to match the policy.
func VerifyAndUpgrade ¶
func VerifyAndUpgrade(password, encodedHash string, policy Policy) (ok bool, newHash string, upgraded bool, err error)
VerifyAndUpgrade verifies a password and returns a replacement hash when policy requires it.
Example ¶
package main
import (
"fmt"
"github.com/gofurry/easyhash"
)
func main() {
legacy, err := easyhash.CreateArgon2(easyhash.DefaultArgon2(), "12345678")
if err != nil {
fmt.Println("error")
return
}
ok, newHash, upgraded, err := easyhash.VerifyAndUpgrade("12345678", legacy, easyhash.DefaultPolicy())
if err != nil {
fmt.Println("error")
return
}
algorithm, err := easyhash.Identify(newHash)
if err != nil {
fmt.Println("error")
return
}
fmt.Println(ok, upgraded, algorithm)
}
Output: true true pbkdf2-sha256
func VerifyArgon2 ¶
VerifyArgon2 verifies a password against a stored Argon2 hash
func VerifyBcrypt ¶
VerifyBcrypt verifies a password against a stored bcrypt hash
func VerifyPBKDF2 ¶
VerifyPBKDF2 verifies a password against a stored PBKDF2 hash
func VerifyScrypt ¶
VerifyScrypt verifies a password against a stored scrypt hash
Types ¶
type Algorithm ¶
type Algorithm string
Algorithm identifies the password hashing algorithm used by a stored hash.
type Argon2 ¶
type Argon2 struct {
// contains filtered or unexported fields
}
Argon2 configuration struct for Argon2id password hashing
func DefaultArgon2 ¶
func DefaultArgon2() Argon2
DefaultArgon2 returns an Argon2 configuration with secure default values
type Option ¶
type Option func(*Options)
Option mutates high-level hash options.
func WithArgon2idConfig ¶
WithArgon2idConfig overrides the Argon2id configuration for Hash.
func WithBcryptCost ¶
WithBcryptCost overrides the bcrypt cost for Hash.
func WithPBKDF2Config ¶
WithPBKDF2Config overrides the PBKDF2 configuration for Hash.
func WithScryptConfig ¶
WithScryptConfig overrides the scrypt configuration for Hash.
type Options ¶
type Options struct {
Algorithm Algorithm
Argon2id Argon2
PBKDF2 PBKDF2
Scrypt Scrypt
BcryptCost int
}
Options configures the high-level Hash API.
func DefaultOptions ¶
func DefaultOptions() Options
DefaultOptions returns the default high-level hashing configuration.
type PBKDF2 ¶
type PBKDF2 struct {
PBKDF2Iterations int // Number of iterations for key derivation
PBKDF2KeyLength int // Length of the derived key in bytes
SaltLength int // Length of the random salt in bytes
}
PBKDF2 configuration struct for PBKDF2 password hashing
func DefaultPBKDF2 ¶
func DefaultPBKDF2() PBKDF2
DefaultPBKDF2 returns a PBKDF2 configuration with secure default values
type Policy ¶
type Policy struct {
PreferredAlgorithm Algorithm
Argon2id Argon2
PBKDF2 PBKDF2
Scrypt Scrypt
BcryptCost int
AllowLegacyMD5 bool
}
Policy describes when a stored hash should be upgraded.
func DefaultPolicy ¶
func DefaultPolicy() Policy
DefaultPolicy returns the default migration target for the high-level API.
func LowMemoryPolicy ¶
func LowMemoryPolicy() Policy
LowMemoryPolicy keeps Argon2id resource usage lower for constrained environments.
func StrongPolicy ¶
func StrongPolicy() Policy
StrongPolicy uses more conservative defaults for new password hashes.
type Scrypt ¶
type Scrypt struct {
// contains filtered or unexported fields
}
Scrypt configuration struct for scrypt password hashing
func DefaultScrypt ¶
func DefaultScrypt() Scrypt
DefaultScrypt returns a Scrypt configuration with secure default values