-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
92 lines (76 loc) · 2.43 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
package passit
import (
"encoding/ascii85"
"encoding/base32"
"encoding/base64"
"io"
"strings"
)
type encodingGenerator struct {
encodeToString func([]byte) string
count int
}
func newEncoding(count int, encodeToString func([]byte) string) Generator {
if count < 0 {
panic("passit: count must be positive")
}
return &encodingGenerator{encodeToString, count}
}
func encodeToHex(hextable string, src []byte) string {
var sb strings.Builder
sb.Grow(len(src) * 2)
for _, v := range src {
sb.WriteByte(hextable[v>>4])
sb.WriteByte(hextable[v&0x0f])
}
return sb.String()
}
// HexLower returns a Generator that encodes count-bytes in lowercase hexadecimal.
func HexLower(count int) Generator {
return newEncoding(count, func(src []byte) string {
return encodeToHex("0123456789abcdef", src)
})
}
// HexUpper returns a Generator that encodes count-bytes in uppercase hexadecimal.
func HexUpper(count int) Generator {
return newEncoding(count, func(src []byte) string {
return encodeToHex("0123456789ABCDEF", src)
})
}
// Base32 returns a Generator that encodes count-bytes with
// encoding/base32.StdEncoding without padding.
func Base32(count int) Generator {
rawStd := base32.StdEncoding.WithPadding(base32.NoPadding)
return newEncoding(count, rawStd.EncodeToString)
}
// Base32Hex returns a Generator that encodes count-bytes with
// encoding/base32.HexEncoding without padding.
func Base32Hex(count int) Generator {
rawHex := base32.HexEncoding.WithPadding(base32.NoPadding)
return newEncoding(count, rawHex.EncodeToString)
}
// Base64 returns a Generator that encodes count-bytes with
// encoding/base64.RawStdEncoding.
func Base64(count int) Generator {
return newEncoding(count, base64.RawStdEncoding.EncodeToString)
}
// Base64URL returns a Generator that encodes count-bytes with
// encoding/base64.RawURLEncoding.
func Base64URL(count int) Generator {
return newEncoding(count, base64.RawURLEncoding.EncodeToString)
}
// Ascii85 returns a Generator that encodes count-bytes with encoding/ascii85.
func Ascii85(count int) Generator {
return newEncoding(count, func(src []byte) string {
dst := make([]byte, ascii85.MaxEncodedLen(len(src)))
n := ascii85.Encode(dst, src)
return string(dst[:n])
})
}
func (eg *encodingGenerator) Password(r io.Reader) (string, error) {
buf := make([]byte, eg.count)
if _, err := io.ReadFull(r, buf); err != nil {
return "", wrapReadError(err)
}
return eg.encodeToString(buf), nil
}