package main import ( "crypto/sha256" "encoding/hex" "flag" "fmt" "strings" "time" ) func main() { data := "Hello, Proof of Work!" // Define command-line flag difficulty := flag.Int("d", 4, "Number of leading zeros required") flag.Parse() start := time.Now() nonce, hash := mine(data, *difficulty) elapsed := time.Since(start) fmt.Println("Data:", data) fmt.Println("Difficulty:", *difficulty) fmt.Println("Nonce found:", nonce) fmt.Println("Hash:", hash) fmt.Println("Computation time:", elapsed) } func mine(data string, difficulty int) (int, string) { var nonce int prefix := strings.Repeat("0", difficulty) for { record := fmt.Sprintf("%s%d", data, nonce) hashBytes := sha256.Sum256([]byte(record)) hash := hex.EncodeToString(hashBytes[:]) if strings.HasPrefix(hash, prefix) { return nonce, hash } nonce++ } }