Skip to content
Snippets Groups Projects
Commit a13e9c8f authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

replace repaeted hard-coded error string

parent a1fdef4f
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,9 @@ const ( ...@@ -20,6 +20,9 @@ const (
keyIterations = 8 keyIterations = 8
) )
const malFormedCiphertext = "malformed ciphertext"
const malFormedCiphertextFmt = malFormedCiphertext + ": %s"
func RandomBytes(size int) []byte { func RandomBytes(size int) []byte {
r := make([]byte, size) r := make([]byte, size)
if _, err := rand.Read(r); err != nil { if _, err := rand.Read(r); err != nil {
...@@ -76,7 +79,7 @@ func aesDecrypt(cipher, password string, keyLen int) (string, error) { ...@@ -76,7 +79,7 @@ func aesDecrypt(cipher, password string, keyLen int) (string, error) {
arr := strings.SplitN(cipher, "-", 2) arr := strings.SplitN(cipher, "-", 2)
salt, err := base64.StdEncoding.DecodeString(arr[0]) salt, err := base64.StdEncoding.DecodeString(arr[0])
if err != nil { if err != nil {
return "", fmt.Errorf("malformed ciphertext: %s", err) return "", fmt.Errorf(malFormedCiphertextFmt, err)
} }
key, _ := deriveKey(password, salt, keyLen) key, _ := deriveKey(password, salt, keyLen)
return AESDecrypt(arr[1], key) return AESDecrypt(arr[1], key)
...@@ -85,15 +88,15 @@ func aesDecrypt(cipher, password string, keyLen int) (string, error) { ...@@ -85,15 +88,15 @@ func aesDecrypt(cipher, password string, keyLen int) (string, error) {
func AESDecrypt(cipher string, key []byte) (string, error) { func AESDecrypt(cipher string, key []byte) (string, error) {
arr := strings.Split(cipher, "-") arr := strings.Split(cipher, "-")
if len(arr) != 2 { if len(arr) != 2 {
return "", fmt.Errorf("malformed ciphertext") return "", fmt.Errorf(malFormedCiphertext)
} }
nonce, err := base64.StdEncoding.DecodeString(arr[0]) nonce, err := base64.StdEncoding.DecodeString(arr[0])
if err != nil { if err != nil {
return "", fmt.Errorf("malformed ciphertext: %s", err) return "", fmt.Errorf(malFormedCiphertextFmt, err)
} }
data, err := base64.StdEncoding.DecodeString(arr[1]) data, err := base64.StdEncoding.DecodeString(arr[1])
if err != nil { if err != nil {
return "", fmt.Errorf("malformed ciphertext: %s", err) return "", fmt.Errorf(malFormedCiphertextFmt, err)
} }
return aesD(data, nonce, key) return aesD(data, nonce, key)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment