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