easyhash

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 14 Imported by: 0

README

easyhash

Last Version License Go Version

English | 中文文档

A simple Go password hashing helper that provides create/verify helpers for MD5 (legacy only), PBKDF2, Argon2id, scrypt, and bcrypt, with built-in default parameters.

Installation

# If you use private modules, set GOPRIVATE to avoid public checksum database lookups.
go get github.com/GoFurry/easyhash

Quick Start

package main

import (
	"fmt"

	"github.com/GoFurry/easyhash"
)

func main() {
	pass := "12345678"

	// PBKDF2
	pbkdf2, _ := easyhash.CreatePBKDF2(easyhash.DefaultPBKDF2(), pass)
	ok, _ := easyhash.VerifyPBKDF2(pass, pbkdf2)
	fmt.Println("pbkdf2:", ok)

	// Argon2id
	argon2, _ := easyhash.CreateArgon2(easyhash.DefaultArgon2(), pass)
	ok, _ = easyhash.VerifyArgon2(pass, argon2)
	fmt.Println("argon2:", ok)

	// Scrypt
	scrypt, _ := easyhash.CreateScrypt(easyhash.DefaultScrypt(), pass)
	ok, _ = easyhash.VerifyScrypt(pass, scrypt)
	fmt.Println("scrypt:", ok)

	// Bcrypt
	bcrypt, _ := easyhash.CreateBcrypt(12, pass)
	ok = easyhash.VerifyBcrypt(pass, bcrypt)
	fmt.Println("bcrypt:", ok)

	// MD5 (legacy / not recommended)
	fmt.Println("md5:", easyhash.CreateMD5(pass))
}

Notes

  • The default global salt is DefaultSalt, appended to the password before hashing.
  • Prefer Argon2id or bcrypt for password hashing.
  • Example code: example/userPassword.go.

Documentation

Index

Constants

View Source
const DefaultSalt = "AwesomeGolangCrypto"

DefaultSalt is a global salt used across all hashing algorithms for additional security

Variables

This section is empty.

Functions

func CreateArgon2

func CreateArgon2(cfg Argon2, password string) (string, error)

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

func CreateBcrypt(cost int, password string) (string, error)

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

func CreateMD5(str string) string

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

func CreatePBKDF2(cfg PBKDF2, password string) (string, error)

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

func CreateScrypt(cfg Scrypt, password string) (string, error)

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 VerifyArgon2

func VerifyArgon2(password, storedHash string) (bool, error)

VerifyArgon2 verifies a password against a stored Argon2 hash

func VerifyBcrypt

func VerifyBcrypt(password, storedHash string) bool

VerifyBcrypt verifies a password against a stored bcrypt hash

func VerifyPBKDF2

func VerifyPBKDF2(password string, storedStr string) (bool, error)

VerifyPBKDF2 verifies a password against a stored PBKDF2 hash

func VerifyScrypt

func VerifyScrypt(password, storedHash string) (bool, error)

VerifyScrypt verifies a password against a stored scrypt hash

Types

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 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 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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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