bcrypt_wrapper

package module
v0.0.0-...-c963743 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2019 License: MIT Imports: 2 Imported by: 0

README

MIT license codecov Go Report Card GoDoc

BCrypt-Wrapper

BCrypt-Wrapper is a small wrapper around Go's standard BCrypt-Implementation with the goal of increasing the cost-factor when its needed. To do this, everytime you use this wrapper when you verify its password, there is a check if the used cost-factor is not already to low. When the cost-factor is to low, the password will be hashed again with the focused cost-factor and returned.

Example Usage

wrapper := NewBCryptWrapper(14) // Create a new BCrypt-Wrapper with cost factor 14
someOutdatedHash := "$2y$11$KPR/tNhxP0RQcO7gSNjgJuXwu1jrwkfuEt2resN98faTtNnzq0DMa" // Hashed with cost-factor 14
someMatchingPassword := "E&dWBjxaE*8V"
newpass, err := wrapper.CompareHashAndPassword([]byte(someOutdatedHash), []byte(someMatchingPassword)) // Would return no error but the password hashed with cost-factor 14

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NilHash = []byte("")
	NilCost = 0
)

Functions

func GetSuitableCost

func GetSuitableCost(maxtime int64) int

Run a benchmark to get a suitable Cost for your application. The benchmark will take a few seconds. It will return the last Cost-value which is within your maxtime.

Types

type BCryptWrapper

type BCryptWrapper struct {
	Cost int
}

func NewBCryptWrapper

func NewBCryptWrapper(cost int) *BCryptWrapper

Returns a new BCrypt-Wrapper. Set Cost "NilCost" for running a benchmark and using the benchmark-outcome as Cost. The benchmark will then use 250ms as default value (meaning max amount of 4 passwords per second per thread).

func (*BCryptWrapper) CompareHashAndPassword

func (wrapper *BCryptWrapper) CompareHashAndPassword(hashedPassword, password []byte) ([]byte, error)

Compares a password to a hashed password. Returns an bcrypt.ErrMismatchedHashAndPassword when password does not match the hash. Returns a newly hashed password when the Cost from the previous one used was lower then specified in this wrapper.

func (*BCryptWrapper) GenerateFromPassword

func (wrapper *BCryptWrapper) GenerateFromPassword(password []byte) ([]byte, error)

Generates a hash value from a password. Uses the wrappers-Cost as Cost-value for bcrypt.

Jump to

Keyboard shortcuts

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