add encryption and get page

This commit is contained in:
Aleksandr Trushkin
2024-01-29 00:09:30 +03:00
parent cc69847c4b
commit 5a49956661
7 changed files with 432 additions and 33 deletions

32
internal/crypto/cipher.go Normal file
View File

@ -0,0 +1,32 @@
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/hex"
"fmt"
)
var (
someDumbKey = []byte("9530e001b619e8e98a889055f06821bb")
)
func Encrypt(plaintext string) (hexed 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)
}
nonce := make([]byte, gcm.NonceSize())
_, err = rand.Read(nonce)
if err != nil {
return "", fmt.Errorf("generating nonce: %w", err)
}
outvalue := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
return hex.EncodeToString(outvalue), nil
}