From 79c5cfb588fd0d4dd98beadbbd4854eee9a293a7 Mon Sep 17 00:00:00 2001 From: Gowtham Gopalakrishnan Date: Fri, 23 Nov 2018 08:43:00 +0530 Subject: [PATCH 1/4] Fixing go lint warnings --- encoder.go | 1 + multibase.go | 1 + 2 files changed, 2 insertions(+) diff --git a/encoder.go b/encoder.go index 42e753f..436eea1 100644 --- a/encoder.go +++ b/encoder.go @@ -48,6 +48,7 @@ func EncoderByName(str string) (Encoder, error) { return Encoder{base}, nil } +// Encoding returns the Encoder's encoding func (p Encoder) Encoding() Encoding { return p.enc } diff --git a/multibase.go b/multibase.go index 38cbfb8..f51626d 100644 --- a/multibase.go +++ b/multibase.go @@ -60,6 +60,7 @@ var Encodings = map[string]Encoding{ "base64urlpad": 'U', } +// EncodingToStr is the reverse map to get string representation given encoding var EncodingToStr = map[Encoding]string{ 0x00: "identity", 'f': "base16", From 77ac1cc979e3bc70d2d14dbc12447a5ff5eb6eb6 Mon Sep 17 00:00:00 2001 From: Gowtham Gopalakrishnan Date: Sun, 25 Nov 2018 12:08:20 +0530 Subject: [PATCH 2/4] Added base8 implementation --- base8.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ multibase.go | 7 +++++++ multibase_test.go | 12 +++++++----- 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 base8.go diff --git a/base8.go b/base8.go new file mode 100644 index 0000000..3316896 --- /dev/null +++ b/base8.go @@ -0,0 +1,50 @@ +package multibase + +import ( + "fmt" + "strconv" +) + +func octalEncodeToString(src []byte) string { + dst := make([]byte, len(src)*3) + octalEncode(dst, src) + return string(dst) +} + +var octalTable = [8]byte{ + '0', '1', '2', '3', '4', '5', '6', '7', +} + +func octalEncode(dst, src []byte) int { + for i, v := range src { + dst[i*3+2] = octalTable[v&0x7] + v = v >> 3 + dst[i*3+1] = octalTable[v&0x7] + v = v >> 3 + dst[i*3] = octalTable[v&0x7] + v = v >> 3 + } + + return len(src) * 3 +} + +// decodeOctalString takes an octal string and gives byte array of decimals +func decodeOctalString(s string) ([]byte, error) { + data := make([]byte, len(s)/3) + if len(s)%3 != 0 { + return nil, fmt.Errorf("cannot decode multibase: %s", + "length should be a multiple of 3") + } + + for i, dstIndex := 0, 0; i < len(s); i = i + 3 { + value, err := strconv.ParseInt(s[i:i+3], 8, 8) + if err != nil { + return nil, fmt.Errorf("error while conversion: %s", err) + } + + data[dstIndex] = byte(value) + dstIndex++ + } + + return data, nil +} diff --git a/multibase.go b/multibase.go index f51626d..bf9034c 100644 --- a/multibase.go +++ b/multibase.go @@ -42,6 +42,7 @@ const ( // specified in standard are left out var Encodings = map[string]Encoding{ "identity": 0x00, + "base8": '7', "base16": 'f', "base16upper": 'F', "base32": 'b', @@ -63,6 +64,7 @@ var Encodings = map[string]Encoding{ // EncodingToStr is the reverse map to get string representation given encoding var EncodingToStr = map[Encoding]string{ 0x00: "identity", + '7': "base8", 'f': "base16", 'F': "base16upper", 'b': "base32", @@ -93,6 +95,8 @@ func Encode(base Encoding, data []byte) (string, error) { case Identity: // 0x00 inside a string is OK in golang and causes no problems with the length calculation. return string(Identity) + string(data), nil + case Base8: + return string(Base8) + octalEncodeToString(data), nil case Base16: return string(Base16) + hex.EncodeToString(data), nil case Base16Upper: @@ -142,6 +146,9 @@ func Decode(data string) (Encoding, []byte, error) { switch enc { case Identity: return Identity, []byte(data[1:]), nil + case Base8: + bytes, err := decodeOctalString(data[1:]) + return enc, bytes, err case Base16, Base16Upper: bytes, err := hex.DecodeString(data[1:]) return enc, bytes, err diff --git a/multibase_test.go b/multibase_test.go index f389f5c..942345b 100644 --- a/multibase_test.go +++ b/multibase_test.go @@ -24,6 +24,7 @@ func TestMap(t *testing.T) { var sampleBytes = []byte("Decentralize everything!!!") var encodedSamples = map[Encoding]string{ Identity: string(0x00) + "Decentralize everything!!!", + Base8: "7104145143145156164162141154151172145040145166145162171164150151156147041041041", Base16: "f446563656e7472616c697a652065766572797468696e67212121", Base16Upper: "F446563656E7472616C697A652065766572797468696E67212121", Base32: "birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijbee", @@ -96,17 +97,17 @@ func TestRoundTrip(t *testing.T) { } if e != base { - t.Fatal("got wrong encoding out") + t.Fatal("got wrong encoding output") } if !bytes.Equal(buf, out) { - t.Fatal("input wasnt the same as output", buf, out) + t.Fatal("input wasn't the same as output", buf, out) } } _, _, err := Decode("") if err == nil { - t.Fatal("shouldnt be able to decode empty string") + t.Fatal("shouldn't be able to decode empty string") } } @@ -117,6 +118,7 @@ func BenchmarkRoundTrip(b *testing.B) { bases := map[string]Encoding{ "Identity": Identity, + "Base8": Base8, "Base16": Base16, "Base16Upper": Base16Upper, "Base32": Base32, @@ -149,11 +151,11 @@ func BenchmarkRoundTrip(b *testing.B) { } if e != base { - b.Fatal("got wrong encoding out") + b.Fatal("got wrong encoding output") } if !bytes.Equal(buf, out) { - b.Fatal("input wasnt the same as output", buf, out) + b.Fatal("input wasn't the same as output", buf, out) } } }) From f738a395a671180ee06d7be2fa3bf4f6be6ef740 Mon Sep 17 00:00:00 2001 From: Gowtham Gopalakrishnan Date: Sun, 25 Nov 2018 12:25:40 +0530 Subject: [PATCH 3/4] Bumping int8 to int16 --- base8.go | 2 +- multibase_test.go | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/base8.go b/base8.go index 3316896..7fce9d8 100644 --- a/base8.go +++ b/base8.go @@ -37,7 +37,7 @@ func decodeOctalString(s string) ([]byte, error) { } for i, dstIndex := 0, 0; i < len(s); i = i + 3 { - value, err := strconv.ParseInt(s[i:i+3], 8, 8) + value, err := strconv.ParseInt(s[i:i+3], 8, 16) if err != nil { return nil, fmt.Errorf("error while conversion: %s", err) } diff --git a/multibase_test.go b/multibase_test.go index 942345b..70bf8c7 100644 --- a/multibase_test.go +++ b/multibase_test.go @@ -2,6 +2,7 @@ package multibase import ( "bytes" + "fmt" "math/rand" "testing" ) @@ -82,8 +83,9 @@ func TestDecode(t *testing.T) { func TestRoundTrip(t *testing.T) { buf := make([]byte, 17) rand.Read(buf) + fmt.Println(buf) - baseList := []Encoding{Identity, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad} + baseList := []Encoding{Identity, Base8, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad} for _, base := range baseList { enc, err := Encode(base, buf) From 626ce16cc2762e8af6a768f738fe5dd56d15adc8 Mon Sep 17 00:00:00 2001 From: Gowtham Gopalakrishnan Date: Tue, 27 Nov 2018 10:28:45 +0530 Subject: [PATCH 4/4] Removed redundant println --- base8.go | 3 ++- multibase_test.go | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/base8.go b/base8.go index 7fce9d8..1adda1a 100644 --- a/base8.go +++ b/base8.go @@ -30,12 +30,13 @@ func octalEncode(dst, src []byte) int { // decodeOctalString takes an octal string and gives byte array of decimals func decodeOctalString(s string) ([]byte, error) { - data := make([]byte, len(s)/3) if len(s)%3 != 0 { return nil, fmt.Errorf("cannot decode multibase: %s", "length should be a multiple of 3") } + data := make([]byte, len(s)/3) + for i, dstIndex := 0, 0; i < len(s); i = i + 3 { value, err := strconv.ParseInt(s[i:i+3], 8, 16) if err != nil { diff --git a/multibase_test.go b/multibase_test.go index 70bf8c7..f72c1c5 100644 --- a/multibase_test.go +++ b/multibase_test.go @@ -2,7 +2,6 @@ package multibase import ( "bytes" - "fmt" "math/rand" "testing" ) @@ -83,7 +82,6 @@ func TestDecode(t *testing.T) { func TestRoundTrip(t *testing.T) { buf := make([]byte, 17) rand.Read(buf) - fmt.Println(buf) baseList := []Encoding{Identity, Base8, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad}