Skip to content

Commit

Permalink
Merge pull request #36 from multiformats/feat/base36
Browse files Browse the repository at this point in the history
Base36 implementation
  • Loading branch information
Stebalien authored May 23, 2020
2 parents ee5c233 + 158c1de commit e2260b5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.11
require (
github.com/mr-tron/base58 v1.1.0
github.com/multiformats/go-base32 v0.0.3
github.com/multiformats/go-base36 v0.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github.com/mr-tron/base58 v1.1.0 h1:Y51FGVJ91WBqCEabAi5OPUz38eAx8DakuAm5svLcsfQ=
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ89tUg4F4=
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
14 changes: 13 additions & 1 deletion multibase.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

b58 "github.com/mr-tron/base58/base58"
b32 "github.com/multiformats/go-base32"
b36 "github.com/multiformats/go-base36"
)

// Encoding identifies the type of base-encoding that a multibase is carrying.
Expand All @@ -29,8 +30,10 @@ const (
Base32hexUpper = 'V'
Base32hexPad = 't'
Base32hexPadUpper = 'T'
Base58Flickr = 'Z'
Base36 = 'k'
Base36Upper = 'K'
Base58BTC = 'z'
Base58Flickr = 'Z'
Base64 = 'm'
Base64url = 'u'
Base64pad = 'M'
Expand All @@ -52,6 +55,8 @@ var EncodingToStr = map[Encoding]string{
'V': "base32hexupper",
't': "base32hexpad",
'T': "base32hexpadupper",
'k': "base36",
'K': "base36upper",
'z': "base58btc",
'Z': "base58flickr",
'm': "base64",
Expand Down Expand Up @@ -102,6 +107,10 @@ func Encode(base Encoding, data []byte) (string, error) {
return string(Base32hexPad) + base32HexLowerPad.EncodeToString(data), nil
case Base32hexPadUpper:
return string(Base32hexPadUpper) + base32HexUpperPad.EncodeToString(data), nil
case Base36:
return string(Base36) + b36.EncodeToStringLc(data), nil
case Base36Upper:
return string(Base36Upper) + b36.EncodeToStringUc(data), nil
case Base58BTC:
return string(Base58BTC) + b58.EncodeAlphabet(data, b58.BTCAlphabet), nil
case Base58Flickr:
Expand Down Expand Up @@ -149,6 +158,9 @@ func Decode(data string) (Encoding, []byte, error) {
case Base32hexPad, Base32hexPadUpper:
bytes, err := b32.HexEncoding.DecodeString(data[1:])
return enc, bytes, err
case Base36, Base36Upper:
bytes, err := b36.DecodeString(data[1:])
return enc, bytes, err
case Base58BTC:
bytes, err := b58.DecodeAlphabet(data[1:], b58.BTCAlphabet)
return Base58BTC, bytes, err
Expand Down
2 changes: 2 additions & 0 deletions multibase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var encodedSamples = map[Encoding]string{
Base32hexUpper: "V8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144",
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
Base36: "km552ng4dabi4neu1oo8l4i5mndwmpc3mkukwtxy9",
Base36Upper: "KM552NG4DABI4NEU1OO8L4I5MNDWMPC3MKUKWTXY9",
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
Base58Flickr: "Z36tpRGiQ9Endr7dHahm9xwQdhmoER4emaRVT",
Base64: "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
Expand Down
2 changes: 1 addition & 1 deletion spec

0 comments on commit e2260b5

Please sign in to comment.