bcrypt

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2023 License: MIT Imports: 5 Imported by: 2

README

PHC Crypto - Bcrypt

Go Reference

According to Wikipedia:

bcrypt is a password-hashing function designed by Niels Provos and David Mazières, based on the Blowfish cipher and presented at USENIX in 1999. Besides incorporating a salt to protect against rainbow table attacks, bcrypt is an adaptive function: over time, the iteration count can be increased to make it slower, so it remains resistant to brute-force search attacks even with increasing computation power.

Configuration options

Key Type Default Notes
Rounds int 10 Cost of rounds, minimum of 4, maximum of 31.

Usage with PHC Crypto

package main

import (
	"fmt"
	"github.com/aldy505/phc-crypto"
)

func main() {
	crypto, err := phccrypto.Use(phccrypto.Bcrypt, phccrypto.Config{
		Rounds: 20,
	})
	if err != nil {
		fmt.Println(err)
	}

	hash, err := phccrypto.Hash("password")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // $bcrypt$v=0$r=12$$2432612431322479356256373563666e503557...

	verify, err := phccrypto.Verify(hash, "password")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(verify) // true
}

Standalone usage

package main

import (
	"fmt"
	"github.com/aldy505/phc-crypto/bcrypt"
)

func main() {
	hash, err := bcrypt.Hash("password", bcrypt.Config{
		Rounds: 12,
	})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // $bcrypt$v=0$r=12$$2432612431322479356256373563666e503557...

	verify, err := bcrypt.Verify(hash, "password")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(verify) // true
}

Documentation

Index

Constants

View Source
const (
	// ROUNDS is the cost of rounds, minimum of 4, maximum of 31.
	ROUNDS = 10
)

Variables

View Source
var ErrEmptyField error = errors.New("function parameters must not be empty")

Functions

func Hash

func Hash(plain string, config Config) (string, error)

Hash creates a PHC-formatted hash with config provided

import (
  "fmt"
  "github.com/aldy505/phc-crypto/bcrypt"
)

func main() {
  hash, err := bcrypt.Hash("password", bcrypt.Config{
    Rounds: 12,
  })
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(hash) // $bcrypt$v=0$r=12$$2432612431322479356256373563666e503557...
}

func Verify

func Verify(hash string, plain string) (bool, error)

Verify checks the hash if it's equal (by an algorithm) to plain text provided.

import (
  "fmt"
  "github.com/aldy505/phc-crypto/bcrypt"
)

func main() {
  hash := "$bcrypt$v=0$r=12$$2432612431322479356256373563666e503557..."

  verify, err := bcrypt.Verify(hash, "password")
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(verify) // true
}

Types

type Config

type Config struct {
	Rounds int
}

Config initialize the config require to create a hash function

Jump to

Keyboard shortcuts

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