-
Notifications
You must be signed in to change notification settings - Fork 2
/
hashcash.go
107 lines (92 loc) · 2 KB
/
hashcash.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package hashcash
import (
"encoding/base64"
"fmt"
"hash"
"math/rand"
"strconv"
)
type Hashcash struct {
hasher hash.Hash
bits uint
resource string
rand string
counter uint64
}
func NewHashcash(resource string, bits uint, hasher hash.Hash) *Hashcash {
rb, _ := randomBytes(8)
return &Hashcash{
hasher: hasher,
bits: bits,
resource: resource,
rand: base64EncodeBytes(rb),
counter: rand.Uint64(),
}
}
func (h *Hashcash) Compute() string {
header := h.GetHeader()
for !h.verifyProof(header) {
h.counter++
header = h.GetHeader()
}
return header
}
func (h *Hashcash) Verify(header string) (bool, error) {
// TODO: check header formatting and other fields.
return h.verifyProof(header), nil
}
func (h *Hashcash) verifyProof(header string) bool {
digest := h.Digest(header)
return checkZeros(digest, h.bits)
}
func (h *Hashcash) Digest(header string) string {
h.hasher.Reset()
h.hasher.Write([]byte(header))
return fmt.Sprintf("%x", h.hasher.Sum(nil))
}
func (h *Hashcash) GetHeader() string {
return fmt.Sprintf("1:%d::%s::%s:%s",
h.bits,
h.resource,
h.rand,
base64EncodeUint(h.counter),
)
}
func checkZeros(digest string, bits uint) bool {
// Do we need additional check for the last rune representing last four bits?
mod := bits % 4
runes := bits / 4
if mod != 0 || bits == 0 {
runes++
}
for _, v := range digest[:runes-1] {
if v != '0' {
return false
}
}
lastChar := int(digest[runes-1] - '0')
if mod == 0 && lastChar != 0 {
return false
} else if mod == 3 && lastChar > 7 {
return false
} else if mod == 2 && lastChar > 3 {
return false
} else if mod == 1 && lastChar > 1 {
return false
}
return true
}
func randomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
func base64EncodeBytes(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
func base64EncodeUint(n uint64) string {
return base64EncodeBytes([]byte(strconv.FormatUint(n, 10)))
}