CodeSampleX

示例

github.com/flynn/noise v1.0.0: CipherChaChaPoly

已验证示例 — golang github.com/flynn/noise v1.0.0: CipherChaChaPoly. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: noise.CipherChaChaPoly identifies…

sha256:a31b53f8dda6e737b69d00ff939016392b2b2db672bc7b8e9672cae7f7caf036

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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 github.com/flynn/noise.CipherChaChaPoly in pkg:golang/github.com/flynn/noise@v1.0.0
包
符号
  • github.com/flynn/noise.CipherChaChaPoly
创建时间
2026-09-04T03:02:43Z

契约

  1. noise.CipherChaChaPoly identifies itself as ChaChaPoly cipher suite component
  2. noise.CipherChaChaPoly.Cipher initializes an AEAD cipher that encrypts plaintext with an authentication tag
  3. noise.Cipher.Decrypt authenticates and decrypts valid ciphertext back to original plaintext
  4. noise.Cipher.Decrypt rejects tampered ciphertext or mismatched authenticated data with an authentication error
  5. noise.CipherChaChaPoly integrates into Noise CipherSuite for secure handshake and transport message encryption

文件

  • 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 github.com/flynn/noise.CipherChaChaPoly in pkg:golang/github.com/flynn/noise@v1.0.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/flynn/noise@v1.0.0
Demonstrate these symbols/APIs:
  - github.com/flynn/noise.CipherChaChaPoly

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:5c6447d94714b5531bc49f783aeba895150259e271ebf6bc27494bcb47725be5","contract":["noise.CipherChaChaPoly identifies itself as ChaChaPoly cipher suite component","noise.CipherChaChaPoly.Cipher initializes an AEAD cipher that encrypts plaintext with an authentication tag","noise.Cipher.Decrypt authenticates and decrypts valid ciphertext back to original plaintext","noise.Cipher.Decrypt rejects tampered ciphertext or mismatched authenticated data with an authentication error","noise.CipherChaChaPoly integrates into Noise CipherSuite for secure handshake and transport message encryption"],"goal":"verify github.com/flynn/noise.CipherChaChaPoly in pkg:golang/github.com/flynn/noise@v1.0.0","kind":"HOW","packages":["pkg:golang/github.com/flynn/noise@v1.0.0"],"schemaVersion":1,"symbols":["github.com/flynn/noise.CipherChaChaPoly"]},"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/github.com/flynn/noise@v1.0.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/flynn/noise@v1.0.0","symbols":["github.com/flynn/noise.CipherChaChaPoly"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22.0

require github.com/flynn/noise v1.0.0

require (
	golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
	golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)
go.sum
github.com/flynn/noise v1.0.0 h1:DlTHqmzmvcEiKj+4RYo/imoswx/4r6iBlCMfVtrMXpQ=
github.com/flynn/noise v1.0.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
main.go
package main

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

	"github.com/flynn/noise"
)

func main() {
	// 1. Direct AEAD encryption and decryption with CipherChaChaPoly
	var key [32]byte
	if _, err := rand.Read(key[:]); err != nil {
		fmt.Fprintf(os.Stderr, "failed to generate random key: %v\n", err)
		os.Exit(1)
	}

	cipher := noise.CipherChaChaPoly.Cipher(key)
	fmt.Printf("Cipher algorithm: %s\n", noise.CipherChaChaPoly.CipherName())

	nonce := uint64(0)
	ad := []byte("header-metadata")
	plaintext := []byte("secret payload protected by ChaCha20-Poly1305")

	ciphertext := cipher.Encrypt(nil, nonce, ad, plaintext)
	fmt.Printf("Ciphertext length: %d bytes (plaintext %d + tag 16)\n", len(ciphertext), len(plaintext))

	decrypted, err := cipher.Decrypt(nil, nonce, ad, ciphertext)
	if err != nil {
		fmt.Fprintf(os.Stderr, "decryption failed: %v\n", err)
		os.Exit(1)
	}

	if !bytes.Equal(decrypted, plaintext) {
		fmt.Fprintf(os.Stderr, "decrypted message mismatch\n")
		os.Exit(1)
	}

	fmt.Printf("Successfully decrypted message: %s\n", string(decrypted))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/flynn/noise.CipherChaChaPoly in pkg:golang/github.com/flynn/noise@v1.0.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/flynn/noise@v1.0.0"
  ],
  "symbols": [
    "github.com/flynn/noise.CipherChaChaPoly"
  ]
}
test/contract.go
package main

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

	"github.com/flynn/noise"
)

func main() {
	// Assertion 1: noise.CipherChaChaPoly identifies itself as ChaChaPoly cipher suite component
	if name := noise.CipherChaChaPoly.CipherName(); name != "ChaChaPoly" {
		fmt.Fprintf(os.Stderr, "FAIL: CipherName expected 'ChaChaPoly', got %q\n", name)
		os.Exit(1)
	}

	// Assertion 2: noise.CipherChaChaPoly.Cipher initializes an AEAD cipher that encrypts plaintext with an authentication tag
	var key [32]byte
	if _, err := rand.Read(key[:]); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: rand.Read key failed: %v\n", err)
		os.Exit(1)
	}
	cipher := noise.CipherChaChaPoly.Cipher(key)
	if cipher == nil {
		fmt.Fprintf(os.Stderr, "FAIL: Cipher returned nil\n")
		os.Exit(1)
	}

	nonce := uint64(1)
	ad := []byte("authenticated-associated-data")
	plaintext := []byte("confidential message payload verified by csx contract")

	ciphertext := cipher.Encrypt(nil, nonce, ad, plaintext)
	expectedLen := len(plaintext) + 16 // ChaCha20-Poly1305 uses a 16-byte Poly1305 tag
	if len(ciphertext) != expectedLen {
		fmt.Fprintf(os.Stderr, "FAIL: ciphertext length expected %d, got %d\n", expectedLen, len(ciphertext))
		os.Exit(1)
	}
	if bytes.Equal(ciphertext[:len(plaintext)], plaintext) {
		fmt.Fprintf(os.Stderr, "FAIL: ciphertext prefix matches plaintext (encryption failed)\n")
		os.Exit(1)
	}

	// Assertion 3: noise.Cipher.Decrypt authenticates and decrypts valid ciphertext back to original plaintext
	decrypted, err := cipher.Decrypt(nil, nonce, ad, ciphertext)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: Decrypt valid ciphertext failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decrypted, plaintext) {
		fmt.Fprintf(os.Stderr, "FAIL: decrypted text mismatch: got %q, want %q\n", decrypted, plaintext)
		os.Exit(1)
	}

	// Assertion 4: noise.Cipher.Decrypt rejects tampered ciphertext or mismatched authenticated data with an authentication error
	tamperedCiphertext := make([]byte, len(ciphertext))
	copy(tamperedCiphertext, ciphertext)
	tamperedCiphertext[0] ^= 0x01
	if _, err := cipher.Decrypt(nil, nonce, ad, tamperedCiphertext); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: Decrypt succeeded on tampered ciphertext\n")
		os.Exit(1)
	}

	wrongAD := []byte("tampered-associated-data")
	if _, err := cipher.Decrypt(nil, nonce, wrongAD, ciphertext); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: Decrypt succeeded with wrong authenticated data\n")
		os.Exit(1)
	}

	wrongNonce := nonce + 1
	if _, err := cipher.Decrypt(nil, wrongNonce, ad, ciphertext); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: Decrypt succeeded with wrong nonce\n")
		os.Exit(1)
	}

	// Assertion 5: noise.CipherChaChaPoly integrates into Noise CipherSuite for secure handshake and transport message encryption
	suite := noise.NewCipherSuite(noise.DH25519, noise.CipherChaChaPoly, noise.HashSHA256)
	initHs, err := noise.NewHandshakeState(noise.Config{
		CipherSuite: suite,
		Pattern:     noise.HandshakeNN,
		Initiator:   true,
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: initiator NewHandshakeState failed: %v\n", err)
		os.Exit(1)
	}
	respHs, err := noise.NewHandshakeState(noise.Config{
		CipherSuite: suite,
		Pattern:     noise.HandshakeNN,
		Initiator:   false,
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: responder NewHandshakeState failed: %v\n", err)
		os.Exit(1)
	}

	msg1, _, _, err := initHs.WriteMessage(nil, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: initHs.WriteMessage msg1 failed: %v\n", err)
		os.Exit(1)
	}
	if _, _, _, err := respHs.ReadMessage(nil, msg1); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: respHs.ReadMessage msg1 failed: %v\n", err)
		os.Exit(1)
	}

	msg2, respSend, respRecv, err := respHs.WriteMessage(nil, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: respHs.WriteMessage msg2 failed: %v\n", err)
		os.Exit(1)
	}
	_, initRecv, initSend, err := initHs.ReadMessage(nil, msg2)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: initHs.ReadMessage msg2 failed: %v\n", err)
		os.Exit(1)
	}

	transportMsg := []byte("handshake completed with CipherChaChaPoly")
	ct, err := initSend.Encrypt(nil, nil, transportMsg)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: initSend.Encrypt failed: %v\n", err)
		os.Exit(1)
	}
	pt, err := respRecv.Decrypt(nil, nil, ct)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: respRecv.Decrypt failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(pt, transportMsg) {
		fmt.Fprintf(os.Stderr, "FAIL: transport plaintext mismatch: got %q, want %q\n", pt, transportMsg)
		os.Exit(1)
	}
	_ = respSend
	_ = initRecv

	fmt.Println("PASS: verify github.com/flynn/noise.CipherChaChaPoly contract passed")
}

原始种子者

匿名