CodeSampleX

샘플

golang.org/x/crypto v0.58.0: argon2.IDKey, bcrypt.GenerateFromPassword, bcrypt.CompareHashAndPassword

검증된 샘플 — golang golang.org/x/crypto v0.58.0: argon2.IDKey, bcrypt.GenerateFromPassword, bcrypt.CompareHashAndPassword. go 1.26 · linux debian/x64 · docker에서…

sha256:e7a03011c02651cc7f3b0deab237f39eed428c05912bdc00e7a37a8445267a14

이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다. 통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다. 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-10-29

케이스

HOW
목표
verify pkg:golang/golang.org/x/crypto@v0.58.0
패키지
심벌
  • argon2.IDKey
  • bcrypt.GenerateFromPassword
  • bcrypt.CompareHashAndPassword
  • sha3.Sum256
환경
go 1.26.6
생성일
2026-10-29T17:03:05Z

컨트랙트

  1. argon2.IDKey derives deterministic 32-byte key slices and produces distinct keys when password or salt changes
  2. bcrypt.GenerateFromPassword hashes passwords at the requested cost and bcrypt.CompareHashAndPassword accepts the matching password while returning bcrypt.ErrMismatchedHashAndPassword on mismatch
  3. sha3.Sum256 and streaming sha3.New256 produce identical 32-byte digests for the same input and distinct digests for different inputs

파일

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

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.58.0

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:cdff1b51f3e6a7941c3c0eb53962b56cee88beb42a3f0a7af48c33d7dcf876af","contract":["argon2.IDKey derives deterministic 32-byte key slices and produces distinct keys when password or salt changes","bcrypt.GenerateFromPassword hashes passwords at the requested cost and bcrypt.CompareHashAndPassword accepts the matching password while returning bcrypt.ErrMismatchedHashAndPassword on mismatch","sha3.Sum256 and streaming sha3.New256 produce identical 32-byte digests for the same input and distinct digests for different inputs"],"goal":"verify pkg:golang/golang.org/x/crypto@v0.58.0","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.58.0"],"schemaVersion":1,"symbols":["argon2.IDKey","bcrypt.GenerateFromPassword","bcrypt.CompareHashAndPassword","sha3.Sum256"]},"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.58.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.58.0","symbols":["argon2.IDKey","bcrypt.GenerateFromPassword","bcrypt.CompareHashAndPassword","sha3.Sum256"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require golang.org/x/crypto v0.58.0

require golang.org/x/sys v0.49.0 // indirect
go.sum
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
main.go
package main

import (
	"encoding/hex"
	"fmt"

	"golang.org/x/crypto/argon2"
	"golang.org/x/crypto/bcrypt"
	"golang.org/x/crypto/sha3"
)

func main() {
	password := []byte("example-passphrase")
	salt := []byte("example-salt-16b")

	derived := argon2.IDKey(password, salt, 1, 32*1024, 2, 32)
	fmt.Printf("argon2id: %s\n", hex.EncodeToString(derived))

	hashed, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost)
	if err != nil {
		panic(err)
	}
	if err := bcrypt.CompareHashAndPassword(hashed, password); err != nil {
		panic(err)
	}

	digest := sha3.Sum256(password)
	fmt.Printf("sha3-256: %s\n", hex.EncodeToString(digest[:]))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/crypto@v0.58.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.58.0"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"os"

	"golang.org/x/crypto/argon2"
	"golang.org/x/crypto/bcrypt"
	"golang.org/x/crypto/sha3"
)

func main() {
	pass := []byte("verification-passphrase")
	salt := []byte("verification-salt-16")

	// 1. argon2.IDKey derives deterministic key bytes matching requested length and varies with password or salt
	key1 := argon2.IDKey(pass, salt, 1, 32*1024, 2, 32)
	if len(key1) != 32 {
		fmt.Fprintf(os.Stderr, "expected key length 32, got %d\n", len(key1))
		os.Exit(1)
	}
	key2 := argon2.IDKey(pass, salt, 1, 32*1024, 2, 32)
	if !bytes.Equal(key1, key2) {
		fmt.Fprintf(os.Stderr, "expected deterministic argon2.IDKey output\n")
		os.Exit(1)
	}
	keyDiffPass := argon2.IDKey([]byte("other-passphrase"), salt, 1, 32*1024, 2, 32)
	if bytes.Equal(key1, keyDiffPass) {
		fmt.Fprintf(os.Stderr, "expected different argon2.IDKey output for different password\n")
		os.Exit(1)
	}
	keyDiffSalt := argon2.IDKey(pass, []byte("other-salt-value-16"), 1, 32*1024, 2, 32)
	if bytes.Equal(key1, keyDiffSalt) {
		fmt.Fprintf(os.Stderr, "expected different argon2.IDKey output for different salt\n")
		os.Exit(1)
	}

	// 2. bcrypt.GenerateFromPassword and bcrypt.CompareHashAndPassword verify matching passwords and reject mismatches
	hash, err := bcrypt.GenerateFromPassword(pass, bcrypt.MinCost)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.GenerateFromPassword failed: %v\n", err)
		os.Exit(1)
	}
	cost, err := bcrypt.Cost(hash)
	if err != nil || cost != bcrypt.MinCost {
		fmt.Fprintf(os.Stderr, "expected bcrypt cost %d, got %d (err=%v)\n", bcrypt.MinCost, cost, err)
		os.Exit(1)
	}
	if err := bcrypt.CompareHashAndPassword(hash, pass); err != nil {
		fmt.Fprintf(os.Stderr, "expected bcrypt.CompareHashAndPassword to succeed: %v\n", err)
		os.Exit(1)
	}
	if err := bcrypt.CompareHashAndPassword(hash, []byte("wrong-passphrase")); !errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
		fmt.Fprintf(os.Stderr, "expected ErrMismatchedHashAndPassword, got: %v\n", err)
		os.Exit(1)
	}

	// 3. sha3.Sum256 and sha3.New256 produce identical 32-byte digests for the same input
	sum := sha3.Sum256(pass)
	h := sha3.New256()
	_, _ = h.Write(pass)
	streamSum := h.Sum(nil)
	if !bytes.Equal(sum[:], streamSum) {
		fmt.Fprintf(os.Stderr, "expected sha3.Sum256 and sha3.New256 to match\n")
		os.Exit(1)
	}
	otherSum := sha3.Sum256([]byte("different-input"))
	if bytes.Equal(sum[:], otherSum[:]) {
		fmt.Fprintf(os.Stderr, "expected different sha3-256 digests for distinct inputs\n")
		os.Exit(1)
	}

	fmt.Println("Contract passed.")
}

오리진 시더

익명