Commit 92e37c6c authored by Timothy Richard's avatar Timothy Richard
Browse files

Merge branch 'ME12-NewEncryption' into 'master'

add new function of encryption

See merge request cuxs/common!1
parents 678767d8 af819e17
......@@ -23,6 +23,7 @@ const (
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
numeric = "0123456789"
hex = 0xFFFFFFFFFFFFFFF
)
// RandomStr return random string with defined length.
......@@ -115,6 +116,38 @@ func Decrypt(v interface{}) (id int64, err error) {
return
}
// DynamicEncrypt perform simple encryption values with optional number of bitshift.
func DynamicEncrypt(n interface{}, bitNum ...int) string {
num := ToInt(n)
bit := 16
if len(bitNum) > 0 {
bit = bitNum[0]
}
return ToString((hex & num) << bit)
}
// DynamicDecrypt return real values of encripted values by DynamicEncrypt function.
func DynamicDecrypt(n interface{}, bitNum ...int) (id int64, err error) {
num := ToInt(n)
bit := 16
if len(bitNum) > 0 {
bit = bitNum[0]
}
decID := ToString((hex & num) >> bit)
if num := ToInt(decID); num != 0 {
id = int64(num)
return id, nil
}
err = &DecryptionError{
Values: n,
}
return
}
// DecryptionError error type caused by decription failure.
type DecryptionError struct {
Message string
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment