-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
152 lines (125 loc) · 3.51 KB
/
encoding.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
// Probably very overengineered right now.
// Will think up a better solution later.
package token
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
"github.com/Mobilpadde/moths/v6/token/emojies"
)
const (
seperator = ";;;"
delimiter = ":"
letterSecret = "s"
letterAmount = "a"
letterEmojies = "e"
letterPeriod = "p"
letterTime = "t"
typeSecret = "%s"
typeAmount = "%d"
typeEmojies = "%s"
typePeriod = "%d"
typeTime = "%d"
prefixSecret = letterSecret + delimiter
prefixAmount = letterAmount + delimiter
prefixEmojies = letterEmojies + delimiter
prefixPeriod = letterPeriod + delimiter
prefixTime = letterTime + delimiter
patternField = "%s%s%s"
patternEncoded = "%s%s%s%s%s"
)
var (
patternSecret = fmt.Sprintf(patternField, prefixSecret, typeSecret, seperator)
patternAmount = fmt.Sprintf(patternField, prefixAmount, typeAmount, seperator)
patternEmojies = fmt.Sprintf(patternField, prefixEmojies, typeEmojies, seperator)
patternPeriod = fmt.Sprintf(patternField, prefixPeriod, typePeriod, seperator)
patternTime = fmt.Sprintf(patternField, prefixTime, typeTime, seperator)
)
// Encode generators fields as a
// base64 string.
//
// Should not be shared, as this
// shows the secret as a string
// if this is decoded.
func (g *Generator) Export() string {
secret := fmt.Sprintf(patternSecret, g.secret)
amount := fmt.Sprintf(patternAmount, g.amount)
emojies := fmt.Sprintf(patternEmojies, g.emojies)
period := fmt.Sprintf(patternPeriod, g.period)
time := fmt.Sprintf(patternTime, g.timing.time.Unix())
str := fmt.Sprintf(patternEncoded, secret, amount, emojies, period, time)
str = str[0 : len(str)-3]
return base64.StdEncoding.EncodeToString([]byte(str))
}
// Try parsing a given base64
// encoded string into Generator.
func (g *Generator) Import(encoded string) error {
str, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
return err
}
updatable := []struct {
prefix string
fn func(string) error
}{
{prefixSecret, g.parseSecret},
{prefixAmount, g.parseAmount},
{prefixEmojies, g.parseEmojies},
{prefixPeriod, g.parsePeriod},
{prefixTime, g.parseTime},
}
split := strings.Split(string(str), seperator)
for _, x := range split { // loop over each field
for _, y := range updatable { // loop over each updatable field
if strings.HasPrefix(x, y.prefix) { // if current field is updatable
if err := y.fn(x); err != nil { // udpate
return err
}
break // break out of inner loop as we found the updatable field
}
}
}
g.Check()
return nil
}
func (g *Generator) parseSecret(x string) error {
str := x[len(prefixSecret):]
// We don't encode secret again,
// so we update the secret (byte-slice)
// manually.
g.secret = []byte(str)
return nil
}
func (g *Generator) parseAmount(x string) error {
str := x[len(prefixAmount):]
amount, err := strconv.Atoi(str)
if err != nil {
return err
}
return g.SetAmount(amount)
}
func (g *Generator) parseEmojies(x string) error {
str := x[len(prefixEmojies):]
split := strings.Split(str, "")
return g.SetEmojies(emojies.ToEmojies(split))
}
func (g *Generator) parsePeriod(x string) error {
str := x[len(prefixPeriod):]
period, err := strconv.Atoi(str)
if err != nil {
return err
}
duration := time.Duration(period)
return g.SetPeriod(duration)
}
func (g *Generator) parseTime(x string) error {
str := x[len(prefixTime):]
unix, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return err
}
parsed := time.Unix(unix, 0).UTC()
return g.SetTime(parsed)
}