salt

package
v0.0.0-...-e11358b Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2015 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package salt : The salt package provides salting services for anyone who uses passwords

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GeneratePasswordWithRndSalt

func GeneratePasswordWithRndSalt(pass string, minSecretLen int, maxSecretLen int) ([]byte, []byte, error)

GeneratePasswordWithRndSalt : Return a generated salted password and the used salt from a given password

func GenerateSaltedPassword

func GenerateSaltedPassword(pwd []byte, minSecretLen int, maxSecretLen int, saltData []byte, passwordLen int) ([]byte, error)

GenerateSaltedPassword : generate a salted password using the given password and salt information

Example

This example shows how to generate a saltetd password from a given password and default salt

package main

import (
	"fmt"

	"github.com/ibm-security-innovation/libsecurity-go/salt"
)

const (
	minSecretLen = 1
	maxSecretLen = 255
)

var BasicSalt = []byte("A1B2")

func main() {
	pass := "MyPassword"
	res, err := salt.GenerateSaltedPassword([]byte(pass), minSecretLen, maxSecretLen, BasicSalt, -1)
	if err != nil {
		fmt.Println("GenerateSaltedPassword failed, error:", err)
	} else {
		fmt.Printf("* Generate basic salted password from a given password: '%v', using the default parameters (sha1, show full password, 1 iteration and random salt: %v) is: %v", pass, BasicSalt, res)
	}
}
Output:

func GetRandomSalt

func GetRandomSalt(size int) ([]byte, error)

GetRandomSalt : generate a random salt with the given length

Example

This example shows how to generate a randomly salted secret contain 8 characters, using hash function of md5 and 3 iterations of calculations

package main

import (
	"crypto/md5"
	"fmt"

	"github.com/ibm-security-innovation/libsecurity-go/salt"
)

const (
	minSecretLen = 1
	maxSecretLen = 255
)

var BasicSecret = []byte("ABCDABCD")

func main() {
	iter := 3
	f := md5.New
	size := 8
	saltLen := 32

	randSalt, _ := salt.GetRandomSalt(saltLen)
	mySalt, err := salt.NewSalt(BasicSecret, minSecretLen, maxSecretLen, randSalt)
	if err != nil {
		fmt.Println("Error while creating new salt structure:", err)
	}
	mySalt.Iterations = iter
	mySalt.OutputLen = size
	mySalt.Digest = f
	res, err := mySalt.Generate(minSecretLen, maxSecretLen)
	if err != nil {
		fmt.Println("GetRandomSaltExample failed, error:", err)
	} else {
		fmt.Println("* Salted password of secret key:", string(BasicSecret), ",random salt length:", saltLen, randSalt,
			"with", iter, "iterations, output password length:", size, "bytes and MD5 function is:", res)
	}
}
Output:

Types

type Salt

type Salt struct {
	Secret     []byte
	Salt       []byte
	OutputLen  int              // Number of digits in the code. Default is 6
	Iterations int              // Number of iterations to run the hash function, Default is 64
	Digest     func() hash.Hash // Digest type, Default is sha1
}

Salt : structure that holds all the parameters relevant to handle salting of password

func NewSalt

func NewSalt(secret []byte, minSecretLen int, maxSecretLen int, salt []byte) (*Salt, error)

NewSalt : The default Salt: use sha1, output length 16 bytes

func (Salt) Generate

func (s Salt) Generate(minSecretLen int, maxSecretLen int) ([]byte, error)

Generate : Return the encrypted data for a given salt and secret The way to add salt is: secret + salt TODO: output len from right or from left

func (Salt) Match

func (s Salt) Match(ref []byte, minSecretLen int, maxSecretLen int) (bool, error)

Match : compare 2 given salt information

func (Salt) String

func (s Salt) String() string

Jump to

Keyboard shortcuts

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