-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from hexis-revival/reconnect-packet
Implement reconnect packet & response password hashing
- Loading branch information
Showing
6 changed files
with
263 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package common | ||
|
||
import ( | ||
"crypto/sha512" | ||
"encoding/hex" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
var passwordCache = map[string]bool{} | ||
|
||
func GetPasswordCache() map[string]bool { | ||
return passwordCache | ||
} | ||
|
||
func ClearPasswordCache() { | ||
passwordCache = map[string]bool{} | ||
} | ||
|
||
func CreatePasswordHash(password string) (string, error) { | ||
hashedPassword := GetSHA512Hash(password) | ||
hashedBytes, err := bcrypt.GenerateFromPassword( | ||
hashedPassword, | ||
bcrypt.DefaultCost, | ||
) | ||
|
||
return string(hashedBytes), err | ||
} | ||
|
||
func CheckPassword(input string, bcryptString string) bool { | ||
inputHashed := GetSHA512Hash(input) | ||
|
||
if isCorrect, ok := passwordCache[string(inputHashed)]; ok { | ||
return isCorrect | ||
} | ||
|
||
err := bcrypt.CompareHashAndPassword( | ||
[]byte(bcryptString), | ||
inputHashed, | ||
) | ||
|
||
isCorrect := err == nil | ||
passwordCache[string(inputHashed)] = isCorrect | ||
return isCorrect | ||
} | ||
|
||
func CheckPasswordHashed(inputHashed []byte, bcryptString string) bool { | ||
if len(inputHashed) != sha512.Size { | ||
return false | ||
} | ||
|
||
if isCorrect, ok := passwordCache[string(inputHashed)]; ok { | ||
return isCorrect | ||
} | ||
|
||
err := bcrypt.CompareHashAndPassword( | ||
[]byte(bcryptString), | ||
inputHashed, | ||
) | ||
|
||
isCorrect := err == nil | ||
passwordCache[string(inputHashed)] = isCorrect | ||
return isCorrect | ||
} | ||
|
||
func CheckPasswordHashedHex(inputHex string, bcryptString string) bool { | ||
inputHashed, err := hex.DecodeString(inputHex) | ||
|
||
if err != nil { | ||
return false | ||
} | ||
|
||
return CheckPasswordHashed(inputHashed, bcryptString) | ||
} | ||
|
||
func GetSHA512Hash(input string) []byte { | ||
hash := sha512.New() | ||
hash.Write([]byte(input)) | ||
hashedBytes := hash.Sum(nil) | ||
return hashedBytes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPasswords(t *testing.T) { | ||
password := "password" | ||
hash, err := CreatePasswordHash(password) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
if !CheckPassword(password, hash) { | ||
t.Error("password check failed") | ||
return | ||
} | ||
|
||
passwordHashed := GetSHA512Hash(password) | ||
|
||
if !CheckPasswordHashed(passwordHashed, hash) { | ||
t.Error("hashed password check failed") | ||
return | ||
} | ||
|
||
if len(passwordCache) != 1 { | ||
t.Error("password cache should have 1 entry") | ||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.