-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasq.go
More file actions
178 lines (164 loc) · 3.61 KB
/
hasq.go
File metadata and controls
178 lines (164 loc) · 3.61 KB
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
package hashq_mod
import (
"bufio"
"container/list"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type CanonicalHash struct {
Sequence int32
Token string
Key string
Gen string
Owner string
//Help string
Verified bool
}
type Token struct {
Sequence int32
Data string
Digest string
Key1 string
Key2 string
LastGen string
List *list.List
}
func NewToken(data string) Token {
return Token{
Data: data,
Digest: Hash(data),
Key1: "",
Key2: NextKey(),
List: list.New(),
}
}
func EmptyKey() string {
return "00000000000000000000000000000000"
}
func (hash CanonicalHash) IsEmpty() bool {
return hash.Key == EmptyKey()
}
func (tok *Token) NextSequence() int32 {
sequence := int32(tok.List.Len() + 1)
tok.Sequence = sequence
return sequence
}
func (tok *Token) Next() CanonicalHash {
var hash = CanonicalHash{}
hash.Sequence = tok.NextSequence()
hash.Token = tok.Digest
hash.Key = tok.Key1
hash.Gen = Hash(hash.Sequence, tok.Digest, tok.Key2)
hash.Owner = Hash(hash.Sequence, tok.Digest, tok.LastGen)
//hash.Help = fmt.Sprint(hash.Sequence) + "_" + tok.Digest + "_" + tok.Key2
hash.Verified = false
tok.Key1 = tok.Key2
tok.Key2 = NextKey()
tok.LastGen = hash.Gen
tok.List.PushBack(&hash)
return hash
}
func (tok *Token) Validate() bool {
var ph *CanonicalHash
for temp := tok.List.Back(); temp != nil; temp = temp.Prev() {
if ph == nil {
ph = temp.Value.(*CanonicalHash)
continue
}
var ch = temp.Value.(*CanonicalHash)
ch.Verified = ValidateHash(*ch, *ph)
if ch.Verified != true {
return false
}
ph = ch
}
return true
}
func StoreToken(tok *Token) bool {
file, err := os.Create(tok.Digest + ".tok")
if err != nil {
log.Println(err)
return false
}
defer file.Close()
writer := bufio.NewWriter(file)
_, _ = fmt.Fprintf(writer, "%05d %s %s %s %s\n", tok.Sequence, tok.Key1, tok.Key2, tok.LastGen, tok.Data)
_ = writer.Flush()
return true
}
func LoadToken(hash string) *Token {
file, err := os.Open(hash + ".tok")
if err != nil {
log.Println(err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) < 4 {
log.Println("Error parse line \"", line, "\"")
continue
}
n, _ := strconv.ParseInt(parts[0], 10, 32)
tok := Token{
Sequence: int32(n),
Digest: hash,
Key1: parts[1],
Key2: parts[2],
LastGen: parts[3],
Data: parts[4],
List: list.New(),
}
if tok.Key1 == EmptyKey() {
tok.Key1 = ""
}
LoadHash(hash, func(hash CanonicalHash) {
tok.List.PushBack(&hash)
})
tok.Validate()
return &tok
}
return nil
}
func (tok *Token) LastHash() *CanonicalHash {
return tok.List.Back().Value.(*CanonicalHash)
}
func (tok *Token) Add(sequence int32, key string, gen string, owner string) *CanonicalHash {
var hash = CanonicalHash{Sequence: sequence, Token: tok.Digest, Key: key, Gen: gen, Owner: owner, Verified: false}
tok.List.PushBack(&hash)
return &hash
}
func LoadHash(hash string, appender func(CanonicalHash)) {
file, err := os.Open(hash + ".hasq")
if err != nil {
log.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Fields(line)
if len(parts) < 4 {
log.Println("Error parse line \"", line, "\"")
continue
}
n, _ := strconv.ParseInt(parts[0], 10, 32)
hash := CanonicalHash{
Sequence: int32(n),
Token: hash,
Key: parts[1],
Gen: parts[2],
Owner: parts[3],
Verified: false,
}
if hash.IsEmpty() {
hash.Key = ""
}
appender(hash)
}
}