mirror of
https://github.com/xiaoqidun/idkey.git
synced 2024-11-21 23:56:45 +08:00
feat(加密选项): 支持设置加密选项
This commit is contained in:
parent
5139364272
commit
30d5e20b10
@ -5,7 +5,7 @@ go get -u github.com/xiaoqidun/idkey
|
||||
# 使用方法
|
||||
```go
|
||||
// 生成argon2id hash密码
|
||||
hash := idkey.Encode([]byte("admin"))
|
||||
hash := idkey.Encode([]byte("admin"), nil)
|
||||
// 进行argon2id hash验证
|
||||
verify := idkey.Verify([]byte("admin"), hash)
|
||||
```
|
||||
|
28
idkey.go
28
idkey.go
@ -11,23 +11,33 @@ import (
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
Hash []byte // 密码密文
|
||||
Salt []byte // 加密盐值
|
||||
EncodeOptions // 加密选项
|
||||
Hash []byte // 密码密文
|
||||
Salt []byte // 加密盐值
|
||||
}
|
||||
|
||||
type EncodeOptions struct {
|
||||
Time uint32 // 时间参数
|
||||
Memory uint32 // 内存参数
|
||||
Threads uint8 // 线程参数
|
||||
KeyLen uint32 // 密文长度
|
||||
}
|
||||
|
||||
func Encode(password []byte) string {
|
||||
func Encode(password []byte, options *EncodeOptions) string {
|
||||
salt := generateSalt(16)
|
||||
data := &Data{
|
||||
Hash: nil,
|
||||
Salt: salt,
|
||||
Time: 1,
|
||||
Memory: 64 * 1024,
|
||||
Threads: 4,
|
||||
KeyLen: 32,
|
||||
Hash: nil,
|
||||
Salt: salt,
|
||||
}
|
||||
if options != nil {
|
||||
data.EncodeOptions = *options
|
||||
} else {
|
||||
data.EncodeOptions = EncodeOptions{
|
||||
Time: 1,
|
||||
Memory: 64 * 1024,
|
||||
Threads: 4,
|
||||
KeyLen: 32,
|
||||
}
|
||||
}
|
||||
hash := argon2.IDKey(
|
||||
password,
|
||||
|
Loading…
x
Reference in New Issue
Block a user