From a13e9c8fbc882e5d23b2e5ea57d1c9461b5e2d3f Mon Sep 17 00:00:00 2001 From: zachmann <gabriel.zachmann@kit.edu> Date: Tue, 1 Jun 2021 13:23:49 +0200 Subject: [PATCH] replace repaeted hard-coded error string --- shared/utils/cryptUtils/cryptUtils.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/shared/utils/cryptUtils/cryptUtils.go b/shared/utils/cryptUtils/cryptUtils.go index f63b5ff8..604e2984 100644 --- a/shared/utils/cryptUtils/cryptUtils.go +++ b/shared/utils/cryptUtils/cryptUtils.go @@ -20,6 +20,9 @@ const ( keyIterations = 8 ) +const malFormedCiphertext = "malformed ciphertext" +const malFormedCiphertextFmt = malFormedCiphertext + ": %s" + func RandomBytes(size int) []byte { r := make([]byte, size) if _, err := rand.Read(r); err != nil { @@ -76,7 +79,7 @@ func aesDecrypt(cipher, password string, keyLen int) (string, error) { arr := strings.SplitN(cipher, "-", 2) salt, err := base64.StdEncoding.DecodeString(arr[0]) if err != nil { - return "", fmt.Errorf("malformed ciphertext: %s", err) + return "", fmt.Errorf(malFormedCiphertextFmt, err) } key, _ := deriveKey(password, salt, keyLen) return AESDecrypt(arr[1], key) @@ -85,15 +88,15 @@ func aesDecrypt(cipher, password string, keyLen int) (string, error) { func AESDecrypt(cipher string, key []byte) (string, error) { arr := strings.Split(cipher, "-") if len(arr) != 2 { - return "", fmt.Errorf("malformed ciphertext") + return "", fmt.Errorf(malFormedCiphertext) } nonce, err := base64.StdEncoding.DecodeString(arr[0]) if err != nil { - return "", fmt.Errorf("malformed ciphertext: %s", err) + return "", fmt.Errorf(malFormedCiphertextFmt, err) } data, err := base64.StdEncoding.DecodeString(arr[1]) if err != nil { - return "", fmt.Errorf("malformed ciphertext: %s", err) + return "", fmt.Errorf(malFormedCiphertextFmt, err) } return aesD(data, nonce, key) } -- GitLab