mirror of
https://github.com/xiaoqidun/idkey.git
synced 2024-05-07 00:14:15 +08:00
Compare commits
2 Commits
010073a6c0
...
87a2045616
Author | SHA1 | Date | |
---|---|---|---|
87a2045616 | |||
4b6cac9d6f |
|
@ -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和验证
|
Golang Argon2id 密码hash和验证
|
||||||
# 安装方法
|
# 安装方法
|
||||||
go get -u github.com/xiaoqidun/idkey
|
go get -u github.com/xiaoqidun/idkey
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -2,4 +2,7 @@ module github.com/xiaoqidun/idkey
|
||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de
|
require (
|
||||||
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
|
||||||
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a // indirect
|
||||||
|
)
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,8 +1,12 @@
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
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 h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig=
|
||||||
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
|
||||||
|
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
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-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 h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a h1:e3IU37lwO4aq3uoRKINC7JikojFmE5gO7xhfxs8VC34=
|
||||||
|
golang.org/x/sys v0.0.0-20201020230747-6e5568b54d1a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
|
5
idkey.go
5
idkey.go
|
@ -10,12 +10,14 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Data 密文编码结构
|
||||||
type Data struct {
|
type Data struct {
|
||||||
EncodeOptions // 加密选项
|
EncodeOptions // 加密选项
|
||||||
Hash []byte // 密码密文
|
Hash []byte // 密码密文
|
||||||
Salt []byte // 加密盐值
|
Salt []byte // 加密盐值
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EncodeOptions 密文编码选项
|
||||||
type EncodeOptions struct {
|
type EncodeOptions struct {
|
||||||
Time uint32 // 时间参数
|
Time uint32 // 时间参数
|
||||||
Memory uint32 // 内存参数
|
Memory uint32 // 内存参数
|
||||||
|
@ -23,6 +25,7 @@ type EncodeOptions struct {
|
||||||
KeyLen uint32 // 密文长度
|
KeyLen uint32 // 密文长度
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode 将密码编码成密文
|
||||||
func Encode(password []byte, options *EncodeOptions) string {
|
func Encode(password []byte, options *EncodeOptions) string {
|
||||||
salt := generateSalt(16)
|
salt := generateSalt(16)
|
||||||
data := &Data{
|
data := &Data{
|
||||||
|
@ -58,6 +61,7 @@ func Encode(password []byte, options *EncodeOptions) string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decode 获取密文编码结构
|
||||||
func Decode(passwordHash string) (data *Data, err error) {
|
func Decode(passwordHash string) (data *Data, err error) {
|
||||||
data = &Data{}
|
data = &Data{}
|
||||||
params := strings.Split(passwordHash, "$")
|
params := strings.Split(passwordHash, "$")
|
||||||
|
@ -102,6 +106,7 @@ func Decode(passwordHash string) (data *Data, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify 验证密码编码后是否等于密文
|
||||||
func Verify(password []byte, passwordHash string) bool {
|
func Verify(password []byte, passwordHash string) bool {
|
||||||
data, err := Decode(passwordHash)
|
data, err := Decode(passwordHash)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user