Enchantrix/examples/main.go
google-labs-jules[bot] d66acec498 feat: Add checksums and asymmetrical sigils to Trix container
This commit enhances the Trix container with two new features for improved data integrity and flexibility:

1.  **Configurable Checksums:**
    - The `Trix` struct now has a `ChecksumAlgo` field to specify a hash algorithm.
    - If set, `Encode` computes a checksum of the payload and adds it to the header.
    - `Decode` verifies this checksum, returning an error if it doesn't match, ensuring data integrity during transit.

2.  **Asymmetrical Sigils:**
    - The `Sigils` field has been replaced with `InSigils` and `OutSigils` to support different transformation chains for packing and unpacking.
    - If `OutSigils` is not set, `Unpack` defaults to using the `InSigils` chain to maintain the previous symmetrical behavior.

These features make the `Trix` container a more robust and self-verifying format for internal data transfer.
2025-10-31 02:55:58 +00:00

89 lines
2.7 KiB
Go

package main
import (
"encoding/base64"
"fmt"
"log"
"time"
"github.com/Snider/Enchantrix/pkg/crypt"
"github.com/Snider/Enchantrix/pkg/crypt/std/chachapoly"
"github.com/Snider/Enchantrix/pkg/trix"
)
func main() {
// 1. Original plaintext and encryption key
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. Create a Trix container with the plaintext and attach sigils
trixContainer := &trix.Trix{
Header: map[string]interface{}{},
Payload: plaintext,
InSigils: []trix.Sigil{&trix.ReverseSigil{}},
}
// 3. Pack the Trix container to apply the sigil transformations
if err := trixContainer.Pack(); err != nil {
log.Fatalf("Failed to pack trix container: %v", err)
}
fmt.Printf("Packed (obfuscated) payload: %x\n", trixContainer.Payload)
// 4. Encrypt the packed payload
ciphertext, err := chachapoly.Encrypt(trixContainer.Payload, key)
if err != nil {
log.Fatalf("Failed to encrypt: %v", err)
}
trixContainer.Payload = ciphertext // Update the payload with the ciphertext
// 5. Add encryption metadata and checksum to the header
nonce := ciphertext[:24]
trixContainer.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.ChecksumAlgo = crypt.SHA256
// 6. Encode the .trix container into its binary format
magicNumber := "MyT1"
encodedTrix, err := trix.Encode(trixContainer, magicNumber)
if err != nil {
log.Fatalf("Failed to encode .trix container: %v", err)
}
fmt.Println("Successfully created .trix container.")
// --- DECODING ---
// 7. Decode the .trix container
decodedTrix, err := trix.Decode(encodedTrix, magicNumber)
if err != nil {
log.Fatalf("Failed to decode .trix container: %v", err)
}
// 8. Decrypt the payload
decryptedPayload, err := chachapoly.Decrypt(decodedTrix.Payload, key)
if err != nil {
log.Fatalf("Failed to decrypt: %v", err)
}
decodedTrix.Payload = decryptedPayload
// 9. Unpack the Trix container to reverse the sigil transformations
decodedTrix.InSigils = trixContainer.InSigils // Re-attach sigils
if err := decodedTrix.Unpack(); err != nil {
log.Fatalf("Failed to unpack trix container: %v", err)
}
fmt.Printf("Unpacked (original) payload: %s\n", decodedTrix.Payload)
// 10. Verify the result
if string(plaintext) == string(decodedTrix.Payload) {
fmt.Println("\nSuccess! The message was decrypted and unpacked correctly.")
} else {
fmt.Println("\nFailure! The final payload does not match the original.")
}
}