Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
caebba6233 | ||
|
|
83915a874d | ||
|
|
03643c33f5 | ||
|
|
2eb83a994b | ||
|
|
5547437445 | ||
|
|
a0557075ec | ||
|
|
3ea5c212ef | ||
|
|
dfd5076869 | ||
|
|
3f064509b3 | ||
|
|
23774f6467 | ||
|
|
f61622a056 |
@@ -1 +1 @@
|
||||
0.2.5: QmafgXF3u3QSWErQoZ2URmQp5PFG384htoE7J338nS2H7T
|
||||
0.2.7: QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR
|
||||
|
||||
@@ -4,7 +4,7 @@ os:
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.8.3
|
||||
- 1.10.2
|
||||
|
||||
install:
|
||||
- go get -u github.com/whyrusleeping/gx
|
||||
|
||||
53
encoder.go
Normal file
53
encoder.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Encoder is a multibase encoding that is verified to be supported and
|
||||
// supports an Encode method that does not return an error
|
||||
type Encoder struct {
|
||||
enc Encoding
|
||||
}
|
||||
|
||||
// NewEncoder create a new Encoder from an Encoding
|
||||
func NewEncoder(base Encoding) (Encoder, error) {
|
||||
_, ok := EncodingToStr[base]
|
||||
if !ok {
|
||||
return Encoder{-1}, fmt.Errorf("Unsupported multibase encoding: %d", base)
|
||||
}
|
||||
return Encoder{base}, nil
|
||||
}
|
||||
|
||||
// EncoderByName creates an encoder from a string, the string can
|
||||
// either be the multibase name or single character multibase prefix
|
||||
func EncoderByName(str string) (Encoder, error) {
|
||||
var base Encoding
|
||||
ok := true
|
||||
if len(str) == 0 {
|
||||
return Encoder{-1}, fmt.Errorf("Empty multibase encoding")
|
||||
} else if len(str) == 1 {
|
||||
base = Encoding(str[0])
|
||||
_, ok = EncodingToStr[base]
|
||||
} else {
|
||||
base, ok = Encodings[str]
|
||||
}
|
||||
if !ok {
|
||||
return Encoder{-1}, fmt.Errorf("Unsupported multibase encoding: %s", str)
|
||||
}
|
||||
return Encoder{base}, nil
|
||||
}
|
||||
|
||||
func (p Encoder) Encoding() Encoding {
|
||||
return p.enc
|
||||
}
|
||||
|
||||
// Encode encodes the multibase using the given Encoder.
|
||||
func (p Encoder) Encode(data []byte) string {
|
||||
str, err := Encode(p.enc, data)
|
||||
if err != nil {
|
||||
// should not happen
|
||||
panic(err)
|
||||
}
|
||||
return str
|
||||
}
|
||||
33
encoder_test.go
Normal file
33
encoder_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInvalidPrefix(t *testing.T) {
|
||||
_, err := NewEncoder('q')
|
||||
if err == nil {
|
||||
t.Error("expected failure")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefix(t *testing.T) {
|
||||
for str, base := range Encodings {
|
||||
prefix, err := NewEncoder(base)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEncoder(%c) failed: %v", base, err)
|
||||
}
|
||||
str1, err := Encode(base, sampleBytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
str2 := prefix.Encode(sampleBytes)
|
||||
if str1 != str2 {
|
||||
t.Errorf("encoded string mismatch: %s != %s", str1, str2)
|
||||
}
|
||||
_, err = EncoderByName(str)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEncoder(%s) failed: %v", str, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
53
multibase.go
53
multibase.go
@@ -5,14 +5,15 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
b58 "github.com/jbenet/go-base58"
|
||||
b58 "github.com/mr-tron/base58/base58"
|
||||
b32 "github.com/whyrusleeping/base32"
|
||||
)
|
||||
|
||||
// Encoding identifies the type of base-encoding that a multibase is carrying.
|
||||
type Encoding int
|
||||
|
||||
// These are the supported encodings
|
||||
// These are the encodings specified in the standard, not are all
|
||||
// supported yet
|
||||
const (
|
||||
Identity = 0x00
|
||||
Base1 = '1'
|
||||
@@ -37,6 +38,48 @@ const (
|
||||
Base64urlPad = 'U'
|
||||
)
|
||||
|
||||
// Encodigs is a map of the supported encoding, unsupported encoding
|
||||
// specified in standard are left out
|
||||
var Encodings = map[string]Encoding{
|
||||
"identity": 0x00,
|
||||
"base16": 'f',
|
||||
"base16upper": 'F',
|
||||
"base32": 'b',
|
||||
"base32upper": 'B',
|
||||
"base32pad": 'c',
|
||||
"base32padupper": 'C',
|
||||
"base32hex": 'v',
|
||||
"base32hexupper": 'V',
|
||||
"base32hexpad": 't',
|
||||
"base32hexpadupper": 'T',
|
||||
"base58flickr": 'Z',
|
||||
"base58btc": 'z',
|
||||
"base64": 'm',
|
||||
"base64url": 'u',
|
||||
"base64pad": 'M',
|
||||
"base64urlpad": 'U',
|
||||
}
|
||||
|
||||
var EncodingToStr = map[Encoding]string{
|
||||
0x00: "identity",
|
||||
'f': "base16",
|
||||
'F': "base16upper",
|
||||
'b': "base32",
|
||||
'B': "base32upper",
|
||||
'c': "base32pad",
|
||||
'C': "base32padupper",
|
||||
'v': "base32hex",
|
||||
'V': "base32hexupper",
|
||||
't': "base32hexpad",
|
||||
'T': "base32hexpadupper",
|
||||
'Z': "base58flickr",
|
||||
'z': "base58btc",
|
||||
'm': "base64",
|
||||
'u': "base64url",
|
||||
'M': "base64pad",
|
||||
'U': "base64urlpad",
|
||||
}
|
||||
|
||||
// ErrUnsupportedEncoding is returned when the selected encoding is not known or
|
||||
// implemented.
|
||||
var ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
|
||||
@@ -114,9 +157,11 @@ func Decode(data string) (Encoding, []byte, error) {
|
||||
bytes, err := b32.HexEncoding.DecodeString(data[1:])
|
||||
return enc, bytes, err
|
||||
case Base58BTC:
|
||||
return Base58BTC, b58.DecodeAlphabet(data[1:], b58.BTCAlphabet), nil
|
||||
bytes, err := b58.DecodeAlphabet(data[1:], b58.BTCAlphabet)
|
||||
return Base58BTC, bytes, err
|
||||
case Base58Flickr:
|
||||
return Base58Flickr, b58.DecodeAlphabet(data[1:], b58.FlickrAlphabet), nil
|
||||
bytes, err := b58.DecodeAlphabet(data[1:], b58.FlickrAlphabet)
|
||||
return Base58Flickr, bytes, err
|
||||
case Base64pad:
|
||||
bytes, err := base64.StdEncoding.DecodeString(data[1:])
|
||||
return Base64pad, bytes, err
|
||||
|
||||
@@ -6,6 +6,21 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
for s,e := range Encodings {
|
||||
s2 := EncodingToStr[e]
|
||||
if s != s2 {
|
||||
t.Errorf("round trip failed on encoding map: %s != %s", s, s2)
|
||||
}
|
||||
}
|
||||
for e,s := range EncodingToStr {
|
||||
e2 := Encodings[s]
|
||||
if e != e2 {
|
||||
t.Errorf("round trip failed on encoding map: '%c' != '%c'", e, e2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sampleBytes = []byte("Decentralize everything!!!")
|
||||
var encodedSamples = map[Encoding]string{
|
||||
Identity: string(0x00) + "Decentralize everything!!!",
|
||||
@@ -92,3 +107,53 @@ func TestRoundTrip(t *testing.T) {
|
||||
t.Fatal("shouldnt be able to decode empty string")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRoundTrip(b *testing.B) {
|
||||
buf := make([]byte, 32)
|
||||
rand.Read(buf)
|
||||
b.ResetTimer()
|
||||
|
||||
bases := map[string]Encoding{
|
||||
"Identity": Identity,
|
||||
"Base16": Base16,
|
||||
"Base16Upper": Base16Upper,
|
||||
"Base32": Base32,
|
||||
"Base32Upper": Base32Upper,
|
||||
"Base32pad": Base32pad,
|
||||
"Base32padUpper": Base32padUpper,
|
||||
"Base32hex": Base32hex,
|
||||
"Base32hexUpper": Base32hexUpper,
|
||||
"Base32hexPad": Base32hexPad,
|
||||
"Base32hexPadUpper": Base32hexPadUpper,
|
||||
"Base58Flickr": Base58Flickr,
|
||||
"Base58BTC": Base58BTC,
|
||||
"Base64": Base64,
|
||||
"Base64url": Base64url,
|
||||
"Base64pad": Base64pad,
|
||||
"Base64urlPad": Base64urlPad,
|
||||
}
|
||||
|
||||
for name, base := range bases {
|
||||
b.Run(name, func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
enc, err := Encode(base, buf)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
e, out, err := Decode(enc)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
if e != base {
|
||||
b.Fatal("got wrong encoding out")
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf, out) {
|
||||
b.Fatal("input wasnt the same as output", buf, out)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
10
package.json
10
package.json
@@ -14,10 +14,10 @@
|
||||
"version": "0.0.2"
|
||||
},
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf",
|
||||
"name": "go-base58",
|
||||
"version": "0.0.0"
|
||||
"author": "mr-tron",
|
||||
"hash": "QmWFAMPqsEyUX7gDUsRVmMWz59FxSpJ1b2v6bJ1yYzo7jY",
|
||||
"name": "go-base58-fast",
|
||||
"version": "0.1.1"
|
||||
}
|
||||
],
|
||||
"gxVersion": "0.8.0",
|
||||
@@ -25,6 +25,6 @@
|
||||
"license": "",
|
||||
"name": "go-multibase",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.2.5"
|
||||
"version": "0.2.7"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user