-
Notifications
You must be signed in to change notification settings - Fork 8
/
base36.go
88 lines (73 loc) · 2.12 KB
/
base36.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
// base36 supports Base36 encoding and decoding.
package base36
import (
"crypto/md5"
"encoding/hex"
"fmt"
"strings"
"github.com/grokify/base36"
"github.com/grokify/simplego/math/bigint"
)
// Md5Base36Length is the length for a MD5 Base36 string
const (
Md5Base36Length int = 25
md5base36format string = `%025s`
)
// Encode36 returns an encoded string given a byte array.
func Encode36(ba []byte) string {
return Encode36HexString(hex.EncodeToString(ba))
}
// Encode36String returns an encoded string given a string.
func Encode36String(s string) string {
return Encode36([]byte(s))
}
// Encode36HexString returns an encoded string given a string.
func Encode36HexString(s16 string) string {
i := bigint.HexToInt(s16)
return strings.ToLower(base36.EncodeBigInt(i))
}
// Decode36 returns a decoded byte array given an encoded byte array.
func Decode36(b36 []byte) ([]byte, error) {
return Decode36String(string(b36))
}
// Decode36String returns a decoded byte array given an encoded string.
func Decode36String(s36 string) ([]byte, error) {
bi := base36.DecodeBigInt(s36)
return hex.DecodeString(bigint.IntToHex(bi))
}
// Md5Base36 returns a Base36 encoded MD5 hash of a string.
func Md5Base36(s string) string {
return fmt.Sprintf(md5base36format,
Encode36HexString(fmt.Sprintf("%x", md5.Sum([]byte(s)))))
}
// Md5String is an alias for Md5Base36.
func Md5String(s string) string {
return Md5Base36(s)
}
/*
GMP Versions
//import "github.com/grokify/gmp"
// Encode36String returns an encoded string given a byte array.
func Encode36(ba []byte) string {
s16 := hex.EncodeToString(ba)
bi := gmp.NewInt(0)
bi.SetString(s16, 16)
return bi.InBase(36)
}
// Decode36String returns a decoded byte array given an encoded string.
func Decode36String(s36 string) ([]byte, error) {
bi := gmp.NewInt(0)
bi.SetString(s36, 36)
s16 := bi.InBase(16)
return hex.DecodeString(s16)
}
// Md5Base36 returns a Base36 encoded MD5 hash of a string.
func Md5Base36(s string) string {
h := md5.New()
io.WriteString(h, s)
s16 := fmt.Sprintf("%x", h.Sum(nil))
bi := gmp.NewInt(0)
bi.SetString(s16, 16)
return fmt.Sprintf("%025s", bi.InBase(36))
}
*/