Created
March 10, 2018 23:03
-
-
Save eamonnmcevoy/c7ab5a5253712561f8dd923936646b96 to your computer and use it in GitHub Desktop.
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
package crypto | |
import ( | |
"errors" | |
"strings" | |
"golang.org/x/crypto/bcrypt" | |
) | |
//Hash implements root.Hash | |
type Hash struct{} | |
//Generate a salted hash for the input string | |
func (c *Hash) Generate(s string) (string, error) { | |
saltedBytes := []byte(s) | |
hashedBytes, err := bcrypt.GenerateFromPassword(saltedBytes, bcrypt.DefaultCost) | |
if err != nil { | |
return "", err | |
} | |
hash := string(hashedBytes[:]) | |
return hash, nil | |
} | |
//Compare string to generated hash | |
func (c *Hash) Compare(hash string, s string) error { | |
incoming := []byte(s) | |
existing := []byte(hash) | |
return bcrypt.CompareHashAndPassword(existing, incoming) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment