master
Evan Chen 2021-12-24 12:39:02 +08:00
parent ab1721c04e
commit 6ccf516c58
1 changed files with 33 additions and 0 deletions

33
util/encode.go Normal file
View File

@ -0,0 +1,33 @@
package util
import "encoding/base64"
const DEFAULT_KEY = "kumoly.io/kumoly/app/util"
func Decrypt(msg, key string) (string, error) {
if key == "" {
key = DEFAULT_KEY
}
deb64, err := base64.URLEncoding.DecodeString(msg)
if err != nil {
return "", err
}
dec := xor(deb64, []byte(key))
return string(dec), nil
}
func Encrypt(msg, key string) string {
if key == "" {
key = DEFAULT_KEY
}
enc := xor([]byte(msg), []byte(key))
return base64.URLEncoding.EncodeToString(enc)
}
func xor(msg, key []byte) []byte {
ret := make([]byte, len(msg))
for i := 0; i < len(msg); i++ {
ret[i] = msg[i] ^ key[i%len(key)]
}
return ret
}