-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase32.go
More file actions
101 lines (83 loc) · 2.18 KB
/
base32.go
File metadata and controls
101 lines (83 loc) · 2.18 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
// Copyright (c) 2013-2015 The btcsuite developers
// Copyright (c) 2018 Saeed Rasooli <saeed.gnu@gmail.com>
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package crock32
import (
"fmt"
"math/big"
)
//go:generate go run genalphabet.go
var bigRadix = big.NewInt(32)
var bigZero = big.NewInt(0)
func decode(s string) ([]byte, *big.Int, error) {
bigVal := big.NewInt(0)
j := big.NewInt(1)
scratch := new(big.Int)
for i := len(s) - 1; i >= 0; i-- {
tmp := b32[s[i]]
if tmp == 255 {
return nil, nil, fmt.Errorf("error in Decode: invalid character %#v", string(s[i]))
}
scratch.SetInt64(int64(tmp))
scratch.Mul(j, scratch)
bigVal.Add(bigVal, scratch)
j.Mul(j, bigRadix)
}
tmpBytes := bigVal.Bytes()
var numZeros int
for numZeros = 0; numZeros < len(s); numZeros++ {
if s[numZeros] != alphabetIdx0 {
break
}
}
flen := numZeros + len(tmpBytes)
output := make([]byte, flen)
copy(output[numZeros:], tmpBytes)
return output, bigVal, nil
}
// Decode decodes a modified base32 string to a byte slice.
func Decode(b string) ([]byte, error) {
output, _, err := decode(b)
return output, err
}
// encode and return check number (0 to 36)
func encode(b []byte, alphabet string, wantCheck bool) (string, uint8) {
x := new(big.Int)
x.SetBytes(b)
mod := uint8(0)
if wantCheck {
bigMod := new(big.Int)
bigMod.Mod(x, big.NewInt(37))
mod = uint8(bigMod.Int64())
}
output := make([]byte, 0, len(b)*136/100)
for x.Cmp(bigZero) > 0 {
mod := new(big.Int)
x.DivMod(x, bigRadix, mod)
output = append(output, alphabet[mod.Int64()])
}
// leading zero bytes
for _, i := range b {
if i != 0 {
break
}
output = append(output, alphabetIdx0)
}
// reverse
alen := len(output)
for i := 0; i < alen/2; i++ {
output[i], output[alen-1-i] = output[alen-1-i], output[i]
}
return string(output), mod
}
// Encode encodes a byte slice to a modified base32 string.
func Encode(b []byte) string {
output, _ := encode(b, alphabet, false)
return output
}
// EncodeLower encodes a byte slice to a modified base32 lower-cased string.
func EncodeLower(b []byte) string {
output, _ := encode(b, alphabetLower, false)
return output
}