argon2/README.md

98 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2024-07-24 23:48:36 +02:00
# Argon2 utils
## About
Utils to encrypt passwords using argon2
2024-10-13 01:30:43 +02:00
## Install
Download package in your project module:
````bash
go get -u euphoria-laxis.fr/go-packages/argon2
````
2024-07-24 23:48:36 +02:00
## Usages
### Hash password
````go
2024-10-13 01:30:43 +02:00
package main
import (
"fmt"
"euphoria-laxis.fr/go-packages/argon2/encoder"
)
func main() {
password := "qwerty@123"
2024-07-24 23:48:36 +02:00
// Create new encoder using default options
2024-10-13 01:30:43 +02:00
encode, _ := encoder.NewEncoder()
hashedString, err := encode.HashString(password)
2024-07-24 23:48:36 +02:00
if err != nil {
2024-10-13 01:30:43 +02:00
panic(err)
2024-07-24 23:48:36 +02:00
}
2024-10-13 01:30:43 +02:00
fmt.Println(hashedString)
}
2024-07-24 23:48:36 +02:00
````
### Compare password with hashed string
````go
2024-10-13 01:30:43 +02:00
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")
2024-07-24 23:48:36 +02:00
}
2024-10-13 01:30:43 +02:00
}
2024-07-24 23:48:36 +02:00
````
### 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
2024-10-13 01:30:43 +02:00
enc, options := encoder.NewEncoder(
2024-07-24 23:48:36 +02:00
SetMemory(64 * 1024), // 64 bits
SetParallelism(4), // 4 concurrent actions
SetKeyLength(32), // key length
SetSaltLength(32), // salt length
SetIterations(4), // number of iterations
)
2024-10-13 01:30:43 +02:00
// Decoder use the same functions
2024-07-24 23:48:36 +02:00
````
## Contributions
2024-10-13 01:30:43 +02:00
**Euphoria Laxis** [Gitea](https://euphoria-laxis.fr/euphoria-laxis)
2024-07-24 23:48:36 +02:00
## License
This project is under [MIT License](./LICENSE)