CodeSampleX

示例

go.step.sm/crypto v0.89.0: fingerprint

已验证示例 — golang go.step.sm/crypto v0.89.0: fingerprint. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: fingerprint.New hashes input data and formats…

sha256:c37864c627383ea2d93378fe239d6ee99e1785f2eaa7a3bcf249dcf1ece09c88

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 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-02

案例

HOW
目标
verify go.step.sm/crypto/fingerprint in pkg:golang/go.step.sm/crypto@v0.89.0
包
符号
  • go.step.sm/crypto/fingerprint
创建时间
2026-09-02T22:07:11Z

契约

  1. fingerprint.New hashes input data and formats fingerprints into standard encodings (hex, base64, base64url, base64raw, base64rawurl, emoji)
  2. fingerprint.Fingerprint formats precomputed digests into specified encodings without hashing
  3. fingerprint.New supports multiple cryptographic hash algorithms including SHA-256 and SHA-512
  4. fingerprint.New returns an error when an unavailable hash algorithm or invalid encoding is supplied

文件

  • PROMPT.md
  • csx.json
  • fingerprint.go
  • go.mod
  • go.sum
  • 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 go.step.sm/crypto/fingerprint in pkg:golang/go.step.sm/crypto@v0.89.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/go.step.sm/crypto@v0.89.0
Demonstrate these symbols/APIs:
  - go.step.sm/crypto/fingerprint

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:6cf7e54627bf99685e4a8067e0361a923c8a3d3a705e98bb5c8e32a18a117ef4","contract":["fingerprint.New hashes input data and formats fingerprints into standard encodings (hex, base64, base64url, base64raw, base64rawurl, emoji)","fingerprint.Fingerprint formats precomputed digests into specified encodings without hashing","fingerprint.New supports multiple cryptographic hash algorithms including SHA-256 and SHA-512","fingerprint.New returns an error when an unavailable hash algorithm or invalid encoding is supplied"],"goal":"verify go.step.sm/crypto/fingerprint in pkg:golang/go.step.sm/crypto@v0.89.0","kind":"HOW","packages":["pkg:golang/go.step.sm/crypto@v0.89.0"],"schemaVersion":1,"symbols":["go.step.sm/crypto/fingerprint"]},"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/go.step.sm/crypto@v0.89.0"],"schemaVersion":1,"subject":"pkg:golang/go.step.sm/crypto@v0.89.0","symbols":["go.step.sm/crypto/fingerprint"],"verifierAdapter":"golang@1"}
fingerprint.go
package fingerprintsample

import (
	"crypto"
	"fmt"

	"go.step.sm/crypto/fingerprint"
)

// HashAndFingerprint hashes data with the specified algorithm and formats it using the given encoding.
func HashAndFingerprint(data []byte, h crypto.Hash, enc fingerprint.Encoding) (string, error) {
	return fingerprint.New(data, h, enc)
}

// FormatRawDigest formats a precomputed hash digest using the given fingerprint encoding.
func FormatRawDigest(digest []byte, enc fingerprint.Encoding) (string, error) {
	result := fingerprint.Fingerprint(digest, enc)
	if result == "" && len(digest) > 0 {
		return "", fmt.Errorf("unsupported or invalid fingerprint encoding: %d", enc)
	}
	return result, nil
}

// AvailableEncodings returns the list of all standard fingerprint encodings supported by the package.
func AvailableEncodings() []fingerprint.Encoding {
	return []fingerprint.Encoding{
		fingerprint.HexFingerprint,
		fingerprint.Base64Fingerprint,
		fingerprint.Base64URLFingerprint,
		fingerprint.Base64RawFingerprint,
		fingerprint.Base64RawURLFingerprint,
		fingerprint.EmojiFingerprint,
	}
}
go.mod
module example.com/fingerprint-sample

go 1.26.6

require go.step.sm/crypto v0.89.0
go.sum
go.step.sm/crypto v0.89.0 h1:NPxryE+cZ4XDwXOQY2U6Gu+mOzOihuRLPIsX7WtPzZM=
go.step.sm/crypto v0.89.0/go.mod h1:IEgE6DndnYojbDyHU7eYXpwWzpu4ftkouw7nyI6jsxg=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify go.step.sm/crypto/fingerprint in pkg:golang/go.step.sm/crypto@v0.89.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/go.step.sm/crypto@v0.89.0"
  ],
  "symbols": [
    "go.step.sm/crypto/fingerprint"
  ]
}
test/contract.go
package main

import (
	"crypto"
	"crypto/sha256"
	_ "crypto/sha256"
	_ "crypto/sha512"
	"encoding/base64"
	"encoding/hex"
	"fmt"
	"os"
	"strings"

	sample "example.com/fingerprint-sample"
	"go.step.sm/crypto/fingerprint"
)

func main() {
	inputData := []byte("clean-room fingerprint contract payload")

	// Assertion 1: fingerprint.New computes correct fingerprints for all standard encodings
	expectedSum := sha256.Sum256(inputData)
	expectedHex := hex.EncodeToString(expectedSum[:])
	expectedB64 := base64.StdEncoding.EncodeToString(expectedSum[:])
	expectedB64URL := base64.URLEncoding.EncodeToString(expectedSum[:])
	expectedRawB64 := base64.RawStdEncoding.EncodeToString(expectedSum[:])
	expectedRawB64URL := base64.RawURLEncoding.EncodeToString(expectedSum[:])

	// Hex
	hexFP, err := fingerprint.New(inputData, crypto.SHA256, fingerprint.HexFingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: fingerprint.New with HexFingerprint failed: %v\n", err)
		os.Exit(1)
	}
	if hexFP != strings.ToLower(expectedHex) {
		fmt.Fprintf(os.Stderr, "FAIL: Hex mismatch: got %q, want %q\n", hexFP, expectedHex)
		os.Exit(1)
	}

	// Base64
	b64FP, err := fingerprint.New(inputData, crypto.SHA256, fingerprint.Base64Fingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: fingerprint.New with Base64Fingerprint failed: %v\n", err)
		os.Exit(1)
	}
	if b64FP != expectedB64 {
		fmt.Fprintf(os.Stderr, "FAIL: Base64 mismatch: got %q, want %q\n", b64FP, expectedB64)
		os.Exit(1)
	}

	// Base64URL
	b64urlFP, err := fingerprint.New(inputData, crypto.SHA256, fingerprint.Base64URLFingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: fingerprint.New with Base64URLFingerprint failed: %v\n", err)
		os.Exit(1)
	}
	if b64urlFP != expectedB64URL {
		fmt.Fprintf(os.Stderr, "FAIL: Base64URL mismatch: got %q, want %q\n", b64urlFP, expectedB64URL)
		os.Exit(1)
	}

	// Base64Raw
	rawB64FP, err := fingerprint.New(inputData, crypto.SHA256, fingerprint.Base64RawFingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: fingerprint.New with Base64RawFingerprint failed: %v\n", err)
		os.Exit(1)
	}
	if rawB64FP != expectedRawB64 {
		fmt.Fprintf(os.Stderr, "FAIL: Base64Raw mismatch: got %q, want %q\n", rawB64FP, expectedRawB64)
		os.Exit(1)
	}

	// Base64RawURL
	rawB64urlFP, err := fingerprint.New(inputData, crypto.SHA256, fingerprint.Base64RawURLFingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: fingerprint.New with Base64RawURLFingerprint failed: %v\n", err)
		os.Exit(1)
	}
	if rawB64urlFP != expectedRawB64URL {
		fmt.Fprintf(os.Stderr, "FAIL: Base64RawURL mismatch: got %q, want %q\n", rawB64urlFP, expectedRawB64URL)
		os.Exit(1)
	}

	// Emoji
	emojiFP, err := fingerprint.New(inputData, crypto.SHA256, fingerprint.EmojiFingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: fingerprint.New with EmojiFingerprint failed: %v\n", err)
		os.Exit(1)
	}
	if len(emojiFP) == 0 {
		fmt.Fprintf(os.Stderr, "FAIL: Emoji fingerprint should not be empty\n")
		os.Exit(1)
	}

	// Assertion 2: fingerprint.Fingerprint formats precomputed raw digests directly
	rawDigest := []byte{0xde, 0xad, 0xbe, 0xef, 0x01, 0x02, 0x03, 0x04}
	directHex := fingerprint.Fingerprint(rawDigest, fingerprint.HexFingerprint)
	if directHex != "deadbeef01020304" {
		fmt.Fprintf(os.Stderr, "FAIL: direct Fingerprint Hex mismatch: got %q, want %q\n", directHex, "deadbeef01020304")
		os.Exit(1)
	}
	directB64 := fingerprint.Fingerprint(rawDigest, fingerprint.Base64Fingerprint)
	expectedDirectB64 := base64.StdEncoding.EncodeToString(rawDigest)
	if directB64 != expectedDirectB64 {
		fmt.Fprintf(os.Stderr, "FAIL: direct Fingerprint Base64 mismatch: got %q, want %q\n", directB64, expectedDirectB64)
		os.Exit(1)
	}

	formatted, err := sample.FormatRawDigest(rawDigest, fingerprint.HexFingerprint)
	if err != nil || formatted != "deadbeef01020304" {
		fmt.Fprintf(os.Stderr, "FAIL: sample.FormatRawDigest failed: %v\n", err)
		os.Exit(1)
	}

	// Assertion 3: fingerprint.New supports different hash functions like SHA-512
	sha512FP, err := sample.HashAndFingerprint(inputData, crypto.SHA512, fingerprint.HexFingerprint)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: sample.HashAndFingerprint with SHA-512 failed: %v\n", err)
		os.Exit(1)
	}
	if len(sha512FP) != 128 {
		fmt.Fprintf(os.Stderr, "FAIL: expected 128 hex chars for SHA-512, got %d\n", len(sha512FP))
		os.Exit(1)
	}

	// Assertion 4: fingerprint.New rejects unavailable hash functions with an error
	unavailableHash := crypto.Hash(9999)
	if _, err := fingerprint.New(inputData, unavailableHash, fingerprint.HexFingerprint); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error when using unavailable hash function, got nil\n")
		os.Exit(1)
	}

	// Assertion 5: fingerprint.New and Fingerprint reject invalid encoding values
	invalidEnc := fingerprint.Encoding(999)
	if _, err := fingerprint.New(inputData, crypto.SHA256, invalidEnc); err == nil {
		fmt.Fprintf(os.Stderr, "FAIL: expected error when using invalid encoding in New, got nil\n")
		os.Exit(1)
	}
	if got := fingerprint.Fingerprint(rawDigest, invalidEnc); got != "" {
		fmt.Fprintf(os.Stderr, "FAIL: expected empty string when using invalid encoding in Fingerprint, got %q\n", got)
		os.Exit(1)
	}

	fmt.Println("PASS: go.step.sm/crypto/fingerprint contract passed")
}

原始种子者

匿名