hashing

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: May 28, 2023 License: MIT Imports: 5 Imported by: 4

README

hashing

Go Go Version GoDoc codecov Go Report Card tests MIT license

Installation

go get github.com/go-packagist/hashing

Usage

package main

import (
	"fmt"
	"github.com/go-packagist/hashing"
)

func main() {
	// Example1: use md5 hasher
	md5 := hashing.Md5Hasher{} // OR md5 := hashing.NewMd5Hasher()
	md5Hashed, err := md5.Make("123456")
	if err != nil {
		panic(err)
	}
	fmt.Println(md5Hashed)                      // e10adc3949ba59abbe56e057f20f883e
	fmt.Println(md5.Check("123456", md5Hashed)) // true

	// Example2: use bcrypt hasher
	bcrypt := hashing.BcryptHasher{} // OR bcrypt := hashing.NewBcryptHasher()
	bcryptHashed, err := bcrypt.Make("123456")
	if err != nil {
		panic(err)
	}
	fmt.Println(bcryptHashed)
	fmt.Println(bcrypt.Check("123456", bcryptHashed)) // true

	// Example3: use manager
	manager := hashing.NewManager(&hashing.Config{
		Driver: "bcrypt", // OR Driver: "md5"
	})

	// use default driver
	fmt.Println(manager.Driver().MustMake("123456"))
	fmt.Println(manager.Driver().Check("123456", manager.Driver().MustMake("123456"))) // true

	// use specified driver
	fmt.Println(manager.Driver("md5").MustMake("123456"))
	fmt.Println(manager.Driver("md5").Check("123456", manager.Driver("md5").MustMake("123456"))) // true
	fmt.Println(manager.Driver("bcrypt").MustMake("123456"))
	fmt.Println(manager.Driver("bcrypt").Check("123456", manager.Driver("bcrypt").MustMake("123456"))) // true
}

License

The MIT License (MIT). Please see License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BcryptHasher

type BcryptHasher struct {
}

func NewBcryptHasher

func NewBcryptHasher() *BcryptHasher

NewBcryptHasher returns a new bcrypt hasher.

func (*BcryptHasher) Check

func (b *BcryptHasher) Check(value, hashedValue string) bool

Check returns true if the value matches the hashed value.

func (*BcryptHasher) Make

func (b *BcryptHasher) Make(value string) (string, error)

Make returns the hashed value.

func (*BcryptHasher) MakeWithCost

func (b *BcryptHasher) MakeWithCost(value string, cost int) (string, error)

MakeWithCost returns the hashed value with the given cost.

func (*BcryptHasher) MustMake

func (b *BcryptHasher) MustMake(value string) string

MustMake returns the hashed value.

type Config

type Config struct {
	Driver string
}

type Hasher

type Hasher interface {
	// Make a hash value from the given value.
	Make(value string) (string, error)

	// MustMake a hash value from the given value.
	// if an error occurs, it will panic.
	MustMake(value string) string

	// Check the given value matches the given hashed value.
	// if Make() is error, it will return false.
	Check(value, hashedValue string) bool
}

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func NewManager

func NewManager(config *Config) *Manager

NewManager creates a new hashing manager instance. config example:

config := &Config{
	Driver: "bcrypt",
}

func (*Manager) Check added in v1.1.1

func (m *Manager) Check(value, hashedValue string) bool

Check checks the given value is equal to the hashed value.

func (*Manager) Driver

func (m *Manager) Driver(driver ...string) Hasher

Driver gets the hasher instance by driver name.

func (*Manager) Make added in v1.1.1

func (m *Manager) Make(value string) (string, error)

Make creates a hash value for the given value.

func (*Manager) MustMake added in v1.1.1

func (m *Manager) MustMake(value string) string

MustMake creates a hash value for the given value.

type Md5Hasher

type Md5Hasher struct {
}

func NewMd5Hasher

func NewMd5Hasher() *Md5Hasher

NewMd5Hasher creates a new md5 hasher instance.

func (*Md5Hasher) Check

func (m *Md5Hasher) Check(value, hashedValue string) bool

Check checks the given value and hashed value.

func (*Md5Hasher) Make

func (m *Md5Hasher) Make(value string) (string, error)

Make generates a new hashed value.

func (*Md5Hasher) MustMake

func (m *Md5Hasher) MustMake(value string) string

MustMake generates a new hashed value.

type Sha1Hasher added in v1.1.0

type Sha1Hasher struct {
}

func NewSha1Hasher added in v1.1.0

func NewSha1Hasher() *Sha1Hasher

NewSha1Hasher creates a new sha1 hasher instance.

func (*Sha1Hasher) Check added in v1.1.0

func (s *Sha1Hasher) Check(value, hashedValue string) bool

Check checks the given value and hashed value.

func (*Sha1Hasher) Make added in v1.1.0

func (s *Sha1Hasher) Make(value string) (string, error)

Make generates a new hashed value.

func (*Sha1Hasher) MustMake added in v1.1.0

func (s *Sha1Hasher) MustMake(value string) string

MustMake generates a new hashed value.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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