passwd

package
v12.120.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: Apache-2.0 Imports: 12 Imported by: 6

Documentation

Overview

Package passwd contains methods for working with passwords

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyPassword = errors.New("Password can't be empty")
	ErrEmptyPepper   = errors.New("Pepper can't be empty")
	ErrInvalidPepper = errors.New("Pepper has invalid size")
)

Functions

func Check

func Check(password, pepper, hash string) bool

Check compares password with encrypted hash

Example
password, pepper := "MyPassword", "ABCD1234abcd1234"
hash, err := Hash(password, pepper)

if err != nil {
	panic(err.Error())
}

fmt.Printf("Valid: %t\n", Check(password, pepper, hash))
Output:

Valid: true

func CheckBytes

func CheckBytes(password, pepper, hash []byte) bool

CheckBytes compares password with encrypted hash

Example
password, pepper := []byte("MyPassword"), []byte("ABCD1234abcd1234")
hash, err := HashBytes(password, pepper)

if err != nil {
	panic(err.Error())
}

fmt.Printf("Valid: %t\n", CheckBytes(password, pepper, hash))
Output:

Valid: true

func GenPassword

func GenPassword(length int, strength Strength) string

GenPassword generates random password

Example
weakPassword := GenPassword(16, STRENGTH_WEAK)
medPassword := GenPassword(16, STRENGTH_MEDIUM)
strongPassword := GenPassword(16, STRENGTH_STRONG)

fmt.Printf("Weak password: %s\n", weakPassword)
fmt.Printf("Medium password: %s\n", medPassword)
fmt.Printf("Strong password: %s\n", strongPassword)
Output:

func GenPasswordBytes

func GenPasswordBytes(length int, strength Strength) []byte

GenPassword generates random password

Example
weakPassword := GenPasswordBytes(16, STRENGTH_WEAK)
medPassword := GenPasswordBytes(16, STRENGTH_MEDIUM)
strongPassword := GenPasswordBytes(16, STRENGTH_STRONG)

fmt.Printf("Weak password: %s\n", weakPassword)
fmt.Printf("Medium password: %s\n", medPassword)
fmt.Printf("Strong password: %s\n", strongPassword)
Output:

func GenPasswordBytesVariations

func GenPasswordBytesVariations(password []byte) [][]byte

GenPasswordBytesVariations generates password variants with possible typos fixes (case swap for all letters, first leter swap, password without last symbol)

Example
password := []byte("myPassword12345")
variants := GenPasswordBytesVariations(password)

fmt.Printf("Variants: %v\n", variants)
Output:

Variants: [[77 89 112 65 83 83 87 79 82 68 49 50 51 52 53] [77 121 80 97 115 115 119 111 114 100 49 50 51 52 53] [109 121 80 97 115 115 119 111 114 100 49 50 51 52]]

func GenPasswordVariations

func GenPasswordVariations(password string) []string

GenPasswordVariations generates password variants with possible typos fixes (case swap for all letters, first leter swap, password without last symbol)

Example
password := "myPassword12345"
variants := GenPasswordVariations(password)

fmt.Printf("Variants: %v\n", variants)
Output:

Variants: [MYpASSWORD12345 MyPassword12345 myPassword1234]

func Hash

func Hash(password, pepper string) (string, error)

Hash creates hash and encrypts it with salt and pepper

Example
password, pepper := "MyPassword", "ABCD1234abcd1234"
hash, err := Hash(password, pepper)

if err != nil {
	panic(err.Error())
}

fmt.Printf("Hash: %s\n", hash)
Output:

func HashBytes

func HashBytes(password, pepper []byte) ([]byte, error)

HashBytes creates hash and encrypts it with salt and pepper

Example
password, pepper := []byte("MyPassword"), []byte("ABCD1234abcd1234")
hash, err := HashBytes(password, pepper)

if err != nil {
	panic(err.Error())
}

fmt.Printf("Hash: %s\n", hash)
Output:

Types

type Strength

type Strength uint8
const (
	STRENGTH_WEAK   Strength = 0 // Only lowercase English alphabet characters
	STRENGTH_MEDIUM Strength = 1 // Lowercase and uppercase English alphabet characters, digits
	STRENGTH_STRONG Strength = 2 // Lowercase and uppercase English alphabet characters, digits, special symbols
)

func GetPasswordBytesStrength

func GetPasswordBytesStrength(password []byte) Strength

GetPasswordBytesStrength returns password strength

Example
strength := GetPasswordBytesStrength([]byte("secret1234%$"))

switch strength {
case STRENGTH_STRONG:
	fmt.Println("Password is strong")
case STRENGTH_MEDIUM:
	fmt.Println("Password is ok")
case STRENGTH_WEAK:
	fmt.Println("Password is weak")
}
Output:

Password is ok

func GetPasswordStrength

func GetPasswordStrength(password string) Strength

GetPasswordStrength returns password strength

Example
strength := GetPasswordStrength("secret1234%$")

switch strength {
case STRENGTH_STRONG:
	fmt.Println("Password is strong")
case STRENGTH_MEDIUM:
	fmt.Println("Password is ok")
case STRENGTH_WEAK:
	fmt.Println("Password is weak")
}
Output:

Password is ok

Jump to

Keyboard shortcuts

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