mixer

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: May 16, 2020 License: MIT Imports: 4 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
)

Variables

View Source
var (

	//StdMixer std mixer is alias AlphanumericCaseMixer
	StdMixer = AlphanumericCaseMixer

	//AlphanumericCaseMixer the alphanumeric include upper and lower:`0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
	AlphanumericCaseMixer = NewWith(defaultSalt, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

	//AlphanumericUpperMixer the alphanumeric include upper:`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ`
	AlphanumericUpperMixer = NewWith(defaultSalt, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")

	//AlphanumericLowerMixer the alphanumeric include lower:`0123456789abcdefghijklmnopqrstuvwxyz`
	AlphanumericLowerMixer = NewWith(defaultSalt, "0123456789abcdefghijklmnopqrstuvwxyz")

	//AlphabetCaseMixer the upper alphabet:`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`
	AlphabetCaseMixer = NewWith(defaultSalt, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

	//AlphabetUpperMixer the upper alphabet:`ABCDEFGHIJKLMNOPQRSTUVWXYZ`
	AlphabetUpperMixer = NewWith(defaultSalt, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")

	//AlphabetLowerMixer the lower alphabet:`abcdefghijklmnopqrstuvwxyz`
	AlphabetLowerMixer = NewWith(defaultSalt, "abcdefghijklmnopqrstuvwxyz")

	//HexCaseMixer the hex alphabet and numeric:`0123456789abcdefABCDEF`
	HexCaseMixer = NewWith(defaultSalt, "0123456789abcdefABCDEF")

	//HexUpperMixer the hex alphabet and numeric:`0123456789abcdef`
	HexUpperMixer = NewWith(defaultSalt, "0123456789ABCDEF")

	//HexLowerMixer the hex alphabet and numeric:`0123456789abcdef`
	HexLowerMixer = NewWith(defaultSalt, "0123456789abcdef")

	//NumericMixer the numeric:`0123456789abcdef`
	NumericMixer = NewWith(defaultSalt, "0123456789")

	//SymbolsMixer the symbols chars
	SymbolsMixer = NewWith(defaultSalt, "0123456789ABCabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_+-=.")
)

Functions

func DecodeNumber added in v0.0.5

func DecodeNumber(password string, data string) int64

DecodeNumber decode default number mixer

func DecodeString added in v0.0.5

func DecodeString(password string, data string) string

DecodeString encode global default mixer

func EncodeNumber added in v0.0.5

func EncodeNumber(password string, value int64) string

EncodeNumber encode global default mixer

func EncodeNumberPadding added in v0.0.5

func EncodeNumberPadding(password string, value int64, paddingLen int) string

EncodeNumberPadding encode padding default number mixer

func EncodeString added in v0.0.5

func EncodeString(password string, data string) string

EncodeString encode string use global default mixer

Types

type Config added in v0.0.5

type Config struct {
	Salt     string //salt for random seed
	MixChars string //chars for mix
}

Config configuration for new mixer

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 New

func New() *Mixer

New create a new mixer with case sensitive alphanumeric

func NewWith added in v0.0.5

func NewWith(salt string, mixChars string) *Mixer

NewWith create a new mixer with args

func NewWithConfig added in v0.0.5

func NewWithConfig(cfg Config) (*Mixer, error)

NewWithConfig create a new mixer

func (Mixer) Config added in v0.0.5

func (m Mixer) Config() Config

Config return current Config

func (Mixer) Decode

func (m Mixer) Decode(password string, data []rune) []rune

Decode decode char array

func (Mixer) DecodeNumber added in v0.0.5

func (m Mixer) DecodeNumber(password string, data string) int64

DecodeNumber decode string

func (Mixer) DecodeString

func (m Mixer) DecodeString(password, data string) string

DecodeString decode string

func (Mixer) Encode

func (m Mixer) Encode(password string, data []rune) []rune

Encode encode char array

func (Mixer) EncodeNumber added in v0.0.5

func (m Mixer) EncodeNumber(password string, value int64) string

EncodeNumber encode string

func (Mixer) EncodeNumberPadding added in v0.0.5

func (m Mixer) EncodeNumberPadding(password string, value int64, paddingLen int) string

EncodeNumberPadding encode string

func (Mixer) EncodeString

func (m Mixer) EncodeString(password, data string) string

EncodeString encode string

func (*Mixer) WithSalt added in v0.0.5

func (m *Mixer) WithSalt(salt string) *Mixer

WithSalt create copy Mixer with new salt

Directories

Path Synopsis
_examples
number command
string command

Jump to

Keyboard shortcuts

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