hashing

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2022 License: MIT Imports: 5 Imported by: 0

README

hashing

Go Report Card tests GoDoc MIT license

——The idea came from Laravel

Usage

package main

import (
	"github.com/go-packagist/framework/hashing"
	"github.com/go-packagist/framework/config"
	"github.com/go-packagist/framework/foundation"
)

func main()  {
    // use hashing.NewBcryptHasher
    bcrypt := hashing.NewBcryptHasher()
    
    bcrypt.Make("password")
    bcrypt.MustMake("password")
    bcrypt.Check("password", "hashed password")
    
    // use Manager
    m := hashing.NewManager(map[string]interface{}{
        "driver": "bcrypt",
    })
    
    m.Driver("bcrypt").Make("password")
    m.Driver().Make("password")
    
    // use foundation.Application
    app := foundation.NewApplication("./")
    
    app.Register(config.NewConfigProvider(app))
    app.MustMake("config").(*config.Config).Set("hashing", map[string]interface{}{
        "driver": "bcrypt",
    })
    
    app.Register(hashing.NewHashProvider(app))
    
    hashing.FacadeMustHash().Driver("bcrypt").Make("password")
    hashing.FacadeMustHashBcrypt().Make("password")
    app.MustMake("hash").(*hashing.HashManager).Driver("bcrypt").Make("password")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHashProvider

func NewHashProvider(app *foundation.Application) *hashProvider

NewHashProvider Bootstrap bootstraps the hashing services.

Types

type BcryptHasher

type BcryptHasher struct {
}

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 HashManager

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

func NewHashManager

func NewHashManager(config map[string]interface{}) *HashManager

NewHashManager creates a new hashing manager instance. config example:

config := map[string]interface{}{
	"driver": "bcrypt",
}

func (*HashManager) Driver

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

Driver gets the hasher instance by driver name.

type Hasher

type Hasher interface {
	Make(value string) (string, error)
	MustMake(value string) string
	Check(value, hashedValue string) bool
}

func NewBcryptHasher

func NewBcryptHasher() Hasher

NewBcryptHasher returns a new bcrypt hasher.

type Md5Hasher added in v0.1.1

type Md5Hasher struct {
}

func NewMd5Hasher added in v0.1.1

func NewMd5Hasher() *Md5Hasher

NewMd5Hasher creates a new md5 hasher instance.

func (*Md5Hasher) Check added in v0.1.1

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

Check checks the given value and hashed value.

func (*Md5Hasher) Make added in v0.1.1

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

Make generates a new hashed value.

func (*Md5Hasher) MustMake added in v0.1.1

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

MustMake generates a new hashed value.

Jump to

Keyboard shortcuts

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