mixer

package module
v0.0.4 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",
	}

	mixers := []struct {
		Name  string
		Mixer *mixer.Mixer
	}{
		//create a new mixer with case sensitive alphanumeric
		{"New(salt)", mixer.New(salt)},

		//create a new mixer with lowercase  alphanumeric
		{"Newt(salt, AlphanumericLower)", mixer.Newt(salt, mixer.AlphanumericLower)},
	}

	//foreach every source
	for _, source := range sources {
		//foreach mixer for encode and decode
		for _, m := range mixers {
			//Encode source data
			encodeData := m.Mixer.EncodeString(source)

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

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

}


Run output:

-------
 mixer: New(salt)
source: 123456
encode: W51Y9U
decode: 123456
-------
-------
 mixer: Newt(salt, AlphanumericLower)
source: 123456
encode: 05139e
decode: 123456
-------
-------
 mixer: New(salt)
source: 12345abcedf
encode: J51YZMDUtWS
decode: 12345abcedf
-------
-------
 mixer: Newt(salt, AlphanumericLower)
source: 12345abcedf
encode: r513spoet0n
decode: 12345abcedf
-------
-------
 mixer: New(salt)
source: 48656c6c6f204d69786572
encode: 9Ub5M9Slm19q9bq9DU15M9
decode: 48656c6c6f204d69786572
-------
-------
 mixer: Newt(salt, AlphanumericLower)
source: 48656c6c6f204d69786572
encode: 9eb5p9nlm19q9bq9oe15p9
decode: 48656c6c6f204d69786572
-------

Documentation

Index

Constants

View Source
const (
	//LcgDefaultModulus default modulus
	LcgDefaultModulus int64 = 4294967296 //math.Pow(2.0, 32.0)
	//LcgDefaultMultiplier default multiplier
	LcgDefaultMultiplier int64 = 1103515245
	//LcgDefaultIncrement default increment
	LcgDefaultIncrement int64 = 12345
)
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"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AlphanumericType added in v0.0.4

type AlphanumericType int

AlphanumericType alphanumeric type

const (
	//AlphanumericCase AlphanumericType is CharsCaseAlphanumeric
	AlphanumericCase AlphanumericType = iota

	//AlphanumericUpper AlphanumericType is CharsUpperAlphanumeric
	AlphanumericUpper

	//AlphanumericLower AlphanumericType is CharsLowerAlphanumeric
	AlphanumericLower
)

type LCGRandom added in v0.0.3

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

LCGRandom linear congruential generator

func NewLGC added in v0.0.3

func NewLGC(seed int64) *LCGRandom

NewLGC new LGC random

func NewLGCWith added in v0.0.3

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

NewLGCWith new LGC with more parameters.

func (*LCGRandom) Int64 added in v0.0.3

func (g *LCGRandom) Int64() int64

Int64 next random number

func (*LCGRandom) Int64n added in v0.0.3

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

Int64n next random int64 number in [0,n)

func (*LCGRandom) Intn added in v0.0.3

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

Intn next random int number in [0,n)

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 MustNewWithChars added in v0.0.4

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

MustNewWithChars must create a new mixer

func New

func New(salt string) *Mixer

New create a new mixer with case sensitive alphanumeric

func NewWithChars added in v0.0.4

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

NewWithChars create a new mixer

func Newt added in v0.0.4

func Newt(salt string, alphanumericType AlphanumericType) *Mixer

Newt create a new mixer with alphanumeric and AlphanumericType

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