-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.go
More file actions
227 lines (201 loc) Β· 5.42 KB
/
misc.go
File metadata and controls
227 lines (201 loc) Β· 5.42 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package geheim
import (
"context"
"crypto/hmac"
"encoding/binary"
"errors"
"fmt"
"io"
"os"
"strings"
"time"
"golang.org/x/term"
)
type PrintFunc func(version int, header Header, key []byte) error
const (
CipherDesc = "cipher"
KDFDesc = "key derivation"
HashDesc = "hash"
SecDesc = "security"
)
var (
meta = NewMeta()
header, _ = meta.Header()
MetaSize = int64(binary.Size(meta))
HeaderSize = int64(binary.Size(header))
OverheadSize = MetaSize + HeaderSize
)
var (
ErrKey = errors.New("geheim: empty key")
ErrHeader = errors.New("geheim: malformed header")
ErrAuth = errors.New("geheim: authentication verification failed")
ErrCipher = fmt.Errorf("geheim: invalid %s (%s)", CipherDesc, CipherString)
ErrKDF = fmt.Errorf("geheim: invalid %s (%s)", KDFDesc, KDFString)
ErrHash = fmt.Errorf("geheim: invalid %s (%s)", HashDesc, HashString)
ErrSec = fmt.Errorf("geheim: invalid %s (%s)", SecDesc, SecString)
)
func Verify(x, y []byte) error {
if !hmac.Equal(x, y) {
return ErrAuth
}
return nil
}
func NewDefaultPrintFunc(w io.Writer) PrintFunc {
printf := func(format string, a ...any) { fmt.Fprintf(w, format, a...) }
return func(version int, header Header, key []byte) error {
cipher, hash, kdf, sec, salt, nonce := header.Get()
printf("%-8s%d\n", "VERSION", version)
printf("%-8s%s(%d)\n", "CIPHER", CipherNames[cipher], cipher)
printf("%-8s%s(%d)\n", "HASH", HashNames[hash], hash)
var hkdf string
if kdf != HKDF {
hkdf = "+HKDF"
}
printf("%-8s%s%s-%s(%d)\n", "KDF", KDFNames[kdf], hkdf, HashNames[hash], kdf)
printf("%-8sHMAC-%s\n", "MAC", HashNames[hash])
if kdf != HKDF {
printf("%-8s%s(%d)\n", "SEC", FormatSize(GetMemory(sec), 0), sec)
}
printf("%-8s%x\n", "SALT", salt)
printf("%-8s%x\n", "NONCE", nonce)
if kdf == HKDF {
printf("%-8s%x\n", "KEY", key)
} else {
printf("%-8s%s(%x)\n", "KEY", key, key)
}
return nil
}
}
func FormatSize(n int64, dec uint) string {
var unit string
nn := float64(n)
f := fmt.Sprintf("%%.%df", dec)
switch {
case nn >= 1<<60:
nn /= 1 << 60
unit = "E"
case nn >= 1<<50:
nn /= 1 << 50
unit = "P"
case nn >= 1<<40:
nn /= 1 << 40
unit = "T"
case nn >= 1<<30:
nn /= 1 << 30
unit = "G"
case nn >= 1<<20:
nn /= 1 << 20
unit = "M"
case nn >= 1<<10:
nn /= 1 << 10
unit = "K"
default:
f = "%.f"
}
return fmt.Sprintf("%s%sB", fmt.Sprintf(f, nn), unit)
}
type ProgressWriter struct {
TotalBytes int64
bytesWritten int64
lastBytesWritten int64
initTime time.Time
lastTime time.Time
}
var _ io.Writer = (*ProgressWriter)(nil)
func NewProgressWriter(total int64) *ProgressWriter { return &ProgressWriter{TotalBytes: total} }
func (w *ProgressWriter) Write(p []byte) (n int, err error) {
n = len(p)
w.bytesWritten += int64(n)
return
}
func (w *ProgressWriter) Reset() {
w.bytesWritten = 0
w.lastBytesWritten = 0
}
func (w *ProgressWriter) Progress(ctx context.Context, d time.Duration) {
w.initTime = time.Now()
w.lastTime = w.initTime
t := time.Tick(d)
for {
w.Print(false)
w.lastBytesWritten = w.bytesWritten
w.lastTime = time.Now()
select {
case <-ctx.Done():
w.Print(true)
return
case <-t:
}
}
}
const (
leftBracket = " ["
rightBracket = "] "
emptyBar = '-'
arrowBar = '>'
competeBar = '='
)
func (w *ProgressWriter) Print(last bool) {
hasTotalPerc := w.TotalBytes > 0
var (
perc float64
totalPerc string
)
if hasTotalPerc {
perc = float64(w.bytesWritten) / float64(w.TotalBytes)
totalPerc = fmt.Sprintf("/%s (%.f%%)", FormatSize(w.TotalBytes, 2), perc*100)
}
left := fmt.Sprintf("%s%s", FormatSize(w.bytesWritten, 2), totalPerc)
var right string
if last {
right = fmt.Sprintf("%s/s", FormatSize(int64(float64(w.bytesWritten)/float64(time.Since(w.initTime))/time.Nanosecond.Seconds()), 2))
} else {
right = fmt.Sprintf("%s/s", FormatSize(int64(float64(w.bytesWritten-w.lastBytesWritten)/float64(time.Since(w.lastTime))/time.Nanosecond.Seconds()), 2))
}
width, _, _ := term.GetSize(int(os.Stderr.Fd()))
middleWidth := width - len(left) - len(right)
var middle string
if hasTotalPerc {
barsWidth := middleWidth - len(leftBracket) - len(rightBracket)
if barsWidth >= 0 {
complete := int(float64(barsWidth) * perc)
bars := make([]byte, barsWidth)
for i := range bars {
if i < complete {
bars[i] = competeBar
} else if i != 0 && i == complete {
bars[i] = arrowBar
} else {
bars[i] = emptyBar
}
}
middle = fmt.Sprintf("%s%s%s", leftBracket, bars, rightBracket)
}
}
middle = fmt.Sprintf(fmt.Sprintf("%%%ds", middleWidth-len(middle)), middle)
var newline string
if last {
newline = "\n"
}
fmt.Fprintf(os.Stderr, "\r%s%s%s%s", left, middle, right, newline)
}
func readBE(r io.Reader, v any) error { return binary.Read(r, binary.BigEndian, v) }
func writeBE(w io.Writer, v any) error { return binary.Write(w, binary.BigEndian, v) }
func readBEN[T any](r io.Reader) (n T, err error) {
err = readBE(r, &n)
return
}
func writeBEN[T any](w io.Writer, n T) error { return writeBE(w, n) }
func getOptionString[T comparable](values []T, names map[T]string) string {
d := make([]string, len(values))
for i, item := range values {
d[i] = fmt.Sprintf("%v:%s", item, names[item])
}
return strings.Join(d, ", ")
}
func checkBytesSize[T comparable](sizes map[T]int, key T, value []byte, name string) error {
if sizes[key] != len(value) {
return fmt.Errorf("geheim: invalid %s size", name)
}
return nil
}