Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BCrypt ¶
type BCrypt struct {
// contains filtered or unexported fields
}
BCrypt is the hasher type for golang.org/x/crypto/bcrypt.
func New ¶
New returns a new instance of BCrypt hasher with given parameters. If the cost is less than [bcrypt.MinCost]=4, then the [bcrypt.DefaultCost]=10 is used. If the cost is larger than [bcrypt.MaxCost]=31, an error is returned.
Parameter ranges:
- 4 <= cost <= 31. It is bcrypt.MinCost <= cost <= bcrypt.MaxCost.
Recommended parameters:
func (*BCrypt) Compare ¶
Compare compares hashed password and the password. If the hashed value of the pw matched to the hashedPW, it returns nil. If not matched, it returns non-nil error.
Example ¶
package main
import (
"crypto/rand"
"fmt"
"strings"
"github.com/aileron-projects/go/zcrypto/zbcrypt"
)
func main() {
// Replace rand reader temporarily for testing.
tmp := rand.Reader
rand.Reader = strings.NewReader("1234567890123456789012345678901234567890")
defer func() { rand.Reader = tmp }()
hashedPW := []byte("$2a$10$Lxe3KBCwKxOzLha2MR.vKeGtY6qvS27BZYaR11ITbrDDr2OYCgirC")
crypt, err := zbcrypt.New(10)
if err != nil {
panic(err)
}
err = crypt.Compare(hashedPW, []byte("password"))
if err != nil {
panic(err)
}
fmt.Println("Is password correct? :", err == nil)
}
Output: Is password correct? : true
func (*BCrypt) Equal ¶
Equal reports if the given hashed password and the password are the same or not.
func (*BCrypt) Sum ¶
Sum returns the hash sum value of the password.
Example ¶
package main
import (
"crypto/rand"
"fmt"
"strings"
"github.com/aileron-projects/go/zcrypto/zbcrypt"
)
func main() {
// Replace rand reader temporarily for testing.
tmp := rand.Reader
rand.Reader = strings.NewReader("1234567890123456789012345678901234567890")
defer func() { rand.Reader = tmp }()
crypt, err := zbcrypt.New(10)
if err != nil {
panic(err)
}
hashedPW, err := crypt.Sum([]byte("password"))
if err != nil {
panic(err)
}
fmt.Println(string(hashedPW))
}
Output: $2a$10$Lxe3KBCwKxOzLha2MR.vKeGtY6qvS27BZYaR11ITbrDDr2OYCgirC
Click to show internal directories.
Click to hide internal directories.