-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
57 lines (48 loc) · 1.15 KB
/
decode.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
package xbase32
import (
"bytes"
"errors"
"strings"
)
func readChar(alphabet string, char rune) (int, error) {
idx := strings.IndexRune(alphabet, char)
if idx == -1 {
return -1, errors.New("invalid character found: " + string(char))
}
return idx, nil
}
func Decode(input string, variant string) ([]byte, error) {
var alphabet string
switch variant {
case "RFC3548", "RFC4648":
alphabet = RFC4648
input = strings.TrimRight(input, "=")
case "RFC4648-HEX":
alphabet = RFC4648_HEX
input = strings.TrimRight(input, "=")
case "Crockford":
alphabet = CROCKFORD
input = strings.ToUpper(input)
input = strings.ReplaceAll(input, "O", "0")
input = strings.ReplaceAll(input, "I", "1")
input = strings.ReplaceAll(input, "L", "1")
default:
return nil, errors.New("unknown base32 variant: " + variant)
}
bits := 0
value := 0
output := bytes.NewBuffer(nil)
for _, char := range input {
charValue, err := readChar(alphabet, char)
if err != nil {
return nil, err
}
value = (value << 5) | charValue
bits += 5
if bits >= 8 {
output.WriteByte(byte((value >> (bits - 8)) & 255))
bits -= 8
}
}
return output.Bytes(), nil
}