CodeSampleX

示例

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

已验证示例 — golang golang.org/x/crypto v0.11.1-0.20230711161743-2e82bdd1719d: chacha20poly1305.New. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:921da7c2106cc9048245f5ece500700d0c7a70d117cd9712e591513c8ffdcd87

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify golang.org/x/crypto/chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.11.1-0.20230711161743-2e82bdd1719d
包
符号
  • golang.org/x/crypto/chacha20poly1305.New
环境
go 1.26.6
创建时间
2026-09-04T13:24:36Z

契约

  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

文件

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

下载源代码构件 (tar.gz)

源代码

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")
}

原始种子者

匿名