pwd

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: MIT Imports: 8 Imported by: 0

README

pwd package

Installation

go get -u github.com/itrepablik/pwd

Argon2

Argon2 is a key derivation function that was selected as the winner of the Password Hashing Competition in July 2015. It was designed by Alex Biryukov, Daniel Dinu, and Dmitry Khovratovich from the University of Luxembourg. It was originally written in C in this repo at phc-winner-argon2 . It has a built-in Go's library at https://pkg.go.dev/golang.org/x/crypto/argon2.

Usage

This is how you can use the simplified argon2id in your next Go project.

package main

import (
	"github.com/itrepablik/pwd"
)

func main() {
	// Method 1: To initialize the pwd with custom configs, use the SetArgon2Configs() method
	var pwdConf = &pwd.Argon2Configs{
		Memory:      128 * 1024,
		Iterations:  1,
		Parallelism: 4,
		SaltLength:  16,
		KeyLength:   32,
	}
	pwdConf.SetArgon2Configs(pwdConf)

	// Generate Argon2id secured hash password
	argon2Hash, err := pwdConf.HashAndSalt("yourPlainPassword")
	if err != nil {
		errInfo := fmt.Sprintf("error hashing password: %s", err.Error())
		log.Fatal(errInfo)
		return
	}
	fmt.Println("argon2Hash: ", argon2Hash)

	// Validate Argon2id secured hash password
	isPwdCorrect, err := pwdConf.CheckPasswordHash("yourPlainPassword", argon2Hash)
	if err != nil {
		errInfo := fmt.Sprintf("error checking password: %s", err.Error())
		log.Fatal(errInfo)
		return
	}
	fmt.Println("Password is correct:", isPwdCorrect)

	// Method 2: To initialize the pwd with default configs, use the NewArgon2id() method
	var pwd = pwd.NewArgon2id()

	// Generate Argon2id secured hash password
	argon2Hash1, err := pwd.HashAndSalt("yourPlainPassword")
	if err != nil {
		errInfo := fmt.Sprintf("error hashing password: %s", err.Error())
		log.Fatal(errInfo)
		return
	}
	fmt.Println("argon2Hash1: ", argon2Hash1)

	// Validate Argon2id secured hash password
	isPwdCorrect1, err := pwd.CheckPasswordHash("yourPlainPassword", argon2Hash1)
	if err != nil {
		errInfo := fmt.Sprintf("error checking password: %s", err.Error())
		log.Fatal(errInfo)
		return
	}
	fmt.Println("Password is correct:", isPwdCorrect1)
}

Subscribe to Maharlikans Code Youtube Channel:

Please consider subscribing to my Youtube Channel to recognize my work on any of my tutorial series. Thank you so much for your support! https://www.youtube.com/c/MaharlikansCode?sub_confirmation=1

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Documentation

Index

Constants

View Source
const (
	MEMORY   uint32 = 64 * 1024
	ITER     uint32 = 1
	PARALLEL uint8  = 2
	SALT_LEN uint32 = 16
	KEY_LEN  uint32 = 32
)

Default Argon2Configs

Variables

This section is empty.

Functions

This section is empty.

Types

type Argon2Configs

type Argon2Configs struct {
	Memory      uint32 // The amount of memory to use for the operation, in KiB
	Iterations  uint32 // The number of iterations to perform
	Parallelism uint8  // The number of threads to use to compute the hash
	SaltLength  uint32 // 16 bytes is recommended for password hashing. Salt SHOULD be unique for each password
	KeyLength   uint32 // The length of the key to use (in bytes)
}

Params are the parameters to use to create an argon2id hash See https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-argon2-04#section-3.1 for more info

func NewArgon2id added in v1.0.1

func NewArgon2id() *Argon2Configs

NewArgon2id returns a new pwd Argon2id instance

func (*Argon2Configs) CheckPasswordHash added in v1.0.1

func (agc *Argon2Configs) CheckPasswordHash(password, hash string) (bool, error)

CheckPasswordHash returns true if the password hash matches the password

func (*Argon2Configs) DecodeHashPassword added in v1.0.1

func (agc *Argon2Configs) DecodeHashPassword(hash string) ([]byte, []byte, error)

DecodeHashPassword decodes the password hash and returns the salt and the key

func (*Argon2Configs) HashAndSalt added in v1.0.1

func (agc *Argon2Configs) HashAndSalt(password string) (string, error)

HashAndSalt generates a hashed password using the given password and parameters and returns the hash

func (*Argon2Configs) SetArgon2Configs added in v1.0.1

func (agc *Argon2Configs) SetArgon2Configs(conf *Argon2Configs)

SetArgon2Configs sets the custom Argon2Configs to use for Argon2 hashing.

Jump to

Keyboard shortcuts

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