docs(更新文档): 添加注释

This commit is contained in:
xiaoqidun 2020-10-21 14:54:59 +08:00
parent 010073a6c0
commit 4b6cac9d6f
2 changed files with 6 additions and 1 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和验证 Golang Argon2id 密码hash和验证
# 安装方法 # 安装方法
go get -u github.com/xiaoqidun/idkey go get -u github.com/xiaoqidun/idkey

View File

@ -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 {