CodeSampleX

Sample

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

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

sha256:d83b1a75ce849051063b84b9a5473d3fde3512cc1619631a4c755383015ad498

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-09-04

Case

HOW
Goal
verify golang.org/x/crypto/bcrypt.GenerateFromPassword in pkg:golang/golang.org/x/crypto@v0.21.0
Packages
Symbols
  • golang.org/x/crypto/bcrypt.GenerateFromPassword
Created
2026-09-04T12:10:55Z

Contract

  1. bcrypt.GenerateFromPassword generates a valid 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.21.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/crypto@v0.21.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:c1d89be3f191b2e747c6f3f3ff3f9dfeaf5e7e6ccb9b2892046d41b8d1f86c9d","contract":["bcrypt.GenerateFromPassword generates a valid 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.21.0","kind":"HOW","packages":["pkg:golang/golang.org/x/crypto@v0.21.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.21.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/crypto@v0.21.0","symbols":["golang.org/x/crypto/bcrypt.GenerateFromPassword"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22

require golang.org/x/crypto v0.21.0
go.sum
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
main.go
package main

import (
	"bytes"
	"fmt"
	"log"

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

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

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

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

	// Verify password matches 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.21.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/crypto@v0.21.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("contract-test-password-456")

	// 1. bcrypt.GenerateFromPassword generates a valid 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 len(hashMin) == 0 {
		fmt.Fprintf(os.Stderr, "Generated hash is empty\n")
		os.Exit(1)
	}
	if !bytes.HasPrefix(hashMin, []byte("$2a$")) {
		fmt.Fprintf(os.Stderr, "Generated hash has unexpected prefix: %s\n", hashMin)
		os.Exit(1)
	}
	costMin, err := bcrypt.Cost(hashMin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.Cost failed for min cost hash: %v\n", err)
		os.Exit(1)
	}
	if costMin != bcrypt.MinCost {
		fmt.Fprintf(os.Stderr, "Expected MinCost (%d), got %d\n", bcrypt.MinCost, costMin)
		os.Exit(1)
	}

	// 2. bcrypt.GenerateFromPassword with cost 0 automatically defaults to DefaultCost
	hashDefault, err := bcrypt.GenerateFromPassword(password, 0)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.GenerateFromPassword failed with cost 0: %v\n", err)
		os.Exit(1)
	}
	costDefault, err := bcrypt.Cost(hashDefault)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.Cost failed for default hash: %v\n", err)
		os.Exit(1)
	}
	if costDefault != bcrypt.DefaultCost {
		fmt.Fprintf(os.Stderr, "Expected DefaultCost (%d), got %d\n", bcrypt.DefaultCost, costDefault)
		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 below MinCost: %v\n", err)
		os.Exit(1)
	}
	costBelowMin, err := bcrypt.Cost(hashBelowMin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "bcrypt.Cost failed for below-min hash: %v\n", err)
		os.Exit(1)
	}
	if costBelowMin != bcrypt.DefaultCost {
		fmt.Fprintf(os.Stderr, "Expected DefaultCost (%d) for cost below MinCost, got %d\n", bcrypt.DefaultCost, costBelowMin)
		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, "bcrypt.GenerateFromPassword second run failed: %v\n", err)
		os.Exit(1)
	}
	if bytes.Equal(hashMin, hashMin2) {
		fmt.Fprintf(os.Stderr, "Expected different hashes for same password due to random salt, but got identical hashes\n")
		os.Exit(1)
	}

	// 5. bcrypt.GenerateFromPassword rejects password exceeding 72 bytes with ErrPasswordTooLong
	longPassword := make([]byte, 73)
	for i := range longPassword {
		longPassword[i] = 'x'
	}
	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
	if _, err := bcrypt.GenerateFromPassword(password, bcrypt.MaxCost+1); err == nil {
		fmt.Fprintf(os.Stderr, "Expected error when cost exceeds MaxCost, got nil\n")
		os.Exit(1)
	} else {
		var invalidCostErr bcrypt.InvalidCostError
		if !errors.As(err, &invalidCostErr) {
			fmt.Fprintf(os.Stderr, "Expected bcrypt.InvalidCostError, got %T: %v\n", 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, "bcrypt.CompareHashAndPassword failed for matching password: %v\n", err)
		os.Exit(1)
	}

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

Origin Seeder

anonymous