identity

package module
v0.0.0-...-d8047f0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: BSD-3-Clause Imports: 11 Imported by: 0

README

identity

CI status GoDoc

Go library implementing the ASP.NET Core identity password hashing algorithm

Install

go get -u github.com/SuNNjek/identity

Usage

Generate new password
package example

import (
    "github.com/SuNNjek/identity"
)

func main() {
    password := "my password"

    // Generate new random salt
    salt, _ := identity.GenerateSalt(identity.DefaultSaltLength)

    // Hash the password using the default parameters
    hash := identity.HashPasswordV3(
        []byte(password),
        salt,
        identity.DefaultHashAlgorithm,
        identity.DefaultIterations,
        identity.DefaultNumBytes)

    // ...
}
Verify existing password hash
package example

import (
    "github.com/SuNNjek/identity"
)

func main() {
    // Placeholder, replace with your logic to retrieve password hash
    hashedPassword := getPasswordHash()
    enteredPassword := "my password"

    // Verify the entered password against the hashed one
    passwordsMatch := identity.Verify(hashedPassword, []byte(enteredPassword))

    // ...
}

Documentation

Index

Constants

View Source
const (
	// DefaultSaltLength is the default salt length for new passwords
	DefaultSaltLength = 16
	// DefaultIterations is the default number of iterations for new passwords
	DefaultIterations = 10000
	// DefaultHashAlgorithm is the default hashing algorithm for new passwords
	DefaultHashAlgorithm = SHA256
	// DefaultNumBytes is the default number of bytes reserved for the subkey
	DefaultNumBytes = 32
)

Variables

This section is empty.

Functions

func GenerateSalt

func GenerateSalt(length int) ([]byte, error)

GenerateSalt generates a cryptographically safe random salt of the specified length

func HashPasswordV2

func HashPasswordV2(password, salt []byte) []byte

func HashPasswordV3

func HashPasswordV3(password, salt []byte, algorithm HashAlgorithm, iterations, numBytes int) []byte

HashPasswordV3 hashes the given password with the specified salt, algorithm, number of iterations and hash length

func Verify

func Verify(hashedPassword, clearPassword []byte) bool

func VerifyPasswordV2

func VerifyPasswordV2(hashedPassword, clearPassword []byte) bool

func VerifyPasswordV3

func VerifyPasswordV3(hashedPassword, clearPassword []byte) bool

VerifyPasswordV3 verifies the given hashed password against the given clear text password and returns true if they match

Types

type HashAlgorithm

type HashAlgorithm uint32

HashAlgorithm represents the hashing algorithm used to hash the password

const (
	SHA1 HashAlgorithm = iota
	SHA256
	SHA512
)

Jump to

Keyboard shortcuts

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