From 30d5e20b10fc5e58d884c9f4c9c6e07b506860a3 Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Sun, 26 Apr 2020 16:28:57 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=8A=A0=E5=AF=86=E9=80=89=E9=A1=B9):=20?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=8A=A0=E5=AF=86=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- idkey.go | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index bdd5830..7a4233f 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/idkey.go b/idkey.go index 1c980ce..ea0d38c 100644 --- a/idkey.go +++ b/idkey.go @@ -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,