From f78900130bd22c66b3302bc0379f6aea57d676ad Mon Sep 17 00:00:00 2001 From: Evan Chen Date: Wed, 17 Nov 2021 09:50:25 +0800 Subject: [PATCH] feat: add default key --- xorencrypt.go | 8 ++++++++ xorencrypt_test.go | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/xorencrypt.go b/xorencrypt.go index a2fed56..fee216e 100644 --- a/xorencrypt.go +++ b/xorencrypt.go @@ -2,7 +2,12 @@ package xorencrypt import "encoding/base64" +var DEFAULT_KEY = "kumoly.io/lib/xorencrypt" + func Decrypt(msg, key string) (string, error) { + if key == "" { + key = DEFAULT_KEY + } deb64, err := base64.StdEncoding.DecodeString(msg) if err != nil { return "", err @@ -12,6 +17,9 @@ func Decrypt(msg, key string) (string, error) { } func Encrypt(msg, key string) string { + if key == "" { + key = DEFAULT_KEY + } enc := xor([]byte(msg), []byte(key)) return base64.StdEncoding.EncodeToString(enc) } diff --git a/xorencrypt_test.go b/xorencrypt_test.go index 49b6ae1..149fa8e 100644 --- a/xorencrypt_test.go +++ b/xorencrypt_test.go @@ -22,4 +22,8 @@ func TestEncrypt(t *testing.T) { decErr, _ := Decrypt(enc, "wrongKey") fmt.Printf("%s -> %s -> %s\n", msg, enc, decErr) + + encD := Encrypt(msg, "") + decD, _ := Decrypt(encD, "") + fmt.Printf("%s -> %s -> %s\n", msg, encD, decD) }