CodeSampleX

Ejemplo

golang.org/x/crypto v0.55.0: secretbox.Seal

Muestra verificada para golang golang.org/x/crypto v0.55.0: secretbox.Seal. El contrato se ejecutó en go 1.26 · linux debian/x64 · docker y pasó…

sha256:5db4066007aa4ddebac1dfb02fd12061124be31864b79d05db29c91c0ce6152a

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-02

Caso

HOW
Objetivo
verify secretbox.Seal in pkg:golang/golang.org/x/crypto@v0.55.0
Paquetes
Símbolos
  • secretbox.Seal
Creado
2026-09-02T17:13:35Z

Contrato

  1. secretbox.Seal appends ciphertext of length len(message) + secretbox.Overhead to out slice
  2. secretbox.Open authenticates and decrypts ciphertext sealed with secretbox.Seal using matching key and nonce
  3. secretbox.Open returns false when ciphertext payload is modified
  4. secretbox.Open returns false when decrypted with a different secret key
  5. secretbox.Open returns false when decrypted with a different nonce
  6. secretbox.Open returns false when ciphertext length is less than secretbox.Overhead
  7. secretbox.Seal encrypts empty message producing an authentication tag of length secretbox.Overhead

Archivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify secretbox.Seal in pkg:golang/golang.org/x/crypto@v0.55.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.55.0
Demonstrate these symbols/APIs:
  - secretbox.Seal

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:0a29a0a8ea748a08a2b947c320f9928aea502a96439332977bed2aa02c700360","contract":["secretbox.Seal appends ciphertext of length len(message) + secretbox.Overhead to out slice","secretbox.Open authenticates and decrypts ciphertext sealed with secretbox.Seal using matching key and nonce","secretbox.Open returns false when ciphertext payload is modified","secretbox.Open returns false when decrypted with a different secret key","secretbox.Open returns false when decrypted with a different nonce","secretbox.Open returns false when ciphertext length is less than secretbox.Overhead","secretbox.Seal encrypts empty message producing an authentication tag of length secretbox.Overhead"],"goal":"verify secretbox.Seal in pkg:golang/golang.org/x/crypto@v0.55.0","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.55.0"],"schemaVersion":1,"symbols":["secretbox.Seal"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/crypto@v0.55.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.55.0","symbols":["secretbox.Seal"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/crypto v0.55.0

require golang.org/x/sys v0.47.0 // indirect
go.sum
golang.org/x/crypto v0.55.0 h1:+KWHjbgOaAQ66dh/YlkZKHlz9ZUlq61AFirAR9ntP8M=
golang.org/x/crypto v0.55.0/go.mod h1:uq0V9dE/fzQuJtbnL+2EhWOE63vo164FY8xqEnV9xis=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
main.go
package main

import (
	"crypto/rand"
	"fmt"
	"io"
	"log"

	"golang.org/x/crypto/nacl/secretbox"
)

func main() {
	var secretKey [32]byte
	if _, err := io.ReadFull(rand.Reader, secretKey[:]); err != nil {
		log.Fatalf("failed to generate key: %v", err)
	}

	var nonce [24]byte
	if _, err := io.ReadFull(rand.Reader, nonce[:]); err != nil {
		log.Fatalf("failed to generate nonce: %v", err)
	}

	message := []byte("confidential payload")

	// Seal appends the encrypted and authenticated message to out
	encrypted := secretbox.Seal(nil, message, &nonce, &secretKey)
	fmt.Printf("Encrypted message length: %d (original %d + overhead %d)\n",
		len(encrypted), len(message), secretbox.Overhead)

	// Decrypt using Open
	decrypted, ok := secretbox.Open(nil, encrypted, &nonce, &secretKey)
	if !ok {
		log.Fatalf("failed to decrypt message")
	}

	fmt.Printf("Decrypted message: %s\n", string(decrypted))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify secretbox.Seal in pkg:golang/golang.org/x/crypto@v0.55.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.55.0"
  ],
  "symbols": [
    "secretbox.Seal"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"golang.org/x/crypto/nacl/secretbox"
)

func main() {
	var key [32]byte
	var nonce [24]byte
	for i := range key {
		key[i] = byte(i + 1)
	}
	for i := range nonce {
		nonce[i] = byte(i + 10)
	}

	message := []byte("hello, secretbox!")
	outPrefix := []byte("prefix:")

	// 1. secretbox.Seal appends ciphertext of length len(message) + secretbox.Overhead to out slice
	sealed := secretbox.Seal(outPrefix, message, &nonce, &key)
	expectedLen := len(outPrefix) + len(message) + secretbox.Overhead
	if len(sealed) != expectedLen {
		fmt.Fprintf(os.Stderr, "Seal output length mismatch: got %d, want %d\n", len(sealed), expectedLen)
		os.Exit(1)
	}
	if !bytes.Equal(sealed[:len(outPrefix)], outPrefix) {
		fmt.Fprintf(os.Stderr, "Seal corrupted prefix slice\n")
		os.Exit(1)
	}

	// 2. secretbox.Open authenticates and decrypts ciphertext sealed with secretbox.Seal using matching key and nonce
	ciphertext := sealed[len(outPrefix):]
	decrypted, ok := secretbox.Open(nil, ciphertext, &nonce, &key)
	if !ok {
		fmt.Fprintf(os.Stderr, "Open failed to decrypt valid ciphertext\n")
		os.Exit(1)
	}
	if !bytes.Equal(decrypted, message) {
		fmt.Fprintf(os.Stderr, "Decrypted message mismatch: got %q, want %q\n", decrypted, message)
		os.Exit(1)
	}

	// 3. secretbox.Open returns false when ciphertext payload is modified
	tampered := make([]byte, len(ciphertext))
	copy(tampered, ciphertext)
	tampered[len(tampered)-1] ^= 0x01
	if _, ok := secretbox.Open(nil, tampered, &nonce, &key); ok {
		fmt.Fprintf(os.Stderr, "Open unexpectedly succeeded on tampered ciphertext\n")
		os.Exit(1)
	}

	// 4. secretbox.Open returns false when decrypted with a different secret key
	var wrongKey [32]byte
	copy(wrongKey[:], key[:])
	wrongKey[0] ^= 0xff
	if _, ok := secretbox.Open(nil, ciphertext, &nonce, &wrongKey); ok {
		fmt.Fprintf(os.Stderr, "Open unexpectedly succeeded with wrong key\n")
		os.Exit(1)
	}

	// 5. secretbox.Open returns false when decrypted with a different nonce
	var wrongNonce [24]byte
	copy(wrongNonce[:], nonce[:])
	wrongNonce[0] ^= 0xff
	if _, ok := secretbox.Open(nil, ciphertext, &wrongNonce, &key); ok {
		fmt.Fprintf(os.Stderr, "Open unexpectedly succeeded with wrong nonce\n")
		os.Exit(1)
	}

	// 6. secretbox.Open returns false when ciphertext length is less than secretbox.Overhead
	shortCiphertext := make([]byte, secretbox.Overhead-1)
	if _, ok := secretbox.Open(nil, shortCiphertext, &nonce, &key); ok {
		fmt.Fprintf(os.Stderr, "Open unexpectedly succeeded on ciphertext shorter than Overhead\n")
		os.Exit(1)
	}

	// 7. secretbox.Seal encrypts empty message producing an authentication tag of length secretbox.Overhead
	emptySealed := secretbox.Seal(nil, []byte{}, &nonce, &key)
	if len(emptySealed) != secretbox.Overhead {
		fmt.Fprintf(os.Stderr, "Seal empty message length mismatch: got %d, want %d\n", len(emptySealed), secretbox.Overhead)
		os.Exit(1)
	}
	decryptedEmpty, ok := secretbox.Open(nil, emptySealed, &nonce, &key)
	if !ok || len(decryptedEmpty) != 0 {
		fmt.Fprintf(os.Stderr, "Open failed to decrypt empty message\n")
		os.Exit(1)
	}

	fmt.Println("All contract assertions passed.")
}

Seeder de origen

anónimo