]> Cypherpunks.ru repositories - gohpenc.git/blobdiff - src/cypherpunks.ru/gohpenc/base32.go
Trivial fixes
[gohpenc.git] / src / cypherpunks.ru / gohpenc / base32.go
diff --git a/src/cypherpunks.ru/gohpenc/base32.go b/src/cypherpunks.ru/gohpenc/base32.go
new file mode 100644 (file)
index 0000000..f84388f
--- /dev/null
@@ -0,0 +1,23 @@
+package main
+
+import (
+       "encoding/base32"
+       "strings"
+)
+
+func ToBase32(data []byte) string {
+       return strings.TrimRight(base32.StdEncoding.EncodeToString(data), "=")
+}
+
+func FromBase32(data string) ([]byte, error) {
+       padSize := len(data) % 8
+       if padSize != 0 {
+               padSize = 8 - padSize
+               pad := make([]byte, 0, padSize)
+               for i := 0; i < padSize; i++ {
+                       pad = append(pad, '=')
+               }
+               data += string(pad)
+       }
+       return base32.StdEncoding.DecodeString(data)
+}