هذا الالتزام موجود في:
Thomas Miceli
2024-10-24 23:23:00 +02:00
ملتزم من قبل GitHub
الأصل df226cbd99
التزام 2bf434f00e
20 ملفات معدلة مع 629 إضافات و16 حذوفات

46
internal/utils/aes.go Normal file
عرض الملف

@@ -0,0 +1,46 @@
package utils
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
)
func AESEncrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
ciphertext := make([]byte, aes.BlockSize+len(text))
iv := ciphertext[:aes.BlockSize]
if _, err = io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
stream := cipher.NewCFBEncrypter(block, iv)
stream.XORKeyStream(ciphertext[aes.BlockSize:], text)
return ciphertext, nil
}
func AESDecrypt(key, ciphertext []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(ciphertext) < aes.BlockSize {
return nil, fmt.Errorf("ciphertext too short")
}
iv := ciphertext[:aes.BlockSize]
ciphertext = ciphertext[aes.BlockSize:]
stream := cipher.NewCFBDecrypter(block, iv)
stream.XORKeyStream(ciphertext, ciphertext)
return ciphertext, nil
}

عرض الملف

@@ -6,7 +6,7 @@ import (
"os"
)
func ReadKey(filePath string) []byte {
func GenerateSecretKey(filePath string) []byte {
key, err := os.ReadFile(filePath)
if err == nil {
return key