feat(encryption): add MultiPasswordVerify function for multiple hashing algorithms
This commit is contained in:
parent
a273f8d883
commit
de4386ff68
@ -2,12 +2,14 @@ package tool
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/anaskhan96/go-password-encoder"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
var options = &password.Options{SaltLen: 16, Iterations: 100, KeyLen: 32, HashFunction: sha512.New}
|
||||
@ -32,3 +34,26 @@ func Md5Encode(str string, isUpper bool) string {
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func MultiPasswordVerify(algo, salt, password, hash string) bool {
|
||||
switch algo {
|
||||
case "md5":
|
||||
sum := md5.Sum([]byte(password))
|
||||
return hex.EncodeToString(sum[:]) == hash
|
||||
|
||||
case "sha256":
|
||||
sum := sha256.Sum256([]byte(password))
|
||||
return hex.EncodeToString(sum[:]) == hash
|
||||
|
||||
case "md5salt":
|
||||
sum := md5.Sum([]byte(password + salt))
|
||||
return hex.EncodeToString(sum[:]) == hash
|
||||
case "default":
|
||||
return VerifyPassWord(password, hash)
|
||||
case "bcrypt":
|
||||
// Bcrypt (corresponding to PHP's password_hash/password_verify)
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
||||
return err == nil
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ -1,7 +1,15 @@
|
||||
package tool
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncodePassWord(t *testing.T) {
|
||||
t.Logf("EncodePassWord: %v", EncodePassWord(""))
|
||||
}
|
||||
|
||||
func TestMultiPasswordVerify(t *testing.T) {
|
||||
pwd := "$2y$10$WFO17pdtohfeBILjEChoGeVxpDG.u9kVCKhjDAeEeNmCjIlj3tDRy"
|
||||
status := MultiPasswordVerify("", "", "admin", pwd)
|
||||
t.Logf("MultiPasswordVerify: %v", status)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user