chore: upgrade dependencies and secure passwords
This commit is contained in:
12
module/ec/mall/internal/password/password.go
Normal file
12
module/ec/mall/internal/password/password.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package password
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
func Hash(plain string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(plain), bcrypt.DefaultCost)
|
||||
return string(hash), err
|
||||
}
|
||||
|
||||
func Verify(hash, plain string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(plain)) == nil
|
||||
}
|
||||
13
module/ec/mall/internal/password/password_test.go
Normal file
13
module/ec/mall/internal/password/password_test.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package password
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHashAndVerify(t *testing.T) {
|
||||
hash, err := Hash("correct horse battery staple")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !Verify(hash, "correct horse battery staple") || Verify(hash, "wrong") {
|
||||
t.Fatal("bcrypt verification failed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user