Skip to content

Instantly share code, notes, and snippets.

@eamonnmcevoy
Created March 10, 2018 23:03
Show Gist options
  • Save eamonnmcevoy/c7ab5a5253712561f8dd923936646b96 to your computer and use it in GitHub Desktop.
Save eamonnmcevoy/c7ab5a5253712561f8dd923936646b96 to your computer and use it in GitHub Desktop.
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