登录功能完成
This commit is contained in:
16
pkg/password/argon2id.go
Normal file
16
pkg/password/argon2id.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package password
|
||||
|
||||
import "github.com/alexedwards/argon2id"
|
||||
|
||||
type Argon2id struct {
|
||||
}
|
||||
|
||||
func (a *Argon2id) Create(password string) (hash string, err error) {
|
||||
hash, err = argon2id.CreateHash(password, argon2id.DefaultParams)
|
||||
return
|
||||
}
|
||||
|
||||
func (a *Argon2id) Verify(password, hash string) (match bool, err error) {
|
||||
match, err = argon2id.ComparePasswordAndHash(password, hash)
|
||||
return
|
||||
}
|
||||
29
pkg/password/password.go
Normal file
29
pkg/password/password.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"mc-client-updater-server/pkg/conf"
|
||||
"mc-client-updater-server/pkg/log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
once sync.Once
|
||||
encoder Encoder
|
||||
)
|
||||
|
||||
func Password() Encoder {
|
||||
once.Do(func() {
|
||||
switch conf.Conf.Security.PasswordEncoder {
|
||||
case "argon2id":
|
||||
encoder = &Argon2id{}
|
||||
default:
|
||||
log.Logger.Fatal("非法的PasswordEncoder类型:", conf.Conf.Security.PasswordEncoder)
|
||||
}
|
||||
})
|
||||
return encoder
|
||||
}
|
||||
|
||||
type Encoder interface {
|
||||
Create(string) (string, error)
|
||||
Verify(password, hash string) (bool, error)
|
||||
}
|
||||
Reference in New Issue
Block a user