shash

package module
v0.0.0-...-9a39e31 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2018 License: MIT Imports: 2 Imported by: 0

README

Build Status Coverage Status GoDoc Go Report Card

Shash

A no-nonsense golang package for creating random string

This package is very simple, just do

	package main

	import (
		"fmt"
		"github.com/JesusIslam/shash"
	)

	func main() {
		h := shash.New(10)
		r, _ := h.Generate()
		fmt.Println(r)
	}

The example will generate a base62 random string with length of 5 (default).

For the documentation see here.

To test, just run go test, but you need to have gomega and ginkgo installed.

To install go get github.com/JesusIslam/shash.

Benchmark

On i3-3217U @1.8GHz with go test -bench . -benchtime=5s -cpu 4

BenchmarkGenerate5-4 	  500000	     15130 ns/op
BenchmarkGenerate16-4	  200000	     48357 ns/op
BenchmarkGenerate32-4	  100000	     98319 ns/op

Documentation

Overview

Package shash will generate a random, base-62, base-36, and base-10 string with user defined length without nonsense. Usage Example:

 func main() {
	 h := shash.New(10)
	 gen, _ := h.Generate()
	 fmt.Println(gen)
 }

Index

Constants

View Source
const (
	CHARSET         = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	CHARSET_BASE_36 = "abcdefghijklmnopqrstuvwxyz0123456789"
	CHARSET_BASE_10 = "0123456789"
	VERSION         = "0.1.0"
)

The base62 charset as const CHARSET.

Variables

View Source
var (
	MAX_LEN         = int64(len(CHARSET) - 1)
	MAX_LEN_BASE_36 = int64(len(CHARSET_BASE_36) - 1)
	MAX_LEN_BASE_10 = int64(len(CHARSET_BASE_10) - 1)
)

Functions

This section is empty.

Types

type Hash

type Hash struct {
	Length int
}

Hash only has one property: Length, with default value of 5 on creation.

func New

func New(n int) *Hash

Create a new Hash with default length 5.

func (*Hash) Generate

func (h *Hash) Generate() (string, error)

Generate() will return a random string in base62. Error will return if there is an error with the crypto/rand package, nil if there isn't any error.

func (*Hash) GenerateBase10

func (h *Hash) GenerateBase10() (string, error)

func (*Hash) GenerateBase36

func (h *Hash) GenerateBase36() (string, error)

Jump to

Keyboard shortcuts

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