hashing

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: MIT Imports: 5 Imported by: 0

README

hashing

Go Report Card tests GoDoc MIT license

——The idea came from Laravel

Installation

go get github.com/go-packagist/framework/hashing

Usage

package main

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

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 facades
	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))

	facades.MustHash().Driver().Make("password")
	facades.MustHash().Driver("bcrypt").Make("password")
	facades.MustHash().Driver("md5").Make("password")
	facades.BcryptHasher().Make("password")
	facades.Md5Hasher().Make("password")
	app.MustMake("hash").(*hashing.HashManager).Driver("bcrypt").Make("password")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 Config added in v0.4.1

type Config struct {
	Driver string
}

type HashProvider added in v0.5.0

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

HashProvider represents the hashing provider.

func NewHashProvider

func NewHashProvider(c *container.Container) *HashProvider

NewHashProvider Bootstrap bootstraps the hashing services.

func (*HashProvider) Boot added in v0.5.0

func (p *HashProvider) Boot()

func (*HashProvider) Register added in v0.5.0

func (p *HashProvider) Register()

Register registers the hashing services into the application.

type Hasher

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

func NewBcryptHasher

func NewBcryptHasher() Hasher

NewBcryptHasher returns a new bcrypt hasher.

type Manager added in v0.4.1

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

func NewManager added in v0.4.1

func NewManager(config *Config) *Manager

NewManager creates a new hashing manager instance. config example:

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

func (*Manager) Driver added in v0.4.1

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

Driver gets the hasher instance by driver name.

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