Browse Source

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

master
xiaoqidun 5 months ago
parent
commit
30d5e20b10
2 changed files with 20 additions and 10 deletions
  1. +1
    -1
      README.md
  2. +19
    -9
      idkey.go

+ 1
- 1
README.md View File

@ -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)
```

+ 19
- 9
idkey.go View File

@ -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…
Cancel
Save