mit-ocw

Source code for MIT-OCW coursework
git clone git://git.laack.co/mit-ocw.git
Log | Files | Refs

commit 94f085ab4564420e321b1fe2b0ac8a372660dcc5
parent 4f86d57e7ba1e18f2a2bb9191e431bbb6793a724
Author: Andrew Laack <andrew@laack.co>
Date:   Mon, 18 May 2026 14:54:46 -0500

Simple implementation of zero prefix matching pow

Diffstat:
Amas.s62-cryptocurrency-engineering-and-design/simple-pow/go.mod | 3+++
Amas.s62-cryptocurrency-engineering-and-design/simple-pow/main.go | 76++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/mas.s62-cryptocurrency-engineering-and-design/simple-pow/go.mod b/mas.s62-cryptocurrency-engineering-and-design/simple-pow/go.mod @@ -0,0 +1,3 @@ +module pow + +go 1.26.3 diff --git a/mas.s62-cryptocurrency-engineering-and-design/simple-pow/main.go b/mas.s62-cryptocurrency-engineering-and-design/simple-pow/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/hex" + "flag" + "fmt" + "sync" +) + +var done = false +var final = "" + +func hashPrefixMatches(message string, prefix string) bool { + + result := sha256.Sum256([]byte(message + "\n")) // echo adds this by default + resString := hex.EncodeToString(result[:]) + + for idx, _ := range prefix { + if prefix[idx] != resString[idx]{ + return false + } + } + + return true +} + +func worker(message string, prefix string, wg *sync.WaitGroup) { + + defer wg.Done() + + for done == false { + text := message + rand.Text() + + if hashPrefixMatches(text, prefix) { + fmt.Println("Good: " + text) + done = true + final = text + return + } else { + continue + } + } +} + + +func main() { + + zeroesPtr := flag.Int("zeroes", 5, "Number of zeroes prefixed on sha256sum") + + flag.Parse() + + if len(flag.Args()) > 1 { + panic("Unexpected number of arguments") + } + + message := flag.Args()[0] + " - " + zeroes := *zeroesPtr + + prefix := "" + + for i := 0 ; i < zeroes ; i++ { + prefix += "0" + } + + wg := sync.WaitGroup{} + + for i := 0; i < 200; i++ { + wg.Add(1) + go worker(message, prefix, &wg) + } + wg.Wait() + + +}