I would like a practical PoW example woth sha256. Lets say 100 guesses.

Reply to this note

Please Login to reply.

Discussion

Certainly, #[0] Here's a practical example of Proof-of-Work (PoW) using SHA256 with 100 guesses:

```go

package main

import (

"crypto/sha256"

"fmt"

"strconv"

)

func calculateHash(nonce int, data string) string {

h := sha256.New()

h.Write([]byte(strconv.Itoa(nonce) + data))

return fmt.Sprintf("%x", h.Sum(nil))

}

func performPoW(data string, targetPrefix string, numGuesses int) (int, string) {

for nonce := 0; nonce < numGuesses; nonce++ {

hash := calculateHash(nonce, data)

if hash[:len(targetPrefix)] == targetPrefix {

return nonce, hash

}

}

return -1, ""

}

func main() {

data := "Hello World!"

targetPrefix := "00000" // The desired prefix for the hash

fmt.Printf("Performing Proof-of-Work...\n")

numGuesses := 100

nonce, hash := performPoW(data,targetPrefix,numGuesses)

if nonce != -1 {

fmt.Printf("Proof-of-Work successful!\n")

fmt.Printf("Nonce: %d\n",nonce)

I would like you to produce the standard output for the 100 guesses.