4 Commits

Author SHA1 Message Date
c0ecb85a53 feat(更新依赖): 更新依赖
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2022-02-06 21:22:17 +08:00
0eae46dfb5 Update README.md
All checks were successful
continuous-integration/drone/push Build is passing
2021-01-21 17:31:12 +08:00
87a2045616 docs(更新文档): 更新文档 2020-10-21 14:58:40 +08:00
4b6cac9d6f docs(更新文档): 添加注释 2020-10-21 14:54:59 +08:00
4 changed files with 20 additions and 11 deletions

View File

@ -1,4 +1,4 @@
# idkey
# idkey [![PkgGoDev](https://pkg.go.dev/badge/github.com/xiaoqidun/idkey)](https://pkg.go.dev/github.com/xiaoqidun/idkey)
Golang Argon2id 密码hash和验证
# 安装方法
go get -u github.com/xiaoqidun/idkey

6
go.mod
View File

@ -1,5 +1,7 @@
module github.com/xiaoqidun/idkey
go 1.15
go 1.17
require golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
require golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838
require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect

18
go.sum
View File

@ -1,8 +1,10 @@
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig=
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 h1:71vQrMauZZhcTVK6KdYM+rklehEEwb3E+ZhaE5jrPrE=
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

@ -10,12 +10,14 @@ import (
"strings"
)
// Data 密文编码结构
type Data struct {
EncodeOptions // 加密选项
Hash []byte // 密码密文
Salt []byte // 加密盐值
}
// EncodeOptions 密文编码选项
type EncodeOptions struct {
Time uint32 // 时间参数
Memory uint32 // 内存参数
@ -23,6 +25,7 @@ type EncodeOptions struct {
KeyLen uint32 // 密文长度
}
// Encode 将密码编码成密文
func Encode(password []byte, options *EncodeOptions) string {
salt := generateSalt(16)
data := &Data{
@ -58,6 +61,7 @@ func Encode(password []byte, options *EncodeOptions) string {
)
}
// Decode 获取密文编码结构
func Decode(passwordHash string) (data *Data, err error) {
data = &Data{}
params := strings.Split(passwordHash, "$")
@ -102,6 +106,7 @@ func Decode(passwordHash string) (data *Data, err error) {
return
}
// Verify 验证密码编码后是否等于密文
func Verify(password []byte, passwordHash string) bool {
data, err := Decode(passwordHash)
if err != nil {