-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhashcash.go
197 lines (167 loc) · 4.42 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package hashcash
import (
"crypto/rand"
"crypto/sha1"
"encoding/base64"
"encoding/binary"
"fmt"
"math"
"math/big"
"strconv"
"strings"
"time"
)
const (
dateLayout = "20060102150405"
zeroBit = '0'
)
// New - returns new hashcash
func New(bits int, resource string) (*Hashcash, error) {
rand, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt32))
if err != nil {
return nil, err
}
if bits <= 0 {
return nil, ErrZeroBitsMustBeMoreThanZero
}
return &Hashcash{
bits: bits,
date: time.Now().UTC().Truncate(time.Second),
resource: resource,
rand: rand.Bytes(),
}, nil
}
// Hashcash - hashcash structure
// Version 1
type Hashcash struct {
bits int // number of zero bits in hashed code
date time.Time // time that the message was sent
resource string // resource data string (IP address, email address, etc)
extension string // extension, ignored in this version
rand []byte // random characters
counter int // computing counter
}
// Bits - returns number of zero bits
func (h *Hashcash) Bits() int {
return h.bits
}
// Counter - returns counter
func (h *Hashcash) Counter() int {
return h.counter
}
// EqualResource - check if input resource is equal with hashcash resource
func (h *Hashcash) EqualResource(resource string) bool {
return h.resource == resource
}
// IsActual - check if hashcash expiration exceeded ttl
func (h *Hashcash) IsActual(ttl time.Duration) bool {
return h.date.Add(ttl).After(time.Now().UTC())
}
// Compute - compute hash with enough zero bits in the begining
// Increase counter if hash does't have enough zero bits in the begining
func (h *Hashcash) Compute(maxAttempts int) error {
if maxAttempts > 0 {
h.counter = 0
for h.counter <= maxAttempts {
ok, err := h.Header().IsHashCorrect(h.bits)
if err != nil {
return err
}
if ok {
return nil
}
h.counter++
}
}
return ErrComputingMaxAttemptsExceeded
}
// Key - returns string presentation of hashcash without counter
// Key is using to match original hashcash with solved hashcash
func (h *Hashcash) Key() string {
return fmt.Sprintf("%d:%d:%s:%d", h.bits, h.date.Unix(), h.resource, binary.BigEndian.Uint32(h.rand))
}
// Header - returns string presentation of hashcash to share it
func (h *Hashcash) Header() Header {
return Header(fmt.Sprintf("1:%d:%s:%s:%s:%s:%s",
h.bits,
h.date.Format(dateLayout),
h.resource,
h.extension,
base64.StdEncoding.EncodeToString(h.rand),
base64.StdEncoding.EncodeToString([]byte(strconv.Itoa(h.counter))),
))
}
// ParseHeader - parse hashcah from header
func ParseHeader(header string) (hashcash *Hashcash, err error) {
parts := strings.Split(header, ":")
if len(parts) < 7 {
return nil, ErrIncorrectHeaderFormat
}
if len(parts) > 7 {
for i := 0; i < len(parts)-7; i++ {
parts[3] += ":" + parts[3+i+1]
}
parts[4] = parts[len(parts)-3]
parts[5] = parts[len(parts)-2]
parts[6] = parts[len(parts)-1]
parts = parts[:7]
}
if parts[0] != "1" {
return nil, ErrIncorrectHeaderFormat
}
hashcash = &Hashcash{}
hashcash.bits, err = strconv.Atoi(parts[1])
if err != nil {
return nil, ErrIncorrectHeaderFormat
}
hashcash.date, err = time.ParseInLocation(dateLayout, parts[2], time.UTC)
if err != nil {
return nil, ErrIncorrectHeaderFormat
}
hashcash.resource = parts[3]
hashcash.extension = parts[4]
hashcash.rand, err = base64.StdEncoding.DecodeString(parts[5])
if err != nil {
return nil, ErrIncorrectHeaderFormat
}
counterStr, err := base64.StdEncoding.DecodeString(parts[6])
if err != nil {
return nil, ErrIncorrectHeaderFormat
}
hashcash.counter, err = strconv.Atoi(string(counterStr))
if err != nil {
return nil, ErrIncorrectHeaderFormat
}
return
}
// Header - string presentation of hashcash
// Format - 1:bits:date:resource:externsion:rand:counter
type Header string
// IsHashCorrect - does header hash constain zero bits enough
func (header Header) IsHashCorrect(bits int) (ok bool, err error) {
if bits <= 0 {
return false, ErrZeroBitsMustBeMoreThanZero
}
hash, err := header.sha1()
if err != nil {
return ok, err
}
if len(hash) < bits {
return false, ErrHashLengthLessThanZeroBits
}
ok = true
for _, s := range hash[:bits] {
if s != zeroBit {
ok = false
break
}
}
return
}
func (header Header) sha1() (hash string, err error) {
hasher := sha1.New()
if _, err = hasher.Write([]byte(header)); err != nil {
return
}
return fmt.Sprintf("%x", hasher.Sum(nil)), nil
}