pbkdf2

package
Version: v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 13 Imported by: 0

README

PHC Crypto - Scrypt

Go Reference

According to Wikipedia:

In cryptography, PBKDF1 and PBKDF2 (Password-Based Key Derivation Function 1 and 2) are key derivation functions with a sliding computational cost, used to reduce vulnerabilities of brute-force attacks. PBKDF2 is part of RSA Laboratories' Public-Key Cryptography Standards (PKCS) series, specifically PKCS #5 v2.0, also published as Internet Engineering Task Force's RFC 2898. It supersedes PBKDF1, which could only produce derived keys up to 160 bits long. RFC 8018 (PKCS #5 v2.1), published in 2017, recommends PBKDF2 for password hashing.

Configuration options

Key Type Default Notes
Rounds int 4096 Iteration counts.
HashFunc string sha256 For calculating HMAC
KeyLen int 32 How many bytes to generate as output.
SaltLen int 16 Salt length in bytes

Usage with PHC Crypto

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

func main() {
  crypto, err := phccrypto.Use(phccrypto.PBKDF2, phccrypto.Config{
    HashFunc: "sha512",
  })
  if err != nil {
    fmt.Println(err)
  }

  hash, err := phccrypto.Hash("password")
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(hash) // $pbkdf2sha512$v=0$i=4096$87a39b3cf30626bc7cf6534ac3a14ddf$d32093416bf521ff0...

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

Standalone usage

$ go get github.com/aldy505/phc-crypto/pbkdf2
import (
  "fmt"
  "github.com/aldy505/phc-crypto/pbkdf2"
)

func main() {

  hash, err := pbkdf2.Hash("password", pbkdf2.Config{
    HashFunc: "sha512",
  })
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(hash) // $pbkdf2sha512$v=0$i=4096$87a39b3cf30626bc7cf6534ac3a14ddf$d32093416bf521ff0...

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

Documentation

Index

Constants

View Source
const (
	// ROUNDS is the iteration counts.
	ROUNDS = 4096
	// KEYLEN is how many bytes to generate as output.
	KEY_LENGTH = 32
	// DEFAULT_HASHFUNCTION is for calculating HMAC. Defaulting to sha256.
	DEFAULT_HASHFUNCTION = "sha256"
	// SALT_LENGTH is the default salth length in bytes.
	SALT_LENGTH = 16
)

Variables

This section is empty.

Functions

func Hash

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

Hash creates a PHC-formatted hash with config provided

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.

Types

type Config

type Config struct {
	Rounds   int
	KeyLen   int
	HashFunc string
	SaltLen  int
}

Config initialize the config require to create a hash function

Source Files

Jump to

Keyboard shortcuts

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