密码从string改为[]byte类型

This commit is contained in:
xiaoqidun 2020-01-07 10:31:00 +08:00
parent 5ebdc04340
commit 5dcac0a3fb
2 changed files with 6 additions and 6 deletions

@ -5,7 +5,7 @@ go get -u github.com/xiaoqidun/idkey
# 使用方法 # 使用方法
```go ```go
// 生成argon2id hash密码 // 生成argon2id hash密码
hash := idkey.Encode("admin") hash := idkey.Encode([]byte("admin"))
// 进行argon2id hash验证 // 进行argon2id hash验证
verify := idkey.Verify("admin", hash) verify := idkey.Verify([]byte("admin"), hash)
``` ```

@ -19,7 +19,7 @@ type Data struct {
KeyLen uint32 // 密文长度 KeyLen uint32 // 密文长度
} }
func Encode(password string) string { func Encode(password []byte) string {
salt := generateSalt(16) salt := generateSalt(16)
data := &Data{ data := &Data{
Hash: nil, Hash: nil,
@ -30,7 +30,7 @@ func Encode(password string) string {
KeyLen: 32, KeyLen: 32,
} }
hash := argon2.IDKey( hash := argon2.IDKey(
[]byte(password), password,
data.Salt, data.Salt,
data.Time, data.Time,
data.Memory, data.Memory,
@ -92,13 +92,13 @@ func Decode(passwordHash string) (data *Data, err error) {
return return
} }
func Verify(password, passwordHash string) bool { func Verify(password []byte, passwordHash string) bool {
data, err := Decode(passwordHash) data, err := Decode(passwordHash)
if err != nil { if err != nil {
return false return false
} }
hash := argon2.IDKey( hash := argon2.IDKey(
[]byte(password), password,
data.Salt, data.Salt,
data.Time, data.Time,
data.Memory, data.Memory,