pbkdf2

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: 13 Imported by: 2

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 HashFunction pbkdf2.SHA256 For calculating HMAC. Available options: pbkdf2.SHA1, pbkdf2.SHA256, pbkdf2.SHA224, pbkdf2.SHA512, pbkdf2.SHA384, pbkdf2.MD5
KeyLen int 32 How many bytes to generate as output.
SaltLen int 16 Salt length in bytes

Usage with PHC Crypto

package main

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

func main() {
	crypto, err := phccrypto.Use(phccrypto.PBKDF2, phccrypto.Config{})
	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

package main

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

func main() {
	hash, err := pbkdf2.Hash("password", pbkdf2.Config{
		HashFunc: pbkdf2.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
	// KEY_LENGTH 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

View Source
var ErrEmptyField error = errors.New("function parameters must not be empty")
View Source
var ErrInvalidHashFunction error = errors.New("invalid hash function was provided")

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/pbkdf2"
)

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

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/pbkdf2"
)

func main() {
  hash := "$pbkdf2sha512$v=0$i=4096$87a39b3cf30626bc7cf6534ac3a14ddf$d32093416bf521ff0..."

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

Types

type Config

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

Config initialize the config require to create a hash function

type HashFunction added in v1.2.0

type HashFunction int
const (
	SHA1 HashFunction = iota
	SHA256
	SHA224
	SHA512
	SHA384
	MD5
)

Jump to

Keyboard shortcuts

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