mirror of
				https://github.com/xiaoqidun/idkey.git
				synced 2025-11-04 09:29:36 +08:00 
			
		
		
		
	docs(更新文档): 添加注释
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
				
			|||||||
# idkey
 | 
					# 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
									
								
								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 {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user