Skip to content

Instantly share code, notes, and snippets.

@thehenster
Created December 8, 2013 18:08
Show Gist options
  • Save thehenster/7861351 to your computer and use it in GitHub Desktop.
Save thehenster/7861351 to your computer and use it in GitHub Desktop.
An example of the proof of work concept in Bitcoin.
package main
import (
"fmt"
"strconv"
"crypto/sha256"
"encoding/base64"
)
func main(){
fmt.Println(findNonce([]byte("henry"), "000"))
}
func findNonce(label []byte, target string) (nonce string) {
targetLength := len(target)
hash := sha256.New()
i := 0
for {
nonce := strconv.Itoa(i)
hash.Write([]byte(nonce))
md := hash.Sum(nil)
mdStr := base64.StdEncoding.EncodeToString(md)
if mdStr[0:targetLength] == target {
return(nonce)
}
i++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment