CodeSampleX

Пример

golang.org/x/crypto v0.48.0: scrypt.Key

Проверенный пример — golang golang.org/x/crypto v0.48.0: scrypt.Key. Контракт выполнен на go 1.26 · linux debian/x64 · docker и пройден.

sha256:32a0d369818b1db9ba0cda46c0d112e8bd4e2063fc9321c32e8703a1701e0954

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. 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-03

Кейс

HOW
Цель
verify golang.org/x/crypto/scrypt.Key in pkg:golang/golang.org/x/crypto@v0.48.0
Пакеты
Символы
  • golang.org/x/crypto/scrypt.Key
Создан
2026-09-03T03:49:51Z

Контракт

  1. scrypt.Key derives a key of requested length using valid parameters
  2. scrypt.Key produces deterministic output matching standard test vectors
  3. scrypt.Key rejects cost parameter N less than or equal to 1
  4. scrypt.Key rejects cost parameter N that is not a power of 2
  5. scrypt.Key rejects non-positive block size parameter r
  6. scrypt.Key rejects non-positive parallelization parameter p
  7. scrypt.Key produces different derived keys for different passwords or salts

Файлы

  • 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/scrypt.Key in pkg:golang/golang.org/x/crypto@v0.48.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.48.0
Demonstrate these symbols/APIs:
  - golang.org/x/crypto/scrypt.Key

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:877a186dce24e133e7ee7fa4beb79930b5077cfae4f50ec411a5d1938365bbcc","contract":["scrypt.Key derives a key of requested length using valid parameters","scrypt.Key produces deterministic output matching standard test vectors","scrypt.Key rejects cost parameter N less than or equal to 1","scrypt.Key rejects cost parameter N that is not a power of 2","scrypt.Key rejects non-positive block size parameter r","scrypt.Key rejects non-positive parallelization parameter p","scrypt.Key produces different derived keys for different passwords or salts"],"goal":"verify golang.org/x/crypto/scrypt.Key in pkg:golang/golang.org/x/crypto@v0.48.0","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.48.0"],"schemaVersion":1,"symbols":["golang.org/x/crypto/scrypt.Key"]},"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.48.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.48.0","symbols":["golang.org/x/crypto/scrypt.Key"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/crypto v0.48.0
go.sum
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
main.go
package main

import (
	"encoding/hex"
	"fmt"
	"log"

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

func main() {
	password := []byte("example-passphrase-to-derive")
	salt := []byte("example-random-salt-16bytes")

	// Derivation parameters:
	// N = 16384 (CPU/memory cost, power of 2 > 1)
	// r = 8 (block size)
	// p = 1 (parallelization)
	// keyLen = 32 (length of derived key in bytes)
	key, err := scrypt.Key(password, salt, 16384, 8, 1, 32)
	if err != nil {
		log.Fatalf("scrypt.Key derivation failed: %v", err)
	}

	fmt.Printf("Derived key (hex): %s\n", hex.EncodeToString(key))
	fmt.Printf("Derived key length: %d bytes\n", len(key))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/crypto/scrypt.Key in pkg:golang/golang.org/x/crypto@v0.48.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.48.0"
  ],
  "symbols": [
    "golang.org/x/crypto/scrypt.Key"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"os"

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

func main() {
	pass := []byte("correct-password-42")
	salt := []byte("unique-salt-sample")

	// 1. scrypt.Key derives a key of requested length using valid parameters
	key32, err := scrypt.Key(pass, salt, 16, 8, 1, 32)
	if err != nil {
		fmt.Fprintf(os.Stderr, "scrypt.Key failed with valid parameters: %v\n", err)
		os.Exit(1)
	}
	if len(key32) != 32 {
		fmt.Fprintf(os.Stderr, "scrypt.Key returned unexpected key length: got %d, want 32\n", len(key32))
		os.Exit(1)
	}

	key64, err := scrypt.Key(pass, salt, 16, 8, 1, 64)
	if err != nil {
		fmt.Fprintf(os.Stderr, "scrypt.Key failed for 64-byte key length: %v\n", err)
		os.Exit(1)
	}
	if len(key64) != 64 {
		fmt.Fprintf(os.Stderr, "scrypt.Key returned unexpected key length: got %d, want 64\n", len(key64))
		os.Exit(1)
	}

	// 2. scrypt.Key produces deterministic output matching standard test vectors
	expectedVec1 := []byte{
		0x48, 0xb0, 0xd2, 0xa8, 0xa3, 0x27, 0x26, 0x11, 0x98,
		0x4c, 0x50, 0xeb, 0xd6, 0x30, 0xaf, 0x52,
	}
	vec1, err := scrypt.Key([]byte("p"), []byte("s"), 2, 1, 1, 16)
	if err != nil {
		fmt.Fprintf(os.Stderr, "scrypt.Key failed on test vector 1: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(vec1, expectedVec1) {
		fmt.Fprintf(os.Stderr, "scrypt.Key vector 1 mismatch: got %x, want %x\n", vec1, expectedVec1)
		os.Exit(1)
	}

	expectedVec2 := []byte{
		0x77, 0xd6, 0x57, 0x62, 0x38, 0x65, 0x7b, 0x20, 0x3b,
		0x19, 0xca, 0x42, 0xc1, 0x8a, 0x04, 0x97, 0xf1, 0x6b,
		0x48, 0x44, 0xe3, 0x07, 0x4a, 0xe8, 0xdf, 0xdf, 0xfa,
		0x3f, 0xed, 0xe2, 0x14, 0x42, 0xfc, 0xd0, 0x06, 0x9d,
		0xed, 0x09, 0x48, 0xf8, 0x32, 0x6a, 0x75, 0x3a, 0x0f,
		0xc8, 0x1f, 0x17, 0xe8, 0xd3, 0xe0, 0xfb, 0x2e, 0x0d,
		0x36, 0x28, 0xcf, 0x35, 0xe2, 0x0c, 0x38, 0xd1, 0x89,
		0x06,
	}
	vec2, err := scrypt.Key([]byte(""), []byte(""), 16, 1, 1, 64)
	if err != nil {
		fmt.Fprintf(os.Stderr, "scrypt.Key failed on test vector 2: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(vec2, expectedVec2) {
		fmt.Fprintf(os.Stderr, "scrypt.Key vector 2 mismatch: got %x, want %x\n", vec2, expectedVec2)
		os.Exit(1)
	}

	// 3. scrypt.Key rejects cost parameter N less than or equal to 1
	for _, invalidN := range []int{0, 1, -1} {
		if _, err := scrypt.Key(pass, salt, invalidN, 8, 1, 32); err == nil {
			fmt.Fprintf(os.Stderr, "scrypt.Key unexpectedly succeeded for invalid N=%d\n", invalidN)
			os.Exit(1)
		} else if err.Error() != "scrypt: N must be > 1 and a power of 2" {
			fmt.Fprintf(os.Stderr, "scrypt.Key returned unexpected error for N=%d: got %v\n", invalidN, err)
			os.Exit(1)
		}
	}

	// 4. scrypt.Key rejects cost parameter N that is not a power of 2
	for _, nonPowerOf2 := range []int{3, 7, 10, 15} {
		if _, err := scrypt.Key(pass, salt, nonPowerOf2, 8, 1, 32); err == nil {
			fmt.Fprintf(os.Stderr, "scrypt.Key unexpectedly succeeded for non-power-of-2 N=%d\n", nonPowerOf2)
			os.Exit(1)
		} else if err.Error() != "scrypt: N must be > 1 and a power of 2" {
			fmt.Fprintf(os.Stderr, "scrypt.Key returned unexpected error for non-power-of-2 N=%d: got %v\n", nonPowerOf2, err)
			os.Exit(1)
		}
	}

	// 5. scrypt.Key rejects non-positive block size parameter r
	for _, invalidR := range []int{0, -1} {
		if _, err := scrypt.Key(pass, salt, 16, invalidR, 1, 32); err == nil {
			fmt.Fprintf(os.Stderr, "scrypt.Key unexpectedly succeeded for invalid r=%d\n", invalidR)
			os.Exit(1)
		} else if err.Error() != "scrypt: parameters must be > 0" {
			fmt.Fprintf(os.Stderr, "scrypt.Key returned unexpected error for r=%d: got %v\n", invalidR, err)
			os.Exit(1)
		}
	}

	// 6. scrypt.Key rejects non-positive parallelization parameter p
	for _, invalidP := range []int{0, -1} {
		if _, err := scrypt.Key(pass, salt, 16, 8, invalidP, 32); err == nil {
			fmt.Fprintf(os.Stderr, "scrypt.Key unexpectedly succeeded for invalid p=%d\n", invalidP)
			os.Exit(1)
		} else if err.Error() != "scrypt: parameters must be > 0" {
			fmt.Fprintf(os.Stderr, "scrypt.Key returned unexpected error for p=%d: got %v\n", invalidP, err)
			os.Exit(1)
		}
	}

	// 7. scrypt.Key produces different derived keys for different passwords or salts
	keyAltPass, err := scrypt.Key([]byte("different-password-42"), salt, 16, 8, 1, 32)
	if err != nil {
		fmt.Fprintf(os.Stderr, "scrypt.Key failed for alternate password: %v\n", err)
		os.Exit(1)
	}
	if bytes.Equal(key32, keyAltPass) {
		fmt.Fprintf(os.Stderr, "scrypt.Key produced identical keys for different passwords\n")
		os.Exit(1)
	}

	keyAltSalt, err := scrypt.Key(pass, []byte("different-salt-sample"), 16, 8, 1, 32)
	if err != nil {
		fmt.Fprintf(os.Stderr, "scrypt.Key failed for alternate salt: %v\n", err)
		os.Exit(1)
	}
	if bytes.Equal(key32, keyAltSalt) {
		fmt.Fprintf(os.Stderr, "scrypt.Key produced identical keys for different salts\n")
		os.Exit(1)
	}

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

Исходный сидер

аноним