Created
September 18, 2017 16:00
-
-
Save Ronsor/790e8ce5f286f2c8eed8a756966c3f18 to your computer and use it in GitHub Desktop.
proof of work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "crypto/sha512" | |
import "crypto/rand" | |
import "encoding/hex" | |
import "strings" | |
import "strconv" | |
func HashHex(data string) string { | |
d := sha512.Sum512([]byte(data)) | |
return hex.EncodeToString(d[:]) | |
} | |
func VerifyWork(diff int, hs, nonce, salt, data string) bool { | |
h := HashHex(nonce + ":" + salt + ":" + data) | |
if h != hs { return false } | |
if !strings.HasPrefix(h, strings.Repeat("0", diff)) { | |
return false | |
} | |
return true | |
} | |
func GenWork(diff int, data string) (string, string, string) { | |
h := "" | |
nr := 0 | |
done := false | |
saltb := make([]byte,8) | |
rand.Read(saltb) | |
salt := hex.EncodeToString(saltb) | |
for !done { | |
h = HashHex(strconv.Itoa(nr) + ":" + salt + ":" + data) | |
done = VerifyWork(diff, h, strconv.Itoa(nr), salt, data); | |
nr++ | |
} | |
return h, strconv.Itoa(nr), salt | |
} | |
func main() { | |
h, n, s := GenWork(2, "hi") | |
println(n + " " + s + " " + h) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment