phccrypto

package module
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: 0

README

PHC Crypto

GitHub release (latest SemVer including pre-releases) Go Reference Go Report Card GitHub codecov CodeFactor Codacy Badge Build test Build test

Inspired by Upash, also implementing PHC string format

Usage

Currently there are two options of using this package:

  1. Import all
  2. Import specific hash function

Bear in mind, these usage function might be changed in the near future.

Currently supported formats
  • Bcrypt
  • Argon2i & Argon2id
  • PBKDF2
  • Scrypt

For details regarding configs, please refer to their own directory.

Option 1 - Import all
package main

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

func main() {
	// Create a crypto instance
	// Change the scope name to your prefered hashing algorithm
	// Available options are: Bcrypt, Scrypt, Argon2, PBKDF2
	crypto, err := phccrypto.Use(phccrypto.Scrypt, phccrypto.Config{})

	hash, err := crypto.Hash("password123")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // returns string ($scrypt$v=0$p=1,ln=32768,r=8$402ffb0b23cd3d3a$62daeae2ac...)

	verify, err := crypto.Verify(hash, "password123")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(verify) // returns boolean (true/false)
}
Option 2 - Import specific hash function
package main

import "github.com/aldy505/phc-crypto/scrypt"

func main() {
	// Change the scope name to your prefered hashing algorithm
	hash, err := scrypt.Hash("password123", scrypt.Config{})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(hash) // returns string ($scrypt$v=0$p=1,ln=32768,r=8$402ffb0b23cd3d3a$62daeae2ac...)

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

Contribute

Yes please! I'm still new to Go and I create this module (or package if you will) to help me fulfill a need on my project. Feel free to refactor, add new feature, fix unknown bugs, and have fun!

LICENSE

MIT License

Copyright (c) 2021-present Reinaldy Rafli and PHC Crypto collaborators

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAlgoNotSupported error = errors.New("the algorithm provided is not supported")
View Source
var ErrEmptyField error = errors.New("function parameters must not be empty")

Functions

This section is empty.

Types

type Algo

type Algo struct {
	Name   Algorithm
	Config *Config
}

Algo returns struct that will be use on Hash and Verify function

func Use

func Use(name Algorithm, config Config) (*Algo, error)

Use initiates the hash/verify function. Available hash functions are: bcrypt, scrypt, argon2, pbkdf2. Please refer to each hash folder for configuration information.

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

func main() {
	// Create a crypto instance
	// Change the scope name to your prefered hashing algorithm
	// Available options are: Bcrypt, Scrypt, Argon2, PBKDF2
	crypto, err := phccrypto.Use(phccrypto.Scrypt, phccrypto.Config{})

  hash, err := crypto.Hash("password123")
  if err != nil {
  	fmt.Println(err)
  }
  fmt.Println(hash) // returns string ($scrypt$v=0$p=1,ln=32768,r=8$402ffb0b23cd3d3a$62daeae2ac...)

	verify, err := crypto.Verify(hash, "password123")
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(verify) // returns boolean (true/false)
}

func (*Algo) Hash

func (a *Algo) Hash(plain string) (hash string, err error)

Hash returns a PHC formatted string of a hash function (that was initiated from Use).

func (*Algo) Verify

func (a *Algo) Verify(hash, plain string) (verify bool, err error)

Verify returns a boolean of a hash function (that was initiated from Use).

type Algorithm added in v1.1.0

type Algorithm int
const (
	Scrypt Algorithm = iota
	Bcrypt
	Argon2
	PBKDF2
)

type Config

type Config struct {
	Cost        int
	Rounds      int
	Parallelism int
	KeyLen      int
	SaltLen     int
	Variant     argon2.Variant
	HashFunc    pbkdf2.HashFunction
}

Config returns the general config of the hashing function

Directories

Path Synopsis
Package format is meant to be utils for PHC Crypto package.
Package format is meant to be utils for PHC Crypto package.

Jump to

Keyboard shortcuts

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