Enchantrix/examples/pgp_encrypt_decrypt/main.go
Claude c8531fa66b
Some checks failed
Go / build (push) Failing after 27s
Publish Docs / build (push) Failing after 1m0s
chore: migrate module path from github.com to forge.lthn.ai
Move module declaration and all internal imports from
github.com/Snider/Enchantrix to forge.lthn.ai/Snider/Enchantrix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 21:34:26 +00:00

52 lines
1.6 KiB
Go

// Example: PGP encryption and decryption
//
// This example demonstrates OpenPGP key generation, asymmetric encryption,
// and decryption. PGP provides end-to-end encryption with ASCII-armored
// output suitable for email and text-based transport.
//
// Run with: go run examples/pgp_encrypt_decrypt/main.go
package main
import (
"fmt"
"log"
"forge.lthn.ai/Snider/Enchantrix/pkg/crypt"
)
func main() {
fmt.Println("--- PGP Encryption & Decryption Demo ---")
cryptService := crypt.NewService()
// 1. Generate PGP key pair
fmt.Println("Generating PGP key pair...")
publicKey, privateKey, err := cryptService.GeneratePGPKeyPair("test", "test@example.com", "test key")
if err != nil {
log.Fatalf("Failed to generate PGP key pair: %v", err)
}
fmt.Println("Key pair generated successfully.")
// 2. Encrypt a message
message := []byte("This is a secret message for PGP.")
fmt.Printf("\nOriginal message: %s\n", message)
ciphertext, err := cryptService.EncryptPGP(publicKey, message)
if err != nil {
log.Fatalf("Failed to encrypt with PGP: %v", err)
}
fmt.Printf("Encrypted ciphertext (armored):\n%s\n", ciphertext)
// 3. Decrypt the message
decrypted, err := cryptService.DecryptPGP(privateKey, ciphertext)
if err != nil {
log.Fatalf("Failed to decrypt with PGP: %v", err)
}
fmt.Printf("Decrypted message: %s\n", decrypted)
// 4. Verify
if string(message) == string(decrypted) {
fmt.Println("\nSuccess! PGP decrypted message matches the original.")
} else {
fmt.Println("\nFailure! PGP decrypted message does not match the original.")
}
fmt.Println()
}