zbcrypt

package
v0.0.0-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2025 License: Apache-2.0 Imports: 2 Imported by: 1

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

func New(cost int) (*BCrypt, error)

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:

Recommended parameters:

func (*BCrypt) Compare

func (c *BCrypt) Compare(hashedPW, pw []byte) error

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

func (c *BCrypt) Equal(hashedPW, pw []byte) bool

Equal reports if the given hashed password and the password are the same or not.

func (*BCrypt) Sum

func (c *BCrypt) Sum(password []byte) ([]byte, error)

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL