NewCryptoFromHexKey parses a 64-char hex string into a 32-byte AES-256 key and constructs a Crypto. The same key must be configured on both client and VPS server.
(hexKey string)
| 33 | // NewCryptoFromHexKey parses a 64-char hex string into a 32-byte AES-256 key |
| 34 | // and constructs a Crypto. The same key must be configured on both client and VPS server. |
| 35 | func NewCryptoFromHexKey(hexKey string) (*Crypto, error) { |
| 36 | key, err := hex.DecodeString(hexKey) |
| 37 | if err != nil { |
| 38 | return nil, fmt.Errorf("crypto: invalid hex key: %w", err) |
| 39 | } |
| 40 | if len(key) != 32 { |
| 41 | return nil, fmt.Errorf("crypto: key must be 32 bytes (AES-256), got %d", len(key)) |
| 42 | } |
| 43 | block, err := aes.NewCipher(key) |
| 44 | if err != nil { |
| 45 | return nil, fmt.Errorf("crypto: aes new cipher: %w", err) |
| 46 | } |
| 47 | gcm, err := cipher.NewGCM(block) |
| 48 | if err != nil { |
| 49 | return nil, fmt.Errorf("crypto: new gcm: %w", err) |
| 50 | } |
| 51 | return &Crypto{aead: gcm}, nil |
| 52 | } |
| 53 | |
| 54 | // Seal encrypts plaintext and returns nonce||ciphertext (tag appended by GCM). |
| 55 | func (c *Crypto) Seal(plaintext []byte) ([]byte, error) { |
no outgoing calls