password

package module
v0.0.0-...-c765b79 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2020 License: MIT Imports: 5 Imported by: 28

README

go-password-encoder

Build Status GoDoc Go Report Card

This package in Go provides functions to encode a raw password (example, during registration on a site), and later verify it (example, while logging in to the site).

Functions available:

func Encode(string, *Options) (string, string) {} // takes the raw password along with options, returns generated salt and hex encoded password
func Verify(string, string, string, *Options) bool {} // takes the raw password, the generated salt, and encoded password with options, and returns true or false

The Options struct is used to enable custom options:

type Options struct {
	SaltLen      int
	Iterations   int
	KeyLen       int
	HashFunction func() hash.Hash
}

Passing nil as the last argument in either function resorts to using the default options. The default options are as follows:

  • Length of generated salt for the user is 256
  • Iteration count in PBKDF2 function is 10000
  • Length of encoded key in PBKDF2 function is 512
  • Hash algorithm used is sha512

Hover over to Usage for a complete example.

Installation
go get github.com/anaskhan96/go-password-encoder

Run go test in the package's directory to run tests.

Usage

Following is an example depicting the usage of this package:

package main

import (
	"crypto/md5"
	"fmt"
	"github.com/anaskhan96/go-password-encoder"
)

func main() {
	// Using the default options
	salt, encodedPwd := password.Encode("generic password", nil)
	check := password.Verify("generic password", salt, encodedPwd, nil)
	fmt.Println(check) // true

	// Using custom options
	options := &password.Options{10, 10000, 50, md5.New}
	salt, encodedPwd = password.Encode("generic password", options)
	check = password.Verify("generic password", salt, encodedPwd, options)
	fmt.Println(check) // true
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Encode

func Encode(rawPwd string, options *Options) (string, string)

Encode takes two arguments, a raw password, and a pointer to an Options struct. In order to use default options, pass `nil` as the second argument. It returns the generated salt and encoded key for the user.

func Verify

func Verify(rawPwd string, salt string, encodedPwd string, options *Options) bool

Verify takes four arguments, the raw password, its generated salt, the encoded password, and a pointer to the Options struct, and returns a boolean value determining whether the password is the correct one or not. Passing `nil` as the last argument resorts to default options.

Types

type Options

type Options struct {
	SaltLen      int
	Iterations   int
	KeyLen       int
	HashFunction func() hash.Hash
}

Options is a struct for custom values of salt length, number of iterations, the encoded key's length, and the hash function being used. If set to `nil`, default options are used: &Options{ 256, 10000, 512, "sha512" }

Jump to

Keyboard shortcuts

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