securerandom

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: GPL-3.0 Imports: 3 Imported by: 4

README

random

useful functions to produce crypto quality random stuff.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByteSlice added in v0.2.2

func ByteSlice(length uint) (bs []byte)

ByteSlice produces a slice of random bytes, of a provided length also used as the base for other outputs returns nil if it fails to make the []byte, or if the created slice is the wrong size

Example
// create a secure slice of bytes for use as a cryto key in a hash

keylen := 64 //bytes
pw := []byte("plain text password")

// ByteSlice requires uint value for length
key := ByteSlice(uint(keylen))

// Use the created key to hash the plain text password
hmac := hmac.New(sha512.New, key)
hmac.Write(pw)
hashedPW := hmac.Sum(nil)

// Clear the plain text password
pw = nil

// Use the key and hashedPW
log.Printf("Password: \n%v\nSecure Key: \n%v\nHashed Password:\n%v", pw, key, hashedPW)
Output:

func String

func String(length uint) (message string)

String returns a secure URL compliant string of a provided length returns a zero value string if it fails to create the message

Example
// create a secure base64 encoded string for use as a unique identifer for a user

type user struct {
	id    string
	name  string
	email string
}

// The size of our identifier
var idLen uint = 128 //characters

// String requires uint value for length
key := String(idLen)

// We now have a 128 character URL safe string to add to our user
u := user{
	id:    key,
	name:  "Testy McTestface",
	email: "testy@mctestface.com",
}

// Use the new user
fmt.Printf("User ID: \n%v\nName: \n%v\nEmail:\n%v", u.id, u.name, u.email)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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