CodeSampleX

Ejemplo

golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d: chacha20poly1305.New

Muestra verificada para golang golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d: chacha20poly1305.New. El contrato se ejecutó en go 1.26 · linux…

sha256:921da7c2106cc9048245f5ece500700d0c7a70d117cd9712e591513c8ffdcd87

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 go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

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-04

Caso

HOW
Objetivo
verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
Paquetes
Símbolos
  • golang.org/x/crypto/chacha20poly1305.New
Entorno
go 1.26.6
Creado
2026-09-04T13:24:36Z

Contrato

  1. chacha20poly1305.New requires a 32-byte key and rejects invalid key lengths
  2. chacha20poly1305.New returns cipher.AEAD with NonceSize 12 and Overhead 16
  3. chacha20poly1305.New creates cipher.AEAD that seals and opens plaintext with matching additional data
  4. chacha20poly1305.New creates cipher.AEAD that rejects altered ciphertext, incorrect nonce, or mismatched additional data

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 golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
Demonstrate these symbols/APIs:
  - golang.org/x/crypto/chacha20poly1305.New
Required runtime conditions:
  - ecosystem: golang
  - language: go
  - packageManager: go@1.26.6
  - runtime: go@1.26.6

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:d3b00252102def52258eb607db9e47182fcb3865ddec15466ae089bcf8c52500","contract":["chacha20poly1305.New requires a 32-byte key and rejects invalid key lengths","chacha20poly1305.New returns cipher.AEAD with NonceSize 12 and Overhead 16","chacha20poly1305.New creates cipher.AEAD that seals and opens plaintext with matching additional data","chacha20poly1305.New creates cipher.AEAD that rejects altered ciphertext, incorrect nonce, or mismatched additional data"],"goal":"verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d"],"schemaVersion":1,"symbols":["golang.org/x/crypto/chacha20poly1305.New"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d","symbols":["golang.org/x/crypto/chacha20poly1305.New"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d

require golang.org/x/sys v0.10.0 // indirect
go.sum
golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d h1:LiA25/KWKuXfIq5pMIBq1s5hz3HQxhJJSu/SUGlD+SM=
golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
main.go
package main

import (
	"crypto/rand"
	"encoding/hex"
	"fmt"
	"io"

	"golang.org/x/crypto/chacha20poly1305"
)

// Encrypt encrypts plaintext with ChaCha20-Poly1305 AEAD.
func Encrypt(key, nonce, plaintext, additionalData []byte) ([]byte, error) {
	aead, err := chacha20poly1305.New(key)
	if err != nil {
		return nil, fmt.Errorf("create cipher: %w", err)
	}
	return aead.Seal(nil, nonce, plaintext, additionalData), nil
}

// Decrypt decrypts ciphertext with ChaCha20-Poly1305 AEAD.
func Decrypt(key, nonce, ciphertext, additionalData []byte) ([]byte, error) {
	aead, err := chacha20poly1305.New(key)
	if err != nil {
		return nil, fmt.Errorf("create cipher: %w", err)
	}
	plaintext, err := aead.Open(nil, nonce, ciphertext, additionalData)
	if err != nil {
		return nil, fmt.Errorf("open ciphertext: %w", err)
	}
	return plaintext, nil
}

func main() {
	key := make([]byte, chacha20poly1305.KeySize)
	if _, err := io.ReadFull(rand.Reader, key); err != nil {
		panic(err)
	}

	nonce := make([]byte, chacha20poly1305.NonceSize)
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		panic(err)
	}

	plaintext := []byte("Hello, ChaCha20-Poly1305 authenticated encryption!")
	ad := []byte("metadata-header")

	ciphertext, err := Encrypt(key, nonce, plaintext, ad)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Ciphertext (%d bytes): %s\n", len(ciphertext), hex.EncodeToString(ciphertext))

	decrypted, err := Decrypt(key, nonce, ciphertext, ad)
	if err != nil {
		panic(err)
	}
	fmt.Printf("Decrypted: %s\n", string(decrypted))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d"
  ],
  "symbols": [
    "golang.org/x/crypto/chacha20poly1305.New"
  ],
  "runtimeConditions": {
    "ecosystem": "golang",
    "language": "go",
    "packageManager": "go@1.26.6",
    "runtime": "go@1.26.6"
  }
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

	"golang.org/x/crypto/chacha20poly1305"
)

func main() {
	// 1. chacha20poly1305.New requires a 32-byte key and rejects invalid key lengths
	{
		invalidLengths := []int{0, 16, 24, 31, 33, 64}
		for _, l := range invalidLengths {
			badKey := make([]byte, l)
			_, err := chacha20poly1305.New(badKey)
			if err == nil {
				fmt.Fprintf(os.Stderr, "FAIL: expected error for invalid key length %d, got nil\n", l)
				os.Exit(1)
			}
		}

		validKey := make([]byte, chacha20poly1305.KeySize) // 32 bytes
		aead, err := chacha20poly1305.New(validKey)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected valid 32-byte key to succeed, got %v\n", err)
			os.Exit(1)
		}
		if aead == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected non-nil cipher.AEAD\n")
			os.Exit(1)
		}
	}

	// 2. chacha20poly1305.New returns cipher.AEAD with NonceSize 12 and Overhead 16
	{
		key := []byte("01234567890123456789012345678901") // 32 bytes
		aead, err := chacha20poly1305.New(key)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unexpected error creating AEAD: %v\n", err)
			os.Exit(1)
		}

		if nonceSize := aead.NonceSize(); nonceSize != 12 {
			fmt.Fprintf(os.Stderr, "FAIL: expected NonceSize 12, got %d\n", nonceSize)
			os.Exit(1)
		}
		if overhead := aead.Overhead(); overhead != 16 {
			fmt.Fprintf(os.Stderr, "FAIL: expected Overhead 16, got %d\n", overhead)
			os.Exit(1)
		}
	}

	// 3. chacha20poly1305.New creates cipher.AEAD that seals and opens plaintext with matching additional data
	{
		key := []byte("01234567890123456789012345678901")
		nonce := []byte("nonce1234567") // 12 bytes
		plaintext := []byte("confidential message content")
		ad := []byte("header-metadata-ad")

		aead, err := chacha20poly1305.New(key)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unexpected error creating AEAD: %v\n", err)
			os.Exit(1)
		}

		ciphertext := aead.Seal(nil, nonce, plaintext, ad)
		expectedCiphertextLen := len(plaintext) + aead.Overhead()
		if len(ciphertext) != expectedCiphertextLen {
			fmt.Fprintf(os.Stderr, "FAIL: expected ciphertext length %d, got %d\n", expectedCiphertextLen, len(ciphertext))
			os.Exit(1)
		}

		opened, err := aead.Open(nil, nonce, ciphertext, ad)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected Open to succeed, got error: %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(opened, plaintext) {
			fmt.Fprintf(os.Stderr, "FAIL: decrypted plaintext does not match original\n")
			os.Exit(1)
		}

		// Also verify empty additional data works
		ciphertextNoAD := aead.Seal(nil, nonce, plaintext, nil)
		openedNoAD, err := aead.Open(nil, nonce, ciphertextNoAD, nil)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected Open with nil AD to succeed, got %v\n", err)
			os.Exit(1)
		}
		if !bytes.Equal(openedNoAD, plaintext) {
			fmt.Fprintf(os.Stderr, "FAIL: decrypted plaintext with nil AD does not match\n")
			os.Exit(1)
		}
	}

	// 4. chacha20poly1305.New creates cipher.AEAD that rejects altered ciphertext, incorrect nonce, or mismatched additional data
	{
		key := []byte("01234567890123456789012345678901")
		nonce := []byte("nonce1234567")
		plaintext := []byte("confidential message content")
		ad := []byte("header-metadata-ad")

		aead, err := chacha20poly1305.New(key)
		if err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: unexpected error: %v\n", err)
			os.Exit(1)
		}

		ciphertext := aead.Seal(nil, nonce, plaintext, ad)

		// Altered ciphertext byte
		corruptedCiphertext := append([]byte(nil), ciphertext...)
		corruptedCiphertext[0] ^= 0x01
		if _, err := aead.Open(nil, nonce, corruptedCiphertext, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error when decrypting corrupted ciphertext, got nil\n")
			os.Exit(1)
		}

		// Altered tag byte (last byte)
		corruptedTag := append([]byte(nil), ciphertext...)
		corruptedTag[len(corruptedTag)-1] ^= 0x01
		if _, err := aead.Open(nil, nonce, corruptedTag, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error when decrypting corrupted tag, got nil\n")
			os.Exit(1)
		}

		// Incorrect nonce
		wrongNonce := []byte("nonceWrong99")
		if _, err := aead.Open(nil, wrongNonce, ciphertext, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error with wrong nonce, got nil\n")
			os.Exit(1)
		}

		// Mismatched additional data
		wrongAD := []byte("different-metadata-ad")
		if _, err := aead.Open(nil, nonce, ciphertext, wrongAD); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error with wrong additional data, got nil\n")
			os.Exit(1)
		}

		// Truncated ciphertext (shorter than overhead)
		truncated := ciphertext[:aead.Overhead()-1]
		if _, err := aead.Open(nil, nonce, truncated, ad); err == nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected error with truncated ciphertext, got nil\n")
			os.Exit(1)
		}
	}

	fmt.Println("PASS: all chacha20poly1305 contract assertions passed")
}

Seeder de origen

anónimo