98 lines
1.9 KiB
Markdown
98 lines
1.9 KiB
Markdown
# Argon2 utils
|
|
|
|
## About
|
|
|
|
Utils to encrypt passwords using argon2
|
|
|
|
## Install
|
|
|
|
Download package in your project module:
|
|
|
|
````bash
|
|
go get -u euphoria-laxis.fr/go-packages/argon2
|
|
````
|
|
|
|
## Usages
|
|
|
|
### Hash password
|
|
|
|
````go
|
|
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
|
|
|
|
````go
|
|
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.
|
|
|
|
````go
|
|
// 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](https://euphoria-laxis.fr/euphoria-laxis)
|
|
|
|
## License
|
|
|
|
This project is under [MIT License](./LICENSE)
|