scrypt

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

README

PHC Crypto - Scrypt

Go Reference

According to Wikipedia:

scrypt (pronounced "ess crypt") is a password-based key derivation function created by Colin Percival, originally for the Tarsnap online backup service. The algorithm was specifically designed to make it costly to perform large-scale custom hardware attacks by requiring large amounts of memory. In 2016, the scrypt algorithm was published by IETF as RFC 7914. A simplified version of scrypt is used as a proof-of-work scheme by a number of cryptocurrencies, first implemented by an anonymous programmer called ArtForz in Tenebrix and followed by Fairbrix and Litecoin soon after.

Configuration options

Key Type Default Notes
Cost int 32768 Iterations count (affects memory and CPU usage)
Rounds int 8 Block size (affects memory and CPU usage)
Parallelism int 1 Parallelism factor (threads to run in parallel - affects the memory, CPU usage).
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.Scrypt, phccrypto.Config{
		Parallelism: 3,
	})
	if err != nil {
		fmt.Println(err)
	}

	hash, err := phccrypto.Hash("password")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // $scrypt$v=0$p=3,ln=32768,r=8$64ecb15ec1aa81bc403a892efb2289ce$4fc8d3bc...

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

func main() {
	hash, err := scrypt.Hash("password", scrypt.Config{
		Parallelism: 3,
	})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // $scrypt$v=0$p=3,ln=32768,r=8$64ecb15ec1aa81bc403a892efb2289ce$4fc8d3bc...

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

Documentation

Index

Constants

View Source
const (
	// KEYLEN is how many bytes to generate as output.
	KEYLEN = 32
	// COST is the iterations count (affects memory and CPU usage).
	// Minimum for interactive login: 16384.
	// Ideal for file encryption: 1048576.
	COST = 32768
	// ROUNDS is the block size (affects memory and CPU usage).
	ROUNDS = 8
	// PARALLELISM is the parallelism factor (threads to run in parallel - affects the memory, CPU usage).
	PARALLELISM = 1
	// 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")

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

func main() {
	hash, err := scrypt.Hash("password", scrypt.Config{
		Parallelism: 3,
	})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // $scrypt$v=0$p=3,ln=32768,r=8$64ecb15ec1aa81bc403a892efb2289ce$4fc8d3bc...
}

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

func main() {
	hash := "$scrypt$v=0$p=3,ln=32768,r=8$64ecb15ec1aa81bc403a892efb2289ce$4fc8d3bc..."

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

Types

type Config

type Config struct {
	Cost        int
	Rounds      int
	Parallelism int
	KeyLen      int
	SaltLen     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