ente/cli/pkg/model/enc_string.go
2024-03-01 12:39:01 +05:30

32 lines
721 B
Go

package model
import (
"github.com/ente-io/cli/internal/crypto"
"github.com/ente-io/cli/utils/encoding"
"log"
)
type EncString struct {
CipherText string `json:"cipherText"`
Nonce string `json:"nonce"`
}
func MakeEncString(plainTextBytes []byte, key []byte) *EncString {
cipher, nonce, err := crypto.EncryptChaCha20poly1305(plainTextBytes, key)
if err != nil {
log.Fatalf("failed to encrypt %s", err)
}
return &EncString{
CipherText: encoding.EncodeBase64(cipher),
Nonce: encoding.EncodeBase64(nonce),
}
}
func (e *EncString) MustDecrypt(key []byte) []byte {
_, plainBytes, err := crypto.DecryptChaChaBase64(e.CipherText, key, e.Nonce)
if err != nil {
panic(err)
}
return plainBytes
}