This package in Go provides functions to encode a raw password (example, during registration on a site), and later verify it (example, while logging in to the site).
Functions available:
func Encode(string, *Options) (string, string) {} // takes the raw password along with options, returns generated salt and hex encoded password
func Verify(string, string, string, *Options) bool {} // takes the raw password, the generated salt, and encoded password with options, and returns true or false
The Options
struct is used to enable custom options:
type Options struct {
SaltLen int
Iterations int
KeyLen int
HashFunction func() hash.Hash
}
Passing nil
as the last argument in either function resorts to using the default options. The default options are as follows:
- Length of generated
salt
for the user is256
- Iteration count in PBKDF2 function is
10000
- Length of encoded key in PBKDF2 function is
512
- Hash algorithm used is
sha512
Hover over to Usage for a complete example.
go get github.com/anaskhan96/go-password-encoder
Run go test
in the package's directory to run tests.
Following is an example depicting the usage of this package:
package main
import (
"crypto/md5"
"fmt"
"github.com/anaskhan96/go-password-encoder"
)
func main() {
// Using the default options
salt, encodedPwd := password.Encode("generic password", nil)
check := password.Verify("generic password", salt, encodedPwd, nil)
fmt.Println(check) // true
// Using custom options
options := &password.Options{10, 10000, 50, md5.New}
salt, encodedPwd = password.Encode("generic password", options)
check = password.Verify("generic password", salt, encodedPwd, options)
fmt.Println(check) // true
}