Enchantrix/examples/main.go
google-labs-jules[bot] d5ae9a44e1 feat: Implement Good, Bad, and Ugly testing strategy
This commit refactors the test suites for the `crypt` and `trix` packages to follow the Good, Bad, and Ugly testing strategy.

- `_Good` tests cover the ideal "happy path" scenarios.
- `_Bad` tests cover expected failure scenarios with well-formed but invalid inputs.
- `_Ugly` tests cover malicious or malformed inputs designed to cause crashes or panics.

This new testing structure makes the test suite more organized, comprehensive, and robust.

Additionally, this commit includes a bug fix for the `Luhn` function, which now correctly handles empty and single-digit strings.
2025-10-31 02:03:33 +00:00

86 lines
2.5 KiB
Go

package main
import (
"encoding/base64"
"fmt"
"log"
"time"
"github.com/Snider/Enchantrix/pkg/crypt/std/chachapoly"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
// 1. Original plaintext
plaintext := []byte("This is a super secret message!")
key := make([]byte, 32) // In a real application, use a secure key
for i := range key {
key[i] = 1
}
// 2. Encrypt the data using the chachapoly package
// The ciphertext from chachapoly includes the nonce.
ciphertext, err := chachapoly.Encrypt(plaintext, key)
if err != nil {
log.Fatalf("Failed to encrypt: %v", err)
}
// For the .trix header, we need to separate the nonce from the ciphertext.
// chacha20poly1305.NewX nonce size is 24 bytes.
nonce := ciphertext[:24]
actualCiphertext := ciphertext[24:]
// 3. Create a .trix container for the encrypted data
header := map[string]interface{}{
"content_type": "application/octet-stream",
"encryption_algorithm": "chacha20poly1305",
"nonce": base64.StdEncoding.EncodeToString(nonce),
"created_at": time.Now().UTC().Format(time.RFC3339),
}
trixContainer := &trix.Trix{
Header: header,
Payload: actualCiphertext,
}
// 4. Encode the .trix container into its binary format
magicNumber := "MyT1" // My Trix 1
encodedTrix, err := trix.Encode(trixContainer, magicNumber)
if err != nil {
log.Fatalf("Failed to encode .trix container: %v", err)
}
fmt.Println("Successfully created .trix container.")
// 5. Decode the .trix container to retrieve the encrypted data
decodedTrix, err := trix.Decode(encodedTrix, magicNumber)
if err != nil {
log.Fatalf("Failed to decode .trix container: %v", err)
}
// 6. Reassemble the ciphertext (nonce + payload) and decrypt
retrievedNonceStr, ok := decodedTrix.Header["nonce"].(string)
if !ok {
log.Fatalf("Nonce not found or not a string in header")
}
retrievedNonce, err := base64.StdEncoding.DecodeString(retrievedNonceStr)
if err != nil {
log.Fatalf("Failed to decode nonce: %v", err)
}
retrievedCiphertext := append(retrievedNonce, decodedTrix.Payload...)
decrypted, err := chachapoly.Decrypt(retrievedCiphertext, key)
if err != nil {
log.Fatalf("Failed to decrypt: %v", err)
}
// 7. Verify the result
fmt.Printf("Original plaintext: %s\n", plaintext)
fmt.Printf("Decrypted plaintext: %s\n", decrypted)
if string(plaintext) == string(decrypted) {
fmt.Println("\nSuccess! The message was decrypted correctly.")
} else {
fmt.Println("\nFailure! The decrypted message does not match the original.")
}
}