From ce39432a07ee74723b611fc3060357f0782cd783 Mon Sep 17 00:00:00 2001 From: Quentin Dufour Date: Tue, 19 Jul 2022 17:18:14 +0200 Subject: Try to fix SSHA conversion --- ssha.go | 14 +++++++++++++- ssha_test.go | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 ssha_test.go diff --git a/ssha.go b/ssha.go index 202ce12..cca51c7 100644 --- a/ssha.go +++ b/ssha.go @@ -2,6 +2,8 @@ package main import ( "errors" + "regexp" + "strings" "github.com/jsimonetti/pwscheme/ssha" "github.com/jsimonetti/pwscheme/ssha256" @@ -14,6 +16,8 @@ const ( SSHA512 = "{SSHA512}" ) +var scheme = regexp.MustCompile(`^{([0-9a-zA-Z]+)}`) + // Encode encodes the string to ssha512 func SSHAEncode(rawPassPhrase string) (string, error) { return ssha512.Generate(rawPassPhrase, 16) @@ -21,7 +25,9 @@ func SSHAEncode(rawPassPhrase string) (string, error) { // Matches matches the encoded password and the raw password func SSHAMatches(encodedPassPhrase string, rawPassPhrase string) (bool, error) { - hashType, err := determineHashType(encodedPassPhrase) + cleanEncodedPass := uppercaseScheme(encodedPassPhrase) + + hashType, err := determineHashType(cleanEncodedPass) if err != nil { return false, errors.New("invalid password hash stored") } @@ -51,3 +57,9 @@ func determineHashType(hash string) (string, error) { return "", errors.New("no valid hash found") } + +func uppercaseScheme(hash string) string { + return scheme.ReplaceAllStringFunc(hash, func(w string) string { + return strings.ToUpper(w) + }) +} diff --git a/ssha_test.go b/ssha_test.go new file mode 100644 index 0000000..7620436 --- /dev/null +++ b/ssha_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "testing" +) + +func TestUpper(t *testing.T) { + r1 := uppercaseScheme("{ssha}ss4654654") + if r1 != "{SSHA}ss4654654" { + t.Fatal(r1) + } + + r2 := uppercaseScheme("{ssha512}fdbgf6546") + if r2 != "{SSHA512}fdbgf6546" { + t.Fatal(r2) + } +} -- cgit v1.2.3