CodeSampleX

サンプル

golang.org/x/crypto v0.36.0: chacha20poly1305.New

検証済みサンプル — golang golang.org/x/crypto v0.36.0: chacha20poly1305.New. go 1.26 · linux alpine/x64 · docker で contract を実行し、成功しました.

sha256:be9c62fc98ec8827b29bbf03ffc155bfa442b4e6e119882508009a85d8e85a3a

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-26

ケース

HOW
ゴール
verify chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.36.0
パッケージ
シンボル
  • chacha20poly1305.New
作成日
2026-08-26T01:19:19Z

コントラクト

  1. chacha20poly1305.New creates an AEAD cipher with valid 32-byte key
  2. chacha20poly1305.New rejects invalid key length with an error
  3. aead.NonceSize returns standard 12-byte nonce size
  4. aead.Overhead returns 16-byte authentication tag overhead
  5. aead.Seal and aead.Open encrypt and decrypt plaintext with additional data
  6. aead.Open returns authentication failure error when ciphertext or tag is modified
  7. aead.Open returns authentication failure error when additional data does not match

ファイル

  • 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 chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.36.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.36.0
Demonstrate these symbols/APIs:
  - chacha20poly1305.New
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:a4e372d245942b4d44c53873b1660196163c6fb7a1e77b4657d632840a32d9e0","contract":["chacha20poly1305.New creates an AEAD cipher with valid 32-byte key","chacha20poly1305.New rejects invalid key length with an error","aead.NonceSize returns standard 12-byte nonce size","aead.Overhead returns 16-byte authentication tag overhead","aead.Seal and aead.Open encrypt and decrypt plaintext with additional data","aead.Open returns authentication failure error when ciphertext or tag is modified","aead.Open returns authentication failure error when additional data does not match"],"goal":"verify chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.36.0","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.36.0"],"schemaVersion":1,"symbols":["chacha20poly1305.New"]},"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.36.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.36.0","symbols":["chacha20poly1305.New"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/crypto v0.36.0

require golang.org/x/sys v0.31.0 // indirect
go.sum
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
main.go
package main

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

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

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

	aead, err := chacha20poly1305.New(key)
	if err != nil {
		log.Fatalf("failed to create chacha20poly1305 cipher: %v", err)
	}

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

	plaintext := []byte("hello chacha20poly1305 encryption")
	additionalData := []byte("header-metadata")

	ciphertext := aead.Seal(nil, nonce, plaintext, additionalData)
	fmt.Printf("Ciphertext length: %d bytes (plaintext: %d + overhead: %d)\n",
		len(ciphertext), len(plaintext), aead.Overhead())

	decrypted, err := aead.Open(nil, nonce, ciphertext, additionalData)
	if err != nil {
		log.Fatalf("failed to decrypt: %v", err)
	}
	fmt.Printf("Decrypted message: %s\n", string(decrypted))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify chacha20poly1305.New in pkg:golang/golang.org/x/crypto@v0.36.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.36.0"
  ],
  "symbols": [
    "chacha20poly1305.New"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"bytes"
	"crypto/rand"
	"fmt"
	"io"
	"os"

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

func main() {
	key := make([]byte, chacha20poly1305.KeySize)
	if _, err := io.ReadFull(rand.Reader, key); err != nil {
		fmt.Fprintf(os.Stderr, "failed to generate key: %v\n", err)
		os.Exit(1)
	}

	// 1. chacha20poly1305.New creates an AEAD cipher with valid 32-byte key
	aead, err := chacha20poly1305.New(key)
	if err != nil {
		fmt.Fprintf(os.Stderr, "chacha20poly1305.New failed with 32-byte key: %v\n", err)
		os.Exit(1)
	}

	// 2. chacha20poly1305.New rejects invalid key length with an error
	invalidKey := make([]byte, 16)
	if _, err := chacha20poly1305.New(invalidKey); err == nil {
		fmt.Fprintf(os.Stderr, "chacha20poly1305.New should fail with invalid key length\n")
		os.Exit(1)
	}

	// 3. aead.NonceSize returns standard 12-byte nonce size
	if nonceSize := aead.NonceSize(); nonceSize != chacha20poly1305.NonceSize {
		fmt.Fprintf(os.Stderr, "aead.NonceSize mismatch: got %d, want %d\n", nonceSize, chacha20poly1305.NonceSize)
		os.Exit(1)
	}

	// 4. aead.Overhead returns 16-byte authentication tag overhead
	if overhead := aead.Overhead(); overhead != chacha20poly1305.Overhead {
		fmt.Fprintf(os.Stderr, "aead.Overhead mismatch: got %d, want %d\n", overhead, chacha20poly1305.Overhead)
		os.Exit(1)
	}

	nonce := make([]byte, aead.NonceSize())
	if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
		fmt.Fprintf(os.Stderr, "failed to generate nonce: %v\n", err)
		os.Exit(1)
	}

	plaintext := []byte("authenticated secret message payload")
	additionalData := []byte("header: user-session-12345")

	// 5. aead.Seal and aead.Open encrypt and decrypt plaintext with additional data
	ciphertext := aead.Seal(nil, nonce, plaintext, additionalData)
	if len(ciphertext) != len(plaintext)+aead.Overhead() {
		fmt.Fprintf(os.Stderr, "ciphertext length mismatch: got %d, want %d\n", len(ciphertext), len(plaintext)+aead.Overhead())
		os.Exit(1)
	}

	decrypted, err := aead.Open(nil, nonce, ciphertext, additionalData)
	if err != nil {
		fmt.Fprintf(os.Stderr, "aead.Open failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decrypted, plaintext) {
		fmt.Fprintf(os.Stderr, "decrypted plaintext mismatch: got %s, want %s\n", string(decrypted), string(plaintext))
		os.Exit(1)
	}

	// 6. aead.Open returns authentication failure error when ciphertext or tag is modified
	tamperedCiphertext := make([]byte, len(ciphertext))
	copy(tamperedCiphertext, ciphertext)
	tamperedCiphertext[0] ^= 0xff
	if _, err := aead.Open(nil, nonce, tamperedCiphertext, additionalData); err == nil {
		fmt.Fprintf(os.Stderr, "aead.Open should fail when ciphertext is modified\n")
		os.Exit(1)
	}

	// 7. aead.Open returns authentication failure error when additional data does not match
	wrongAD := []byte("header: user-session-99999")
	if _, err := aead.Open(nil, nonce, ciphertext, wrongAD); err == nil {
		fmt.Fprintf(os.Stderr, "aead.Open should fail when additional data does not match\n")
		os.Exit(1)
	}

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

オリジンシーダー

匿名