33 lines
774 B
Go
33 lines
774 B
Go
package crypto
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
func Decrypt(hexedcipher string) (plaintext string, err error) {
|
|
aes, err := aes.NewCipher(someDumbKey)
|
|
if err != nil {
|
|
return "", fmt.Errorf("making new cipher: %w", err)
|
|
}
|
|
gcm, err := cipher.NewGCM(aes)
|
|
if err != nil {
|
|
return "", fmt.Errorf("making new gcm: %w", err)
|
|
}
|
|
|
|
nonceSize := gcm.NonceSize()
|
|
valueDecoded, err := hex.DecodeString(hexedcipher)
|
|
if err != nil {
|
|
return "", fmt.Errorf("decoding hexed value: %w", err)
|
|
}
|
|
nonce, cipherText := valueDecoded[:nonceSize], valueDecoded[nonceSize:]
|
|
plaintextRaw, err := gcm.Open(nil, []byte(nonce), []byte(cipherText), nil)
|
|
if err != nil {
|
|
return "", fmt.Errorf("opening: %w", err)
|
|
}
|
|
|
|
return string(plaintextRaw), nil
|
|
}
|