Exemple
golang.org/x/crypto v0.57.0: bcrypt.GenerateFromPassword
Échantillon vérifié pour golang golang.org/x/crypto v0.57.0: bcrypt.GenerateFromPassword. Le contrat s'est exécuté sur go 1.26 · linux debian/x64 · docker et…
sha256:c2c4edb19777690577ab7105e2b17ec127a71f3a0de38ef75c57faab1f2f1a58
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 1
- Clés de signature qui l’ont compilé
- 1
Environnement déclaré
linux 24 · ubuntu · glibc 2.39 x64 go
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| 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 |
Cas
HOW- Objectif
- verify golang.org/x/crypto/bcrypt.GenerateFromPassword in pkg:golang/golang.org/x/crypto@v0.57.0
- Paquets
- Symboles
-
- golang.org/x/crypto/bcrypt.GenerateFromPassword
- Créé
- 2026-11-05T03:50:16Z
Contrat
- 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
Fichiers
- PROMPT.md
- csx.json
- go.mod
- go.sum
- main.go
- spec.json
- test/contract.go
Code source
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.
{"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"}
module example.com/sample
go 1.26.6
require golang.org/x/crypto v0.57.0
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
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.")
}
{
"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"
]
}
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.")
}
Seeder d'origine
anonyme