mixer

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 15, 2020 License: MIT Imports: 3 Imported by: 6

README

mixer

go-doc-img travis-img go-report-card-img Coverage Status

Mixer is a very simple encrypt and decrypt golang library for short strings such as id or hash.

Features

  • Security - Support for password(salt) encryption.
  • Symmetrical Support decrypt from the encrypted string.
  • Equal length - The length of the encrypted string is equal to the length of the original string.
  • Simple - The encryption algorithm works by replacing and mixing characters.
  • Custom Support for custom replacement characters.
  • LCG algorithm Use Linear Congruential Generator (LCG) algorithm to generate pseudorandom numbers.

Install

go get -u github.com/foolin/mixer

Or get the specified version:

go get github.com/foolin/mixer@{version}

The {version} release list: https://github.com/foolin/mixer/releases

Docs

See Mixer

Usage


package main

import (
	"fmt"
	"github.com/foolin/mixer"
)

func main() {

	//password
	salt := "a1b2c3d4"

	//the source to be encrypted
	sources := []string{
		"123456",
		"12345abcedf",
		"48656c6c6f204d69786572",
	}

	//NewHex(salt string, upper bool) create a new Mixer
	mix := mixer.NewHex(salt, false)

	for _, source := range sources {

		//Encode source data
		encodeData := mix.EncodeString(source)

		//Decode source data
		decodeData := mix.DecodeString(encodeData)

		//Output result
		fmt.Printf("-------\nsource: %v\nencode: %v\ndecode: %v\n-------\n", source, encodeData, decodeData)
	}
}

Run output:


-------
source: 123456
encode: 05139e
decode: 123456
-------
-------
source: 12345abcedf
encode: d513a8ce704
decode: 12345abcedf
-------
-------
source: 48656c6c6f204d69786572
encode: 9eb58942619f9bf9ce1589
decode: 48656c6c6f204d69786572
-------



Documentation

Index

Constants

View Source
const (
	LcgDefaultMudu int64 = 4294967296 //math.Pow(2.0, 32.0)
	LcgDefaultMult int64 = 1103515245
	LcgDefaultInc  int64 = 12345
	LcgMaxRand     int64 = 4294967295
)
View Source
const (
	//CharsCaseAlphanumeric the alphanumeric include upper and lower:`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
	CharsCaseAlphanumeric = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

	//CharsUpperAlphanumeric the alphanumeric include upper:`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ`
	CharsUpperAlphanumeric = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	//CharsLowerAlphanumeric the alphanumeric include lower:`0123456789abcdefghijklmnopqrstuvwxyz`
	CharsLowerAlphanumeric = "0123456789abcdefghijklmnopqrstuvwxyz"

	//CharsUpperAlphabet the upper alphabet:`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
	CharsUpperAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

	//CharsLowerAlphabet the lower alphabet:`abcdefghijklmnopqrstuvwxyz`
	CharsLowerAlphabet = "abcdefghijklmnopqrstuvwxyz"

	//CharsUpperHex the hex alphabet and numeric:`0123456789abcdef`
	CharsUpperHex = "0123456789ABCDEF"

	//CharsLowerHex the hex alphabet and numeric:`0123456789abcdef`
	CharsLowerHex = "0123456789abcdef"

	//CharsNumeric the numeric:`0123456789abcdef`
	CharsNumeric = "0123456789"
)
View Source
const (
	//AlphanumericCase alphanumericType is CharsCaseAlphanumeric
	AlphanumericCase = iota

	//AlphanumericUpper alphanumericType is CharsUpperAlphanumeric
	AlphanumericUpper

	//AlphanumericLower alphanumericType is CharsLowerAlphanumeric
	AlphanumericLower
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LCGRandom added in v0.0.3

type LCGRandom struct {
	// contains filtered or unexported fields
}

linear congruential generator

func NewLGC added in v0.0.3

func NewLGC(seed int64) *LCGRandom

func NewLGCWith added in v0.0.3

func NewLGCWith(seed, modulus, multiplier, increment int64) *LCGRandom

func (*LCGRandom) Int64 added in v0.0.3

func (g *LCGRandom) Int64() int64

func (*LCGRandom) Int64n added in v0.0.3

func (g *LCGRandom) Int64n(n int64) int64

func (*LCGRandom) Intn added in v0.0.3

func (g *LCGRandom) Intn(n int) int

func (*LCGRandom) Perm added in v0.0.3

func (g *LCGRandom) Perm(n int) []int

Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).

type Mixer

type Mixer struct {
	// contains filtered or unexported fields
}

Mixer a mixer instance for encode/decode

func MustNew

func MustNew(salt string, chars string, candidateChars ...string) *Mixer

MustNew must create a new mixer

func New

func New(salt string, chars string, candidateChars ...string) (*Mixer, error)

New create a new mixer

func NewAlphanumeric

func NewAlphanumeric(salt string, alphanumericType int) *Mixer

NewAlphanumeric must create a new mixer with alphanumeric

func NewHex

func NewHex(salt string, upper bool) *Mixer

NewHex must create a new mixer with hex

func NewNumeric

func NewNumeric(salt string) *Mixer

NewNumeric must create a new mixer with numeric

func (Mixer) Decode

func (m Mixer) Decode(data []rune) []rune

Decode decode char array

func (Mixer) DecodeString

func (m Mixer) DecodeString(data string) string

DecodeString decode string

func (Mixer) Encode

func (m Mixer) Encode(data []rune) []rune

Encode encode char array

func (Mixer) EncodeString

func (m Mixer) EncodeString(data string) string

EncodeString encode string

Directories

Path Synopsis
_examples
basic command

Jump to

Keyboard shortcuts

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