-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwithout_padding_base32.go
102 lines (91 loc) · 2.83 KB
/
without_padding_base32.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
package withoutpaddingbase32
import (
"math"
"strings"
)
const (
InByteSize = 8
OutByteSize = 5
Base32Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
)
func EncodeToBase32String(src string) string {
if src == "" {
return ""
}
bytes := []byte(src)
sbuf := make([]string, len(bytes)*InByteSize/OutByteSize)
var (
bytesPosition uint
bytesSubPosition uint
outputBase32Byte uint
outputBase32BytePosition uint
)
bytesPosition = 0
bytesSubPosition = 0
outputBase32Byte = 0
outputBase32BytePosition = 0
i := 0
for bytesPosition < uint(len(bytes)) {
i++
bitsAvailableInByte := uint(math.Min(float64(InByteSize-bytesSubPosition), float64(OutByteSize-outputBase32BytePosition)))
outputBase32Byte = outputBase32Byte << bitsAvailableInByte
outputBase32Byte |= uint(bytes[bytesPosition] >> (InByteSize - (bytesSubPosition + bitsAvailableInByte)))
bytesSubPosition += bitsAvailableInByte
if bytesSubPosition >= InByteSize {
bytesPosition++
bytesSubPosition = 0
}
outputBase32BytePosition += bitsAvailableInByte
if outputBase32BytePosition >= OutByteSize {
outputBase32Byte &= 0x1F // 0x1F = 00011111 in binary
sbuf = append(sbuf, string(Base32Alphabet[outputBase32Byte]))
outputBase32BytePosition = 0
}
}
if outputBase32BytePosition > 0 {
outputBase32Byte = outputBase32Byte << (OutByteSize - outputBase32BytePosition)
outputBase32Byte &= 0x1F // 0x1F = 00011111 in binary
sbuf = append(sbuf, string(Base32Alphabet[outputBase32Byte]))
}
return strings.Join(sbuf, "")
}
func DecodeFromBase32String(base32Str string) string {
if base32Str == "" {
return ""
}
base32StringUpperCase := strings.ToUpper(base32Str)
outputBytes := make([]byte, len(base32StringUpperCase)*OutByteSize/InByteSize)
if len(outputBytes) == 0 {
return ""
}
var (
base32Position uint
base32SubPosition uint
outputBytePosition uint
outputByteSubPosition uint
)
base32Position = 0
base32SubPosition = 0
outputBytePosition = 0
outputByteSubPosition = 0
for outputBytePosition < uint(len(outputBytes)) {
currentBase32Byte := strings.Index(Base32Alphabet, string(base32StringUpperCase[base32Position]))
if currentBase32Byte < 0 {
return ""
}
bitsAvailableInByte := uint(math.Min(float64(OutByteSize-base32SubPosition), float64(InByteSize-outputByteSubPosition)))
outputBytes[outputBytePosition] = outputBytes[outputBytePosition] << bitsAvailableInByte
outputBytes[outputBytePosition] |= byte(currentBase32Byte >> (OutByteSize - (base32SubPosition + bitsAvailableInByte)))
outputByteSubPosition += bitsAvailableInByte
if outputByteSubPosition >= InByteSize {
outputBytePosition++
outputByteSubPosition = 0
}
base32SubPosition += bitsAvailableInByte
if base32SubPosition >= OutByteSize {
base32Position++
base32SubPosition = 0
}
}
return string(outputBytes)
}