Merge pull request #4 from euphoria-laxis/release/v2.0.1
Release/v2.0.1
This commit is contained in:
commit
0c73f2cc50
48
README.md
48
README.md
|
@ -4,30 +4,46 @@
|
||||||
|
|
||||||
Utils to encrypt passwords using argon2
|
Utils to encrypt passwords using argon2
|
||||||
|
|
||||||
## Usage
|
## Usages
|
||||||
|
|
||||||
### Example
|
### Hash password
|
||||||
|
|
||||||
````go
|
````go
|
||||||
func func main() {
|
|
||||||
password := 'qwerty@123'
|
password := 'qwerty@123'
|
||||||
hashedString, err := argon2_utils.HashStringArgon2(password)
|
// Create new encoder using default options
|
||||||
|
encoder, _ := argon2.NewEncoder()
|
||||||
|
hashedString, err = encoder.HashString(randomString)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
...
|
// handle error
|
||||||
}
|
|
||||||
match, err := argon2_utils.CompareStringToArgon2Hash(password, hashedString)
|
|
||||||
if err != nil {
|
|
||||||
...
|
|
||||||
}
|
|
||||||
if !match {
|
|
||||||
log.Println("passwords don't match")
|
|
||||||
} else {
|
|
||||||
log.Println("passwords match")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
````
|
````
|
||||||
|
|
||||||
This package also contains a **RandomString(int)(string,error)** function.
|
### Compare password with hashed string
|
||||||
|
|
||||||
|
````go
|
||||||
|
// Create new decoder using default options
|
||||||
|
decoder, _ := argon2.NewDecoder()
|
||||||
|
match, err := decoder.CompareStringToHash(password, hashedString)
|
||||||
|
if err != nil {
|
||||||
|
// handle error
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
### 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
|
||||||
|
encoder, options := argon2.NewEncoder(
|
||||||
|
SetMemory(64 * 1024), // 64 bits
|
||||||
|
SetParallelism(4), // 4 concurrent actions
|
||||||
|
SetKeyLength(32), // key length
|
||||||
|
SetSaltLength(32), // salt length
|
||||||
|
SetIterations(4), // number of iterations
|
||||||
|
)
|
||||||
|
````
|
||||||
|
|
||||||
## Contributions
|
## Contributions
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ func (decoder *Decoder) decodeHash(encodedHash string) (d *Decoder, salt, hash [
|
||||||
if version != argon2.Version {
|
if version != argon2.Version {
|
||||||
return nil, nil, nil, ErrIncompatibleVersion
|
return nil, nil, nil, ErrIncompatibleVersion
|
||||||
}
|
}
|
||||||
_, err = fmt.Sscanf(values[3], "m=%d,t=%d,p=%d", &decoder.memory, &decoder.iterations, &decoder.parallelism)
|
_, err = fmt.Sscanf(values[3], "m=%d,t=%d,p=%d", &decoder.Memory, &decoder.Iterations, &decoder.Parallelism)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
@ -43,12 +43,12 @@ func (decoder *Decoder) decodeHash(encodedHash string) (d *Decoder, salt, hash [
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
d = decoder
|
d = decoder
|
||||||
d.saltLength = uint32(len(salt))
|
d.SaltLength = uint32(len(salt))
|
||||||
hash, err = base64.RawStdEncoding.DecodeString(values[5])
|
hash, err = base64.RawStdEncoding.DecodeString(values[5])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, nil, err
|
return nil, nil, nil, err
|
||||||
}
|
}
|
||||||
d.keyLength = uint32(len(hash))
|
d.KeyLength = uint32(len(hash))
|
||||||
|
|
||||||
return d, salt, hash, nil
|
return d, salt, hash, nil
|
||||||
}
|
}
|
||||||
|
@ -58,7 +58,7 @@ func (decoder *Decoder) CompareStringToHash(password string, hashedPassword stri
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
otherHash := argon2.IDKey([]byte(password), salt, p.iterations, p.memory, p.parallelism, p.keyLength)
|
otherHash := argon2.IDKey([]byte(password), salt, p.Iterations, p.Memory, p.Parallelism, p.KeyLength)
|
||||||
if subtle.ConstantTimeCompare(hash, otherHash) == 1 {
|
if subtle.ConstantTimeCompare(hash, otherHash) == 1 {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,26 +31,26 @@ func (encoder *Encoder) generateRandomBytes(n uint32) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *Encoder) HashString(password string) (encodedHash string, err error) {
|
func (encoder *Encoder) HashString(password string) (encodedHash string, err error) {
|
||||||
salt, err := encoder.generateRandomBytes(encoder.saltLength)
|
salt, err := encoder.generateRandomBytes(encoder.SaltLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
hash := argon2.IDKey(
|
hash := argon2.IDKey(
|
||||||
[]byte(password),
|
[]byte(password),
|
||||||
salt,
|
salt,
|
||||||
encoder.iterations,
|
encoder.Iterations,
|
||||||
encoder.memory,
|
encoder.Memory,
|
||||||
encoder.parallelism,
|
encoder.Parallelism,
|
||||||
encoder.keyLength,
|
encoder.KeyLength,
|
||||||
)
|
)
|
||||||
b64Salt := base64.RawStdEncoding.EncodeToString(salt)
|
b64Salt := base64.RawStdEncoding.EncodeToString(salt)
|
||||||
b64Hash := base64.RawStdEncoding.EncodeToString(hash)
|
b64Hash := base64.RawStdEncoding.EncodeToString(hash)
|
||||||
encodedHash = fmt.Sprintf(
|
encodedHash = fmt.Sprintf(
|
||||||
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||||
argon2.Version,
|
argon2.Version,
|
||||||
encoder.memory,
|
encoder.Memory,
|
||||||
encoder.iterations,
|
encoder.Iterations,
|
||||||
encoder.parallelism,
|
encoder.Parallelism,
|
||||||
b64Salt,
|
b64Salt,
|
||||||
b64Hash,
|
b64Hash,
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,22 +3,22 @@ package argon2
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
memory uint32
|
Memory uint32
|
||||||
iterations uint32
|
Iterations uint32
|
||||||
parallelism uint8
|
Parallelism uint8
|
||||||
saltLength uint32
|
SaltLength uint32
|
||||||
keyLength uint32
|
KeyLength uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrInvalidHash = errors.New("the encoded hash is not in the correct format")
|
ErrInvalidHash = errors.New("the encoded hash is not in the correct format")
|
||||||
ErrIncompatibleVersion = errors.New("incompatible version of argon2")
|
ErrIncompatibleVersion = errors.New("incompatible version of argon2")
|
||||||
defaultOptions = Options{
|
defaultOptions = Options{
|
||||||
memory: 64 * 1024,
|
Memory: 64 * 1024,
|
||||||
iterations: 3,
|
Iterations: 3,
|
||||||
parallelism: 2,
|
Parallelism: 2,
|
||||||
saltLength: 16,
|
SaltLength: 16,
|
||||||
keyLength: 32,
|
KeyLength: 32,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,30 +26,30 @@ type OptFunc func(*Options)
|
||||||
|
|
||||||
func SetMemory(memory uint32) OptFunc {
|
func SetMemory(memory uint32) OptFunc {
|
||||||
return func(options *Options) {
|
return func(options *Options) {
|
||||||
options.memory = memory
|
options.Memory = memory
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetIterations(iterations uint32) OptFunc {
|
func SetIterations(iterations uint32) OptFunc {
|
||||||
return func(options *Options) {
|
return func(options *Options) {
|
||||||
options.iterations = iterations
|
options.Iterations = iterations
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetParallelism(parallelism uint8) OptFunc {
|
func SetParallelism(parallelism uint8) OptFunc {
|
||||||
return func(options *Options) {
|
return func(options *Options) {
|
||||||
options.parallelism = parallelism
|
options.Parallelism = parallelism
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetSaltLength(saltLength uint32) OptFunc {
|
func SetSaltLength(saltLength uint32) OptFunc {
|
||||||
return func(options *Options) {
|
return func(options *Options) {
|
||||||
options.saltLength = saltLength
|
options.SaltLength = saltLength
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetKeyLength(keyLength uint32) OptFunc {
|
func SetKeyLength(keyLength uint32) OptFunc {
|
||||||
return func(options *Options) {
|
return func(options *Options) {
|
||||||
options.keyLength = keyLength
|
options.KeyLength = keyLength
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user