feat: add default key

master
Evan Chen 2021-11-17 09:50:25 +08:00
parent 1fb4d8eda9
commit f78900130b
2 changed files with 12 additions and 0 deletions

View File

@ -2,7 +2,12 @@ package xorencrypt
import "encoding/base64" import "encoding/base64"
var DEFAULT_KEY = "kumoly.io/lib/xorencrypt"
func Decrypt(msg, key string) (string, error) { func Decrypt(msg, key string) (string, error) {
if key == "" {
key = DEFAULT_KEY
}
deb64, err := base64.StdEncoding.DecodeString(msg) deb64, err := base64.StdEncoding.DecodeString(msg)
if err != nil { if err != nil {
return "", err return "", err
@ -12,6 +17,9 @@ func Decrypt(msg, key string) (string, error) {
} }
func Encrypt(msg, key string) string { func Encrypt(msg, key string) string {
if key == "" {
key = DEFAULT_KEY
}
enc := xor([]byte(msg), []byte(key)) enc := xor([]byte(msg), []byte(key))
return base64.StdEncoding.EncodeToString(enc) return base64.StdEncoding.EncodeToString(enc)
} }

View File

@ -22,4 +22,8 @@ func TestEncrypt(t *testing.T) {
decErr, _ := Decrypt(enc, "wrongKey") decErr, _ := Decrypt(enc, "wrongKey")
fmt.Printf("%s -> %s -> %s\n", msg, enc, decErr) fmt.Printf("%s -> %s -> %s\n", msg, enc, decErr)
encD := Encrypt(msg, "")
decD, _ := Decrypt(encD, "")
fmt.Printf("%s -> %s -> %s\n", msg, encD, decD)
} }