Documentation ¶
Overview ¶
Package sha256 implements the SHA-256 hashing algorithm for crypt(3).
Index ¶
- Constants
- func Check(hash, password string) error
- func Key(password, salt []byte, rounds uint32) ([]byte, error)
- func NewHash(password string, rounds uint32) (string, error)
- func Params(hash string) (salt []byte, rounds uint32, err error)
- type InvalidRoundsError
- type InvalidSaltError
- type InvalidSaltLengthError
- type UnsupportedPrefixError
Examples ¶
Constants ¶
const ( MaxSaltLength = 16 DefaultSaltLength = MaxSaltLength )
const ( MinRounds = 1000 MaxRounds = 999999999 DefaultRounds = 535000 ImplicitRounds = 5000 // the value if the rounds parameter is omitted from the hash string )
const Prefix = "$5$"
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check compares the given crypt(3) SHA-256 hash with a new hash derived from the password. Returns nil on success, or an error on failure.
Example ¶
package main import ( "fmt" "github.com/sergeymakinen/go-crypt/sha256" ) func main() { hash := "$5$rounds=505000$.HnFpd3anFzRwVj5$EdcK/Q9wfmq1XsG5OTKP0Ns.ZlN9DRHslblcgCLtXY5" fmt.Println(sha256.Check(hash, "password")) fmt.Println(sha256.Check(hash, "test")) }
Output: <nil> hash and password mismatch
func Key ¶
Key returns a SHA-256 key derived from the password, salt and rounds.
Example ¶
package main import ( "fmt" "github.com/sergeymakinen/go-crypt/hash" "github.com/sergeymakinen/go-crypt/sha256" ) func main() { salt, rounds, _ := sha256.Params("$5$rounds=505000$.HnFpd3anFzRwVj5$EdcK/Q9wfmq1XsG5OTKP0Ns.ZlN9DRHslblcgCLtXY5") fmt.Println(string(salt)) fmt.Println(rounds) key, _ := sha256.Key([]byte("password"), salt, rounds) fmt.Println(hash.LittleEndianEncoding.EncodeToString(key)) }
Output: .HnFpd3anFzRwVj5 505000 EdcK/Q9wfmq1XsG5OTKP0Ns.ZlN9DRHslblcgCLtXY5
func Params ¶
Params returns the hashing salt and rounds used to create the given crypt(3) SHA-256 hash.
Example ¶
package main import ( "fmt" "github.com/sergeymakinen/go-crypt/sha256" ) func main() { salt, rounds, _ := sha256.Params("$5$rounds=505000$.HnFpd3anFzRwVj5$EdcK/Q9wfmq1XsG5OTKP0Ns.ZlN9DRHslblcgCLtXY5") fmt.Println(string(salt)) fmt.Println(rounds) }
Output: .HnFpd3anFzRwVj5 505000
Types ¶
type InvalidRoundsError ¶
type InvalidRoundsError uint32
InvalidRoundsError values describe errors resulting from an invalid round count.
func (InvalidRoundsError) Error ¶
func (e InvalidRoundsError) Error() string
type InvalidSaltError ¶
type InvalidSaltError byte
InvalidSaltError values describe errors resulting from an invalid character in a hash string.
func (InvalidSaltError) Error ¶
func (e InvalidSaltError) Error() string
type InvalidSaltLengthError ¶
type InvalidSaltLengthError int
InvalidSaltLengthError values describe errors resulting from an invalid length of a salt.
func (InvalidSaltLengthError) Error ¶
func (e InvalidSaltLengthError) Error() string
type UnsupportedPrefixError ¶
type UnsupportedPrefixError string
UnsupportedPrefixError values describe errors resulting from an unsupported prefix string.
func (UnsupportedPrefixError) Error ¶
func (e UnsupportedPrefixError) Error() string