CodeSampleX

Sample

golang.org/x/crypto v0.57.0: bcrypt.GenerateFromPassword

Verified sample for golang golang.org/x/crypto v0.57.0: bcrypt.GenerateFromPassword. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:c2c4edb19777690577ab7105e2b17ec127a71f3a0de38ef75c57faab1f2f1a58

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
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-11-05

Case

HOW
Goal
verify golang.org/x/crypto/bcrypt.GenerateFromPassword in pkg:golang/golang.org/x/crypto@v0.57.0
Packages
Symbols
  • golang.org/x/crypto/bcrypt.GenerateFromPassword
Created
2026-11-05T03:50:16Z

Contract

  1. bcrypt.GenerateFromPassword generates a 60-byte $2a$ bcrypt hash with MinCost
  2. bcrypt.GenerateFromPassword with cost 0 automatically defaults to DefaultCost
  3. bcrypt.GenerateFromPassword with cost less than MinCost automatically defaults to DefaultCost
  4. bcrypt.GenerateFromPassword generates unique hashes for identical passwords due to distinct salts
  5. bcrypt.GenerateFromPassword rejects password exceeding 72 bytes with ErrPasswordTooLong
  6. bcrypt.GenerateFromPassword rejects cost exceeding MaxCost with InvalidCostError
  7. bcrypt.CompareHashAndPassword verifies hash created by bcrypt.GenerateFromPassword

Files

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

Download the source artifact (tar.gz)

Source

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/bcrypt.GenerateFromPassword in pkg:golang/golang.org/x/crypto@v0.57.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.57.0
Demonstrate these symbols/APIs:
  - golang.org/x/crypto/bcrypt.GenerateFromPassword

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:5f9cd3c216615dddf80de9e0454f6110e953d095eafb8c2ae5006064150b1d80","contract":["bcrypt.GenerateFromPassword generates a 60-byte $2a$ bcrypt hash with MinCost","bcrypt.GenerateFromPassword with cost 0 automatically defaults to DefaultCost","bcrypt.GenerateFromPassword with cost less than MinCost automatically defaults to DefaultCost","bcrypt.GenerateFromPassword generates unique hashes for identical passwords due to distinct salts","bcrypt.GenerateFromPassword rejects password exceeding 72 bytes with ErrPasswordTooLong","bcrypt.GenerateFromPassword rejects cost exceeding MaxCost with InvalidCostError","bcrypt.CompareHashAndPassword verifies hash created by bcrypt.GenerateFromPassword"],"goal":"verify golang.org/x/crypto/bcrypt.GenerateFromPassword in pkg:golang/golang.org/x/crypto@v0.57.0","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.57.0"],"schemaVersion":1,"symbols":["golang.org/x/crypto/bcrypt.GenerateFromPassword"]},"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.57.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.57.0","symbols":["golang.org/x/crypto/bcrypt.GenerateFromPassword"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/crypto v0.57.0
go.sum
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
main.go
package main

import (
	"bytes"
	"fmt"
	"log"

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

func main() {
	password := []byte("example-password")

	hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost)
	if err != nil {
		log.Fatalf("GenerateFromPassword failed: %v", err)
	}
	fmt.Printf("Generated hash: %s\n", hash)

	if !bytes.HasPrefix(hash, []byte("$2a$")) {
		log.Fatalf("Unexpected hash prefix: %s", hash)
	}

	if err := bcrypt.CompareHashAndPassword(hash, password); err != nil {
		log.Fatalf("CompareHashAndPassword failed: %v", err)
	}
	fmt.Println("Password verified successfully.")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/crypto/bcrypt.GenerateFromPassword in pkg:golang/golang.org/x/crypto@v0.57.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.57.0"
  ],
  "symbols": [
    "golang.org/x/crypto/bcrypt.GenerateFromPassword"
  ]
}
test/contract.go
package main

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

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

func main() {
	password := []byte("sample-secret-password")

	// 1. bcrypt.GenerateFromPassword generates a 60-byte $2a$ bcrypt hash with MinCost
	hashMin, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.GenerateFromPassword failed with MinCost: %v\n", err)
		os.Exit(1)
	}
	if !bytes.HasPrefix(hashMin, []byte("$2a$04$")) || len(hashMin) != 60 {
		fmt.Fprintf(os.Stderr, "Unexpected hash format: %s (len=%d)\n", hashMin, len(hashMin))
		os.Exit(1)
	}
	costMin, err := bcrypt.Cost(hashMin)
	if err != nil || costMin != bcrypt.MinCost {
		fmt.Fprintf(os.Stderr, "Expected MinCost (%d), got %d (err=%v)\n", bcrypt.MinCost, costMin, err)
		os.Exit(1)
	}

	// 2. bcrypt.GenerateFromPassword with cost 0 automatically defaults to DefaultCost
	hashZero, err := bcrypt.GenerateFromPassword(password, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.GenerateFromPassword failed with cost 0: %v\n", err)
		os.Exit(1)
	}
	costZero, err := bcrypt.Cost(hashZero)
	if err != nil || costZero != bcrypt.DefaultCost {
		fmt.Fprintf(os.Stderr, "Expected DefaultCost (%d) for cost 0, got %d (err=%v)\n", bcrypt.DefaultCost, costZero, err)
		os.Exit(1)
	}

	// 3. bcrypt.GenerateFromPassword with cost less than MinCost automatically defaults to DefaultCost
	hashBelowMin, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost-1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.GenerateFromPassword failed with cost < MinCost: %v\n", err)
		os.Exit(1)
	}
	costBelowMin, err := bcrypt.Cost(hashBelowMin)
	if err != nil || costBelowMin != bcrypt.DefaultCost {
		fmt.Fprintf(os.Stderr, "Expected DefaultCost (%d) for cost < MinCost, got %d (err=%v)\n", bcrypt.DefaultCost, costBelowMin, err)
		os.Exit(1)
	}

	// 4. bcrypt.GenerateFromPassword generates unique hashes for identical passwords due to distinct salts
	hashMin2, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Second bcrypt.GenerateFromPassword failed: %v\n", err)
		os.Exit(1)
	}
	if bytes.Equal(hashMin, hashMin2) {
		fmt.Fprintf(os.Stderr, "Expected unique hashes for identical passwords, got identical hashes\n")
		os.Exit(1)
	}

	// 5. bcrypt.GenerateFromPassword rejects password exceeding 72 bytes with ErrPasswordTooLong
	longPassword := bytes.Repeat([]byte("a"), 73)
	if _, err := bcrypt.GenerateFromPassword(longPassword, bcrypt.MinCost); !errors.Is(err, bcrypt.ErrPasswordTooLong) {
		fmt.Fprintf(os.Stderr, "Expected ErrPasswordTooLong for 73-byte password, got: %v\n", err)
		os.Exit(1)
	}

	// 6. bcrypt.GenerateFromPassword rejects cost exceeding MaxCost with InvalidCostError
	_, err = bcrypt.GenerateFromPassword(password, bcrypt.MaxCost+1)
	var invalidCostErr bcrypt.InvalidCostError
	if !errors.As(err, &invalidCostErr) || int(invalidCostErr) != bcrypt.MaxCost+1 {
		fmt.Fprintf(os.Stderr, "Expected InvalidCostError(%d), got %T: %v\n", bcrypt.MaxCost+1, err, err)
		os.Exit(1)
	}

	// 7. bcrypt.CompareHashAndPassword verifies hash created by bcrypt.GenerateFromPassword
	if err := bcrypt.CompareHashAndPassword(hashMin, password); err != nil {
		fmt.Fprintf(os.Stderr, "CompareHashAndPassword failed on matching password: %v\n", err)
		os.Exit(1)
	}
	if err := bcrypt.CompareHashAndPassword(hashMin, []byte("wrong-password")); !errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
		fmt.Fprintf(os.Stderr, "Expected ErrMismatchedHashAndPassword, got: %v\n", err)
		os.Exit(1)
	}

	fmt.Println("Contract assertions passed.")
}

Origin Seeder

anonymous