udemy-go-web-2/internal/cards/encryption/encryption.go

55 lines
1.1 KiB
Go

package encryption
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"io"
)
type Encryption struct {
Key []byte
}
func (e *Encryption) Encrypt(text string) (string, error) {
plaintext := []byte(text)
block, err := aes.NewCipher(e.Key)
if err != nil {
return "", err
}
cipherText := make([]byte, aes.BlockSize+len(plaintext))
iv := cipherText[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return "", err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(cipherText[aes.BlockSize:], plaintext)
return base64.URLEncoding.EncodeToString(cipherText), nil
}
func (e *Encryption) Decrypt(cryptoText string) (string, error) {
cipherText, _ := base64.URLEncoding.DecodeString(cryptoText)
block, err := aes.NewCipher(e.Key)
if err != nil {
return "", err
}
if len(cipherText) < aes.BlockSize {
return "", err
}
iv := cipherText[:aes.BlockSize]
cipherText = cipherText[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(cipherText, cipherText)
return string(cipherText), nil
}