argon2/decoder.go

62 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-11-28 22:01:52 +01:00
package argon2
2024-07-24 19:42:25 +02:00
import (
"crypto/subtle"
"encoding/base64"
"fmt"
"strings"
"golang.org/x/crypto/argon2"
)
type Decoder struct{}
func NewDecoder() *Decoder {
return new(Decoder)
}
2024-11-28 22:01:52 +01:00
func (decoder *Decoder) decodeHash(encodedHash string) (o *Options, salt, hash []byte, err error) {
2024-07-24 19:42:25 +02:00
values := strings.Split(encodedHash, "$")
if len(values) != 6 {
2024-11-28 22:01:52 +01:00
return nil, nil, nil, ErrInvalidHash
2024-07-24 19:42:25 +02:00
}
var version int
_, err = fmt.Sscanf(values[2], "v=%d", &version)
if err != nil {
return nil, nil, nil, err
}
if version != argon2.Version {
2024-11-28 22:01:52 +01:00
return nil, nil, nil, ErrIncompatibleVersion
2024-07-24 19:42:25 +02:00
}
2024-11-28 22:01:52 +01:00
o = new(Options)
2024-07-24 19:42:25 +02:00
_, err = fmt.Sscanf(values[3], "m=%d,t=%d,p=%d", &o.Memory, &o.Iterations, &o.Parallelism)
if err != nil {
return nil, nil, nil, err
}
salt, err = base64.RawStdEncoding.DecodeString(values[4])
if err != nil {
return nil, nil, nil, err
}
o.SaltLength = uint32(len(salt))
hash, err = base64.RawStdEncoding.DecodeString(values[5])
if err != nil {
return nil, nil, nil, err
}
o.KeyLength = uint32(len(hash))
return o, salt, hash, nil
}
func (decoder *Decoder) CompareStringToHash(password string, hashedPassword string) (match bool, err error) {
p, salt, hash, err := decoder.decodeHash(hashedPassword)
if err != nil {
return false, err
}
otherHash := argon2.IDKey([]byte(password), salt, p.Iterations, p.Memory, p.Parallelism, p.KeyLength)
if subtle.ConstantTimeCompare(hash, otherHash) == 1 {
return true, nil
}
return false, nil
}