crypt

package module
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: Apache-2.0 Imports: 12 Imported by: 5

README

Crypt

Build Status Go Report Card Documentation

Crypt implementation in pure Go. It provides a unified interface to varous hashing algorithms for key derivation.

Example

func main() {
    password := "password"
    settings := "$argon2id$v=19$m=65536,t=2,p=4$c2FsdHNhbHQ" // salt = "saltsalt"

    encoded, err := crypt.Crypt(password, settings)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(encoded)
    // Output: $argon2id$v=19$m=65536,t=2,p=4$c2FsdHNhbHQ$mxUf7CB5gEwtDSiHfZCvxj17E8XeTFh2fpti1ioD3SA
}

Algorithm

Currently, the following algorithms are supported:

Code Name Example
1 MD5 $1$deadbeef$Q7g0UO4hRC0mgQUQ/qkjZ0
5 SHA256 $5$saltstring$5B8vYYiY.CVt1RlTTf8KbXBH3hsxY/GNooZaBBGWEc5
6 SHA512 $6$saltstring$svn8UoSVapNtMuq1ukKS4tPQd8iKwSMHWjl/O817G3uBnIFNjnQJuesI68u4OTLiBFdcbYEdFCoEOfaS35inz1
2a bcrypt $2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq
argon2i Argon2i $argon2i$v=19$m=65536,t=2$c29tZXNhbHQ$IMit9qkFULCMA/ViizL57cnTLOa5DiVM9eMwpAvPwr4
argon2id Argon2id $argon2id$v=19$m=65536,t=2,p=4$c29tZXNhbHQ$GpZ3sK/oH9p7VIiV56G/64Zo/8GaUw434IimaPqxwCo

It's recommended that you used argon2id for crypting passwords.

This implementation is inspired by crypt.

License

The code is licensed under Apache 2.0

Documentation

Index

Constants

View Source
const Argon2iPrefix = "$argon2i$"

Argon2iPrefix defines the settings prefix for argon2i hashes.

View Source
const Argon2idPrefix = "$argon2id$"

Argon2idPrefix defines the settings prefix for argon2id hashes.

View Source
const BCryptPrefix = "$2a$"

BCryptPrefix defines the settings prefix for bcrypt hashes.

View Source
const MD5Prefix = "$1$"

MD5Prefix defines the settings prefix for md5 hashes.

View Source
const SHA256Prefix = "$5$"

SHA256Prefix defines the settings prefix for sha256 hashes.

View Source
const SHA512Prefix = "$6$"

SHA512Prefix defines the settings prefix for sha512 hashes.

Variables

View Source
var Base64Encoding = base64.StdEncoding.WithPadding(base64.NoPadding)

Base64Encoding implements the crypt-specific base63 encoding.

Functions

func Argon2iSettings

func Argon2iSettings(m, t, p, k int, salts ...string) (string, error)

Argon2iSettings returns argon2i settings with the provided parameter.

func Argon2idSettings added in v0.2.0

func Argon2idSettings(m, t, p, k int, salts ...string) (string, error)

Argon2idSettings returns argon2id settings with the provided parameter.

func BCryptSettings

func BCryptSettings(cost int, salts ...string) (string, error)

BCryptSettings returns bcrypt settings with the provided parameter.

func Crypt

func Crypt(password, settings string) (string, error)

Crypt hashes the provided password using the provided salt.

func Encode24BitBase64

func Encode24BitBase64(src []byte) []byte

Encode24BitBase64 implements a special version of base64 that is used with md5, sha256 and sha512.

func RegisterAlgorithm

func RegisterAlgorithm(prefix string, algorithm Algorithm)

RegisterAlgorithm registers an algorithm under the provided prefix.

func Settings added in v0.3.0

func Settings(settings string) string

Settings removes the hash from the provided settings and returns the result.

Types

type Algorithm

type Algorithm func(string, string) (string, error)

Algorithm defines a algorithm.

type Parameter

type Parameter map[string]string

Parameter defines a parameter map.

func DecodeSettings

func DecodeSettings(settings string) (code string, parameter Parameter, salt string, hash string, err error)

DecodeSettings decodes the provided settings string into it's parts.

func (Parameter) GetInt

func (p Parameter) GetInt(key string, defaultValue int) int

GetInt tries to return an int value from the parameter map and returns the provided defaultValue on failure.

Directories

Path Synopsis
example
internal
bcrypt
Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing algorithm.
Package bcrypt implements Provos and Mazières's bcrypt adaptive hashing algorithm.

Jump to

Keyboard shortcuts

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