feat(加密选项): 支持设置加密选项

This commit is contained in:
xiaoqidun 2020-04-26 16:28:57 +08:00
parent 5139364272
commit 30d5e20b10
2 changed files with 20 additions and 10 deletions

View File

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

View File

@ -11,23 +11,33 @@ import (
) )
type Data struct { type Data struct {
Hash []byte // 密码密文 EncodeOptions // 加密选项
Salt []byte // 加密盐值 Hash []byte // 密码密文
Salt []byte // 加密盐值
}
type EncodeOptions struct {
Time uint32 // 时间参数 Time uint32 // 时间参数
Memory uint32 // 内存参数 Memory uint32 // 内存参数
Threads uint8 // 线程参数 Threads uint8 // 线程参数
KeyLen uint32 // 密文长度 KeyLen uint32 // 密文长度
} }
func Encode(password []byte) string { func Encode(password []byte, options *EncodeOptions) string {
salt := generateSalt(16) salt := generateSalt(16)
data := &Data{ data := &Data{
Hash: nil, Hash: nil,
Salt: salt, Salt: salt,
Time: 1, }
Memory: 64 * 1024, if options != nil {
Threads: 4, data.EncodeOptions = *options
KeyLen: 32, } else {
data.EncodeOptions = EncodeOptions{
Time: 1,
Memory: 64 * 1024,
Threads: 4,
KeyLen: 32,
}
} }
hash := argon2.IDKey( hash := argon2.IDKey(
password, password,