-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
85 lines (68 loc) · 2.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"log"
"math/rand"
"strconv"
)
func main() {
// Generate client seed
client_seed := generateSeedAsHash()
fmt.Printf("Client seed hash: %s\n", client_seed)
// Generate server seed
server_seed := generateSeedAsHash()
fmt.Printf("Server seed hash: %s\n\n", server_seed)
// Generate nonce - Hash revealed after the round is over. Otherwise the game can be predicted!
// Incremental nonce's also shouldn't revealed without encryption / hashing because of prediction risk.
// A real encryption should be considered. Hash could be decrypted by a rainbow table. But maybe string salting is enough.
nonce, nonce_hash := generateNonce()
fmt.Printf("Nonce num: %v\n", nonce)
fmt.Printf("Nonce hash: %s\n\n", nonce_hash)
// Generate provably fair number
provably_fair_num, err := generateProvablyFairNumber(client_seed, server_seed, nonce_hash)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Provably fair num: %v\n", provably_fair_num)
// Validate generated provably fair number
validation_client_seed := client_seed
validation_server_seed := server_seed
validation_nonce_hash := nonce_hash
validation_provably_fair_num := provably_fair_num
if ValidateProvablyFairNumber(validation_client_seed, validation_server_seed, validation_nonce_hash, validation_provably_fair_num) {
fmt.Print("Provability fair validation successful!")
} else {
fmt.Print("Provability fair validation not successful!")
}
}
func ValidateProvablyFairNumber(client_seed string, server_seed string, nonce_hash string, provably_fair_num int64) (validation_result bool) {
validation_provably_fair_num, err := generateProvablyFairNumber(client_seed, server_seed, nonce_hash)
if err != nil {
log.Fatal(err)
}
return validation_provably_fair_num == provably_fair_num
}
func generateSeedAsHash() (seed string) {
client_seed_bytes := sha256.New()
client_seed_bytes.Write([]byte(strconv.Itoa(rand.Int())))
return fmt.Sprintf("%x", client_seed_bytes.Sum(nil))
}
func generateNonce() (nonce string, hash string) {
nonce_num := 1
nonce_str := strconv.Itoa(nonce_num)
nonce_hash := generateSha512Hash(nonce_str)
return nonce_str, nonce_hash
}
func generateProvablyFairNumber(server_seed string, client_seed string, nonce string) (number int64, err error) {
provably_fair_input := server_seed + "_" + client_seed + "_" + nonce
provably_fair_hash := generateSha512Hash(provably_fair_input)
provably_fair_hash_prefix := provably_fair_hash[0:8]
return strconv.ParseInt(provably_fair_hash_prefix, 16, 64)
}
func generateSha512Hash(value string) (hash string) {
bytes := sha512.New()
bytes.Write([]byte(value))
return fmt.Sprintf("%x", bytes.Sum(nil))
}