zscrypt

package
v0.0.0-alpha.15 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrNotMatch  = errors.New("zscrypt: password hash value not match")
	ErrShortHash = errors.New("zscrypt: password hash is too short")
)

Functions

This section is empty.

Types

type SCrypt

type SCrypt struct {
	// contains filtered or unexported fields
}

SCrypt is the hasher type for golang.org/x/crypto/scrypt.

func New

func New(saltLen, n, r, p, keyLen int) (*SCrypt, error)

New returns a new instance of SCrypt hasher with given parameters. The parameters n, r, p and keyLen are passed to the golang.org/x/crypto/scrypt.Key.

Parameter ranges:

Recommended parameters:

func (*SCrypt) Compare

func (c *SCrypt) 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"
	"encoding/hex"
	"fmt"
	"strings"

	"github.com/aileron-projects/go/zcrypto/zscrypt"
)

func main() {
	// Replace rand reader temporarily for testing.
	tmp := rand.Reader
	rand.Reader = strings.NewReader("12345678901234567890")
	defer func() { rand.Reader = tmp }()

	hashedPW, _ := hex.DecodeString("313233343536373839302c717880aa40e2d11cc48c684730cd6f6d87fd2bc7f2aea2ec1246d7019461d2")

	crypt, err := zscrypt.New(10, 32768, 8, 1, 32)
	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 (*SCrypt) Equal

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

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

func (*SCrypt) Split

func (c *SCrypt) Split(hashedPW []byte) (salt, hash []byte, err error)

Split splits the hashed password into salt and hash value of the password. If the hashedPW is too short, it returns ErrShortHash.

func (*SCrypt) Sum

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

Sum returns the hash sum value of the password. Salt is joined at the left side of the returned sum. Use SCrypt.Split to split salt and the sum of the password.

Example
package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"strings"

	"github.com/aileron-projects/go/zcrypto/zscrypt"
)

func main() {
	// Replace rand reader temporarily for testing.
	tmp := rand.Reader
	rand.Reader = strings.NewReader("12345678901234567890")
	defer func() { rand.Reader = tmp }()

	saltLen := 10
	crypt, err := zscrypt.New(saltLen, 32768, 8, 1, 32)
	if err != nil {
		panic(err)
	}
	hashedPW, err := crypt.Sum([]byte("password"))
	if err != nil {
		panic(err)
	}
	fmt.Println("salt    :", hex.EncodeToString(hashedPW[:saltLen]))
	fmt.Println("pw hash :", hex.EncodeToString(hashedPW[saltLen:]))
	fmt.Println("overall :", hex.EncodeToString(hashedPW))
}
Output:

salt    : 31323334353637383930
pw hash : 2c717880aa40e2d11cc48c684730cd6f6d87fd2bc7f2aea2ec1246d7019461d2
overall : 313233343536373839302c717880aa40e2d11cc48c684730cd6f6d87fd2bc7f2aea2ec1246d7019461d2

Jump to

Keyboard shortcuts

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