Skip to content
Snippets Groups Projects
Commit 5681beee authored by Johan de Klerk's avatar Johan de Klerk
Browse files

Return string on decrypt

parent fff8c2f7
No related branches found
No related tags found
No related merge requests found
...@@ -45,22 +45,23 @@ func Encrypt(plaintext string, key string) (string, error) { ...@@ -45,22 +45,23 @@ func Encrypt(plaintext string, key string) (string, error) {
return string(gcm.Seal(nonce, nonce, []byte(plaintext), nil)), nil return string(gcm.Seal(nonce, nonce, []byte(plaintext), nil)), nil
} }
func Decrypt(ciphertext string, key string) ([]byte, error) { func Decrypt(ciphertext string, key string) (string, error) {
c, err := aes.NewCipher([]byte(key)) c, err := aes.NewCipher([]byte(key))
if err != nil { if err != nil {
return nil, err return "", err
} }
gcm, err := cipher.NewGCM(c) gcm, err := cipher.NewGCM(c)
if err != nil { if err != nil {
return nil, err return "", err
} }
nonceSize := gcm.NonceSize() nonceSize := gcm.NonceSize()
if len(ciphertext) < nonceSize { if len(ciphertext) < nonceSize {
return nil, errors.New("ciphertext too short") return "", errors.New("ciphertext too short")
} }
nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:] nonce, ciphertext := ciphertext[:nonceSize], ciphertext[nonceSize:]
return gcm.Open(nil, []byte(nonce), []byte(ciphertext), nil) value, err := gcm.Open(nil, []byte(nonce), []byte(ciphertext), nil)
return string(value), err
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment