mixer

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 17, 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.
  • ID Number Padding Support number padding for ID number(default is padding 16 characters)

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


//EncodeString encode string use global default mixer
func EncodeString(password string, data string) string

//DecodeString encode global default mixer
func DecodeString(password string, data string) string

//EncodeNumber encode global default mixer
func EncodeNumber(password string, value int64) string

//EncodeNumberPadding  encode padding default number mixer
func EncodeNumberPadding(password string, value int64, paddingLen int) string

//DecodeNumber decode default number mixer
func DecodeNumber(password string, data string) int64

See Mixer Godoc

Usage

  • Number(ID) Example:

package main

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

func main() {

	//the source to be encrypted
	sources := []int64{
		123,
		123456,
		1234567890,
		999999999,
		time.Now().UnixNano(),
	}

	//password
	password := "1q2w3e"

	//foreach every source
	for _, source := range sources {

		//Encode source data
		encodeData := mixer.EncodeNumber(password, source)

		//Decode source data
		decodeData := mixer.DecodeNumber(password, encodeData)

		//Encode source data with padding 20
		encodePaddingData := mixer.EncodeNumberPadding(password, source, 20)

		//Decode source padding data
		decodePaddingData := mixer.DecodeNumber(password, encodeData)

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

}

Run output:

-------
source: 123
encode: 0kanys350waz3juw
decode: 123
encodePadding(20): 0ka8yss50waz3oulw3nj
decodePadding(20): 123
-------
-------
source: 123456
encode: skiwyapn30j5z0u7
decode: 123456
encodePadding(20): ski3yaan30j5zluw7pw0
decodePadding(20): 123456
-------
-------
source: 1234567890
encode: wkidy1pbens30au7
decode: 1234567890
encodePadding(20): wkijy10bens30zu57pda
decodePadding(20): 1234567890
-------
-------
source: 999999999
encode: aeeeeeewes0n53ee
decode: 999999999
encodePadding(20): aeezeejwes0n5we0eee3
decodePadding(20): 999999999
-------
-------
source: 1589704043390707700
encode: k1dbibbk7dbbdeudeb7
decode: 1589704043390707700
encodePadding(20): k1dbibbk7dbbdwudeb7e
decodePadding(20): 1589704043390707700
-------


  • String(Hash) Example:

package main

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

func main() {

	//the source to be encrypted
	sources := []string{
		"abc012345edf",
		"0123456789abcdefghijklmnopqrstuvwxyz",
		fmt.Sprintf("%x", md5.Sum([]byte("Hello Mixer"))),
	}

	//password
	password := "1q2w3e"

	//foreach every source
	for _, source := range sources {
		//Encode source data
		encodeData := mixer.EncodeString(password, source)

		//Decode source data
		decodeData := mixer.DecodeString(password, encodeData)

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

}


Run output:


-------
source: abc012345edf
encode: hruC87FfRv5N
decode: abc012345edf
-------
-------
source: 0123456789abcdefghijklmnopqrstuvwxyz
encode: 5B6oSK3Q1wfrvHNqjRp87LhlucJ0Xn2CFaIt
decode: 0123456789abcdefghijklmnopqrstuvwxyz
-------
-------
source: 51f5df9e0e802ed54465638ba31158c2
encode: 1rppCfufN1hFR8R7RvQNh5fRuF1uC7vR
decode: 51f5df9e0e802ed54465638ba31158c2
-------

Todo

Other languages Implementations
  • Java Implementation
  • PHP Implementation
  • Javascript Implementation

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 (
	//DefaultSalt salt for random seed: 202002022002
	DefaultSalt = "202002022002" //2020.02.02 20:02
)

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