mixer

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 17, 2020 License: MIT Imports: 4 Imported by: 4

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

See Mixer Godoc

Usage

  • ID(uint64) Example:

package main

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

func main() {

	//the source to be encrypted
	sources := []uint64{
		0,
		123,
		123456,
		1234567890,
		999999999,
		9223372036854775808,
		18446744073709551615,
	}

	//password
	password := "1q2w3e"

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

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

		//Decode source data
		decodeData, err := mixer.DecodeID(password, encodeData)
		if err != nil {
			panic(err)
		}

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

		//Decode source padding data
		decodePaddingData, err := mixer.DecodeID(password, encodeData)
		if err != nil {
			panic(err)
		}

		//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: 0
encode: 0BAN5S350WAZ3JQW
decode: 0
encodePadding(20): 0BA85SS50WAZ3OQLW3NJ
decodePadding(20): 0
-------
-------
source: 123
encode: S0QWFABN30J5Z0QH
decode: 123
encodePadding(20): S0Q3FAAN30J5ZLQWHBW0
decodePadding(20): 123
-------
-------
source: 123456
encode: B0Q8FC4OQ3NASWQH
decode: 123456
encodePadding(20): B0Q0FC5OQ3NASJQ0H48W
decodePadding(20): 123456
-------
-------
source: 1234567890
encode: K0Q8FC4OQSL33ZQH
decode: 1234567890
encodePadding(20): K0QAFCWOQSL333QBH48Z
decodePadding(20): 1234567890
-------
-------
source: 999999999
encode: 77JLF0SFNS9JBZNZ
decode: 999999999
encodePadding(20): 77J3F0AFNS9JBNNWZSLZ
decodePadding(20): 999999999
-------
-------
source: 9223372036854775808
encode: 059Y4Z77Q0052GNN5W0HFQWZFHSHM50
decode: 9223372036854775808
encodePadding(20): 059Y4Z77Q0052GNN5W0HFQWZFHSHM50
decodePadding(20): 9223372036854775808
-------
-------
source: 18446744073709551615
encode: YL5KS2O7QL89GCQQW48H9QWZFCWZMW7H
decode: 18446744073709551615
encodePadding(20): YL5KS2O7QL89GCQQW48H9QWZFCWZMW7H
decodePadding(20): 18446744073709551615
-------

  • String 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
	//DefaultPaddingLength default padding length
	DefaultPaddingLength = 16
)

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 DecodeBase32 added in v0.0.8

func DecodeBase32(password string, data string) (string, error)

DecodeBase32 decode default number mixer

func DecodeID added in v0.0.8

func DecodeID(password string, data string) (uint64, error)

DecodeID decode default number mixer

func DecodeNumber added in v0.0.5

func DecodeNumber(password string, data string) (int64, error)

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 EncodeBase32 added in v0.0.8

func EncodeBase32(password string, value string) string

EncodeBase32 encode global default mixer

func EncodeBase32Padding added in v0.0.8

func EncodeBase32Padding(password string, value string, paddingLen int) string

EncodeBase32Padding encode padding default number mixer

func EncodeID added in v0.0.8

func EncodeID(password string, value uint64) string

EncodeID encode global default mixer

func EncodeIDPadding added in v0.0.8

func EncodeIDPadding(password string, value uint64, paddingLen int) string

EncodeIDPadding encode padding default number 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) DecodeBase32 added in v0.0.8

func (m Mixer) DecodeBase32(password string, data string) (string, error)

DecodeBase32 decode base32 data

func (Mixer) DecodeID added in v0.0.8

func (m Mixer) DecodeID(password string, data string) (uint64, error)

DecodeID decode uint64 ID

func (Mixer) DecodeNumber added in v0.0.5

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

DecodeNumber decode int64 number

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) EncodeBase32 added in v0.0.8

func (m Mixer) EncodeBase32(password string, value string) string

EncodeBase32 encode base32 data

func (Mixer) EncodeBase32Padding added in v0.0.8

func (m Mixer) EncodeBase32Padding(password string, value string, paddingLen int) string

EncodeBase32Padding encode base32 data

func (Mixer) EncodeID added in v0.0.8

func (m Mixer) EncodeID(password string, id uint64) string

EncodeID encode uint64 ID

func (Mixer) EncodeIDPadding added in v0.0.8

func (m Mixer) EncodeIDPadding(password string, id uint64, paddingLen int) string

EncodeIDPadding encode uint64 ID

func (Mixer) EncodeNumber added in v0.0.5

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

EncodeNumber encode int64 number

func (Mixer) EncodeNumberPadding added in v0.0.5

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

EncodeNumberPadding encode int64 number

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
id

Jump to

Keyboard shortcuts

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