main.go (511B)
1 package main 2 3 import ( 4 "crypto/sha256" 5 "encoding/hex" 6 "fmt" 7 "os" 8 "strconv" 9 "strings" 10 ) 11 12 // Build with `go build -o pow main.go` 13 // Use `./pow my_username` 14 func main() { 15 username := os.Args[1] 16 difficulty := 7 17 prefix := strings.Repeat("0", difficulty) 18 var nonce int 19 for { 20 h := sha256.Sum256([]byte(username + ":" + strconv.Itoa(nonce))) 21 hashed := hex.EncodeToString(h[:]) 22 if strings.HasPrefix(hashed, prefix) { 23 fmt.Printf("%s:%d -> %s\n", username, nonce, hashed) 24 return 25 } 26 nonce++ 27 } 28 }