hashpass

package module
v0.0.0-...-062432b Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2019 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

hashpass

Build Status Coverage Status Go Report Card GoDoc

Package hashpass is a package capable parsing and generating libc compatible password hashes If salt is a character string starting with the characters "$id$" followed by a string terminated by "$": $id$salt$encrypted then instead of using the DES machine, id identifies the encryption method used and this then determines how the rest of the password string is interpreted.

Download:

go get github.com/1800alex/go-utilities-hashpass

Package hashpass is a package capable parsing and generating libc compatible password hashes

If salt is a character string starting with the characters
"$id$" followed by a string terminated by "$":

	$id$salt$encrypted

then instead of using the DES machine, id identifies the
encryption method used and this then determines how the rest
of the password string is interpreted.  The following values
of id are supported:

		ID  | Method
		─────────────────────────────────────────────────────────
		1   | MD5
		2a  | Blowfish (not in mainline glibc; added in some
			| Linux distributions, not supported by this package)
		5   | SHA-256 (since glibc 2.7)
		6   | SHA-512 (since glibc 2.7)

So $5$salt$encrypted is an SHA-256 encoded password and
$6$salt$encrypted is an SHA-512 encoded one.

"salt" stands for the up to 16 characters following "$id$" in
the salt.  The encrypted part of the password string is the
actual computed password.  The size of this string is fixed:

MD5     | 22 characters
SHA-256 | 43 characters
SHA-512 | 86 characters

Examples

Hash Code:

{
	var hash HashPass
	var err error
	hash, err = Hash("$6$1bM59POonOkcgZVg$", "7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")
	if err != nil {
		fmt.Println(err.Error())
	}
	if hash.Hash == "" {
		fmt.Println("something went wrong")
	}
}

MD5Hash Code:

{
	var hash HashPass
	var err error
	hash, err = MD5Hash("7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")
	if err != nil {
		fmt.Println(err.Error())
	}
	if hash.Hash == "" {
		fmt.Println("something went wrong")
	}
}

SHA256Hash Code:

{
	var hash HashPass
	var err error
	hash, err = SHA256Hash("7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")
	if err != nil {
		fmt.Println(err.Error())
	}
	if hash.Hash == "" {
		fmt.Println("something went wrong")
	}
}

SHA512Hash Code:

{
	var hash HashPass
	var err error
	hash, err = SHA512Hash("7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")
	if err != nil {
		fmt.Println(err.Error())
	}
	if hash.Hash == "" {
		fmt.Println("something went wrong")
	}
	fmt.Println(hash.Hash)
}

Verify Code:

{
	var verify bool
	var err error
	verify, err = Verify("$1$oNAuG282$IJK4Qq1ySC1tW03dYg7XS.", "password")
	if err != nil {
		fmt.Println(err.Error())
	}
	if verify == false {
		fmt.Println("incorrect password")
	} else {
		fmt.Println("correct password")
	}
}

Documentation

Overview

Package hashpass is a package capable parsing and generating libc compatible password hashes

If salt is a character string starting with the characters
"$id$" followed by a string terminated by "$":

	$id$salt$encrypted

then instead of using the DES machine, id identifies the
encryption method used and this then determines how the rest
of the password string is interpreted.  The following values
of id are supported:

		ID  | Method
		─────────────────────────────────────────────────────────
		1   | MD5
		2a  | Blowfish (not in mainline glibc; added in some
			| Linux distributions, not supported by this package)
		5   | SHA-256 (since glibc 2.7)
		6   | SHA-512 (since glibc 2.7)

So $5$salt$encrypted is an SHA-256 encoded password and
$6$salt$encrypted is an SHA-512 encoded one.

"salt" stands for the up to 16 characters following "$id$" in
the salt.  The encrypted part of the password string is the
actual computed password.  The size of this string is fixed:

MD5     | 22 characters
SHA-256 | 43 characters
SHA-512 | 86 characters

Index

Examples

Constants

View Source
const (
	// MD5 Hash type value
	MD5 = "$1"
	// SHA256 Hash type  value
	SHA256 = "$5"
	// MDSHA5125 Hash type  value
	SHA512 = "$6"
)

Variables

View Source
var (
	// ErrInvalidHashType is the error returned when the hash type
	// is not valid.
	ErrInvalidHashType = errors.New("invalid hash type")

	// ErrInvalidSalt is the error returned when the salt
	// is not valid.
	ErrInvalidSalt = errors.New("invalid salt")

	// ErrInvalidSalt is the error returned when the hash length
	// is not valid.
	ErrInvalidLength = errors.New("invalid hash length")
)
View Source
var DepGetRandomMD5Seed = func() (result string, err error) {
	result, err = password.Generate(8, true, false, false, true)
	return
}

DepGetRandomMD5Seed is an external dependancy wrapper to get a random 8 character seed.

View Source
var DepGetRandomSHA256Seed = func() (result string, err error) {
	result, err = password.Generate(16, true, false, false, true)
	return
}

DepGetRandomSHA256Seed is an external dependancy wrapper to get a random 16 character seed.

View Source
var DepGetRandomSHA512Seed = func() (result string, err error) {
	result, err = password.Generate(16, true, false, false, true)
	return
}

DepGetRandomSHA512Seed is an external dependancy wrapper to get a random 16 character seed.

Functions

func Verify

func Verify(hash string, password string) (result bool, err error)

Verify verifies that a password matches the computed hash

Example
var verify bool
var err error

verify, err = Verify("$1$oNAuG282$IJK4Qq1ySC1tW03dYg7XS.", "password")

if err != nil {
	fmt.Println(err.Error())
}

if verify == false {
	fmt.Println("incorrect password")
} else {
	fmt.Println("correct password")
}

Types

type HashPass

type HashPass struct {
	Type string
	Salt string
	Hash string
}

HashPass defines the resulting object for all the hashpass functions

func Hash

func Hash(salt string, password string) (result HashPass, err error)

Hash creates a hash from a known salt

Example
var hash HashPass
var err error

hash, err = Hash("$6$1bM59POonOkcgZVg$", "7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")

if err != nil {
	fmt.Println(err.Error())
}

if hash.Hash == "" {
	fmt.Println("something went wrong")
}

func MD5Hash

func MD5Hash(password string) (result HashPass, err error)

MD5Hash creates a new MD5 hash from a randomly generated salt

Example
var hash HashPass
var err error

hash, err = MD5Hash("7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")

if err != nil {
	fmt.Println(err.Error())
}

if hash.Hash == "" {
	fmt.Println("something went wrong")
}

func ParseHash

func ParseHash(input string) (result HashPass, err error)

ParseHash parses a password hash and deduces the type, salt, and verifies format is correct

func SHA256Hash

func SHA256Hash(password string) (result HashPass, err error)

SHA256Hash creates a new SHA256 hash from a randomly generated salt

Example
var hash HashPass
var err error

hash, err = SHA256Hash("7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")

if err != nil {
	fmt.Println(err.Error())
}

if hash.Hash == "" {
	fmt.Println("something went wrong")
}

func SHA512Hash

func SHA512Hash(password string) (result HashPass, err error)

SHA512Hash creates a new SHA512 hash from a randomly generated salt

Example
var hash HashPass
var err error

hash, err = SHA512Hash("7Jh29J49Q9hgLYHxQMduzFcTkwrz5AB4")

if err != nil {
	fmt.Println(err.Error())
}

if hash.Hash == "" {
	fmt.Println("something went wrong")
}

fmt.Println(hash.Hash)

Jump to

Keyboard shortcuts

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