sha512

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package sha512 implements the SHA-512 hashing algorithm for crypt(3).

Index

Examples

Constants

View Source
const (
	MaxSaltLength     = 16
	DefaultSaltLength = MaxSaltLength
)
View Source
const (
	MinRounds      = 1000
	MaxRounds      = 999999999
	DefaultRounds  = 656000
	ImplicitRounds = 5000 // the value if the rounds parameter is omitted from the hash string
)
View Source
const Prefix = "$6$"

Variables

This section is empty.

Functions

func Check

func Check(hash, password string) error

Check compares the given crypt(3) SHA-512 hash with a new hash derived from the password. Returns nil on success, or an error on failure.

Example
package main

import (
	"fmt"

	"github.com/sergeymakinen/go-crypt/sha512"
)

func main() {
	hash := "$6$rounds=505000$69oRpYjidkp7hFdm$nbf4615NgTuG8kCnGYSjz/lXw4KrGMVR16cbCa9CSIHXK8UXwCK9bzCqDUw/I8hgb9Wstd1w5Bwgu5YG6Q.dm."
	fmt.Println(sha512.Check(hash, "password"))
	fmt.Println(sha512.Check(hash, "test"))
}
Output:

<nil>
hash and password mismatch

func Key

func Key(password, salt []byte, rounds uint32) ([]byte, error)

Key returns a SHA-512 key derived from the password, salt and rounds.

Example
package main

import (
	"fmt"

	"github.com/sergeymakinen/go-crypt/hash"
	"github.com/sergeymakinen/go-crypt/sha512"
)

func main() {
	salt, rounds, _ := sha512.Params("$6$rounds=505000$69oRpYjidkp7hFdm$nbf4615NgTuG8kCnGYSjz/lXw4KrGMVR16cbCa9CSIHXK8UXwCK9bzCqDUw/I8hgb9Wstd1w5Bwgu5YG6Q.dm.")
	fmt.Println(string(salt))
	fmt.Println(rounds)

	key, _ := sha512.Key([]byte("password"), salt, rounds)
	fmt.Println(hash.LittleEndianEncoding.EncodeToString(key))
}
Output:

69oRpYjidkp7hFdm
505000
nbf4615NgTuG8kCnGYSjz/lXw4KrGMVR16cbCa9CSIHXK8UXwCK9bzCqDUw/I8hgb9Wstd1w5Bwgu5YG6Q.dm.

func NewHash

func NewHash(password string, rounds uint32) (string, error)

NewHash returns the crypt(3) SHA-512 hash of the password with the given rounds.

func Params

func Params(hash string) (salt []byte, rounds uint32, err error)

Params returns the hashing salt and rounds used to create the given crypt(3) SHA-512 hash.

Example
package main

import (
	"fmt"

	"github.com/sergeymakinen/go-crypt/sha512"
)

func main() {
	salt, rounds, _ := sha512.Params("$6$rounds=505000$69oRpYjidkp7hFdm$nbf4615NgTuG8kCnGYSjz/lXw4KrGMVR16cbCa9CSIHXK8UXwCK9bzCqDUw/I8hgb9Wstd1w5Bwgu5YG6Q.dm.")
	fmt.Println(string(salt))
	fmt.Println(rounds)
}
Output:

69oRpYjidkp7hFdm
505000

Types

type InvalidRoundsError

type InvalidRoundsError uint32

InvalidRoundsError values describe errors resulting from an invalid round count.

func (InvalidRoundsError) Error

func (e InvalidRoundsError) Error() string

type InvalidSaltError

type InvalidSaltError byte

InvalidSaltError values describe errors resulting from an invalid character in a hash string.

func (InvalidSaltError) Error

func (e InvalidSaltError) Error() string

type InvalidSaltLengthError

type InvalidSaltLengthError int

InvalidSaltLengthError values describe errors resulting from an invalid length of a salt.

func (InvalidSaltLengthError) Error

func (e InvalidSaltLengthError) Error() string

type UnsupportedPrefixError

type UnsupportedPrefixError string

UnsupportedPrefixError values describe errors resulting from an unsupported prefix string.

func (UnsupportedPrefixError) Error

func (e UnsupportedPrefixError) Error() string

Jump to

Keyboard shortcuts

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