From 158c1deff123b9fc595f0280c17b24be9fa633e8 Mon Sep 17 00:00:00 2001 From: Peter Rabbitson Date: Sat, 23 May 2020 03:37:18 +0200 Subject: [PATCH] Base36 implementation --- go.mod | 1 + go.sum | 2 ++ multibase.go | 14 +++++++++++++- multibase_test.go | 2 ++ spec | 2 +- 5 files changed, 19 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 5e430ae..68d0982 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 510e3dc..8256cfd 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/multibase.go b/multibase.go index c1833b5..87b8118 100644 --- a/multibase.go +++ b/multibase.go @@ -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. @@ -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' @@ -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", @@ -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: @@ -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 diff --git a/multibase_test.go b/multibase_test.go index 4c75036..12f24da 100644 --- a/multibase_test.go +++ b/multibase_test.go @@ -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", diff --git a/spec b/spec index 54f897a..3806057 160000 --- a/spec +++ b/spec @@ -1 +1 @@ -Subproject commit 54f897a5032fc35d2ae50477c2918da715b43efb +Subproject commit 3806057e6f6c09c7cc0a6dc14de22fa50c9e0b51