argon2

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2025 License: MIT Imports: 7 Imported by: 0

README

Argon2 utils

About

Utils to encrypt passwords using argon2

Install

Download package in your project module:

go get -u euphoria-laxis.fr/go-packages/argon2

Usages

Hash password
package main

import (
	"fmt"

    "euphoria-laxis.fr/go-packages/argon2/encoder"
)

func main() {
    password := "qwerty@123"
    // Create new encoder using default options
    encode, _ := encoder.NewEncoder()
    hashedString, err := encode.HashString(password)
    if err != nil {
		panic(err)
    }
	fmt.Println(hashedString)
}
Compare password with hashed string
package main

import (
	"fmt"
	
	"euphoria-laxis.fr/go-packages/argon2/encoder"
	"euphoria-laxis.fr/go-packages/argon2/decoder"
)

func main() {
	password := "qwerty@123"
	// Create new encoder using default options
	enc, _ := encoder.NewEncoder()
	hashedString, err := enc.HashString(password)
	if err != nil {
		panic(err)
	}
	fmt.Println(hashedString)
	// Create new decoder using default options
	dec := decoder.NewDecoder()
	var match bool
	match, err = dec.CompareStringToHash(password, hashedString)
	if err != nil {
		panic(err)
	}
	if !match {
		fmt.Println("wrong password")
    }
}
Configure encoder or decoder options

Note that encoder and decoder inherited from the same base struct (argon2.Options). You can use the same argon2.OptFunc slice to configure both encoder and decoder.

    // Create new encoder using custom parameters
    enc, options := encoder.NewEncoder(
        SetMemory(64 * 1024), // 64 bits
        SetParallelism(4),    // 4 concurrent actions
        SetKeyLength(32),     // key length
        SetSaltLength(32),    // salt length
        SetIterations(4),     // number of iterations
    )
	// Decoder use the same functions

Contributions

Euphoria Laxis Gitea

License

This project is under MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidHash         = errors.New("the encoded hash is not in the correct format")
	ErrIncompatibleVersion = errors.New("incompatible version of argon2")
)

Functions

func NewEncoder

func NewEncoder(opts ...OptFunc) (*Encoder, *Options)

Types

type Decoder

type Decoder struct{}

func NewDecoder

func NewDecoder() *Decoder

func (*Decoder) CompareStringToHash

func (decoder *Decoder) CompareStringToHash(password string, hashedPassword string) (match bool, err error)

type Encoder

type Encoder struct {
	Options
}

func (*Encoder) HashString

func (encoder *Encoder) HashString(password string) (encodedHash string, err error)

func (*Encoder) RandomString

func (encoder *Encoder) RandomString(s int) (string, error)

type OptFunc

type OptFunc func(*Options)

func SetIterations

func SetIterations(iterations uint32) OptFunc

func SetKeyLength

func SetKeyLength(keyLength uint32) OptFunc

func SetMemory

func SetMemory(memory uint32) OptFunc

func SetParallelism

func SetParallelism(parallelism uint8) OptFunc

func SetSaltLength

func SetSaltLength(saltLength uint32) OptFunc

type Options

type Options struct {
	Memory      uint32
	Iterations  uint32
	Parallelism uint8
	SaltLength  uint32
	KeyLength   uint32
}

Jump to

Keyboard shortcuts

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