Compare commits
45 Commits
gx/v0.2.6
...
feat/multi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1be98b8c97 | ||
|
|
95cb7074c4 | ||
|
|
2985033078 | ||
|
|
e2260b5ff3 | ||
|
|
158c1deff1 | ||
|
|
ee5c23343b | ||
|
|
aa5d547a81 | ||
|
|
c03399abc2 | ||
|
|
f5fced06c2 | ||
|
|
be9e91119a | ||
|
|
6519131ca4 | ||
|
|
ef04c6a3db | ||
|
|
b84fc17b77 | ||
|
|
f279c85720 | ||
|
|
d63641945d | ||
|
|
79803cc6b5 | ||
|
|
4819336788 | ||
|
|
f25b77813c | ||
|
|
be9178df09 | ||
|
|
f7396abfab | ||
|
|
fca1c65daf | ||
|
|
0a49bd57bb | ||
|
|
5d43951a20 | ||
|
|
4cd2fef284 | ||
|
|
916e8af3d6 | ||
|
|
007b57d388 | ||
|
|
3cdc462d3f | ||
|
|
5b7719f2f5 | ||
|
|
c53ff45d4d | ||
|
|
8ab2e3688b | ||
|
|
bb91b53e56 | ||
|
|
964f55ad40 | ||
|
|
3b3047873d | ||
|
|
2170058ef9 | ||
|
|
ac3d23441b | ||
|
|
ecd5d58562 | ||
|
|
b46f1c99f0 | ||
|
|
5fb339e88a | ||
|
|
caebba6233 | ||
|
|
83915a874d | ||
|
|
03643c33f5 | ||
|
|
2eb83a994b | ||
|
|
5547437445 | ||
|
|
a0557075ec | ||
|
|
3ea5c212ef |
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "spec"]
|
||||||
|
path = spec
|
||||||
|
url = https://github.com/multiformats/multibase.git
|
||||||
@@ -1 +0,0 @@
|
|||||||
0.2.6: QmexBtiTTEwwn42Yi6ouKt6VqzpA6wjJgiW1oh9VfaRrup
|
|
||||||
32
.travis.yml
32
.travis.yml
@@ -1,25 +1,31 @@
|
|||||||
os:
|
os:
|
||||||
- linux
|
- linux
|
||||||
|
|
||||||
language: go
|
language: go
|
||||||
|
|
||||||
go:
|
go:
|
||||||
- 1.8.3
|
- 1.11.x
|
||||||
|
- 1.15.x
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- GOTFLAGS="-race"
|
||||||
|
matrix:
|
||||||
|
- BUILD_DEPTYPE=gomod
|
||||||
|
|
||||||
|
|
||||||
|
# disable travis install
|
||||||
install:
|
install:
|
||||||
- go get -u github.com/whyrusleeping/gx
|
- true
|
||||||
- go get -u github.com/whyrusleeping/gx-go
|
|
||||||
- gx install
|
|
||||||
|
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- gx-go rewrite
|
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
|
||||||
- go test -race -coverprofile=unittest.coverprofile -covermode=atomic .
|
|
||||||
|
|
||||||
|
|
||||||
after_success:
|
|
||||||
- bash <(curl -s https://codecov.io/bash) -f unittest.coverprofile -F unittest
|
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
- $GOPATH/src/gx
|
- $GOPATH/pkg/mod
|
||||||
|
- /home/travis/.cache/go-build
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email: false
|
||||||
|
|||||||
7
Makefile
Normal file
7
Makefile
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
test: deps
|
||||||
|
go test -count=1 -race -v ./...
|
||||||
|
|
||||||
|
export IPFS_API ?= v04x.ipfs.io
|
||||||
|
|
||||||
|
deps:
|
||||||
|
go get -t ./...
|
||||||
22
README.md
22
README.md
@@ -18,28 +18,6 @@
|
|||||||
go get github.com/multiformats/go-multibase
|
go get github.com/multiformats/go-multibase
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that `go-multibase` is packaged with Gx, so it is recommended to use Gx to install and use it (see Usage section).
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
This module is packaged with [Gx](https://github.com/whyrusleeping/gx). In order to use it in your own project it is recommended that you:
|
|
||||||
|
|
||||||
```sh
|
|
||||||
go get -u github.com/whyrusleeping/gx
|
|
||||||
go get -u github.com/whyrusleeping/gx-go
|
|
||||||
cd <your-project-repository>
|
|
||||||
gx init
|
|
||||||
gx import github.com/multiformats/go-multibase
|
|
||||||
gx install --global
|
|
||||||
gx-go --rewrite
|
|
||||||
```
|
|
||||||
|
|
||||||
Please check [Gx](https://github.com/whyrusleeping/gx) and [Gx-go](https://github.com/whyrusleeping/gx-go) documentation for more information.
|
|
||||||
|
|
||||||
## Maintainers
|
|
||||||
|
|
||||||
Captain: [@whyrusleeping](https://github.com/whyrusleeping).
|
|
||||||
|
|
||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multibase/issues).
|
Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multibase/issues).
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ func hexEncodeToStringUpper(src []byte) string {
|
|||||||
return string(dst)
|
return string(dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
var hextableUpper = [16]byte{
|
var hexTableUppers = [16]byte{
|
||||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||||
'A', 'B', 'C', 'D', 'E', 'F',
|
'A', 'B', 'C', 'D', 'E', 'F',
|
||||||
}
|
}
|
||||||
|
|
||||||
func hexEncodeUpper(dst, src []byte) int {
|
func hexEncodeUpper(dst, src []byte) int {
|
||||||
for i, v := range src {
|
for i, v := range src {
|
||||||
dst[i*2] = hextableUpper[v>>4]
|
dst[i*2] = hexTableUppers[v>>4]
|
||||||
dst[i*2+1] = hextableUpper[v&0x0f]
|
dst[i*2+1] = hexTableUppers[v&0x0f]
|
||||||
}
|
}
|
||||||
|
|
||||||
return len(src) * 2
|
return len(src) * 2
|
||||||
|
|||||||
52
base2.go
Normal file
52
base2.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package multibase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// binaryEncodeToString takes an array of bytes and returns
|
||||||
|
// multibase binary representation
|
||||||
|
func binaryEncodeToString(src []byte) string {
|
||||||
|
dst := make([]byte, len(src)*8)
|
||||||
|
encodeBinary(dst, src)
|
||||||
|
return string(dst)
|
||||||
|
}
|
||||||
|
|
||||||
|
// encodeBinary takes the src and dst bytes and converts each
|
||||||
|
// byte to their binary rep using power reduction method
|
||||||
|
func encodeBinary(dst []byte, src []byte) {
|
||||||
|
for i, b := range src {
|
||||||
|
for j := 0; j < 8; j++ {
|
||||||
|
if b&(1<<uint(7-j)) == 0 {
|
||||||
|
dst[i*8+j] = '0'
|
||||||
|
} else {
|
||||||
|
dst[i*8+j] = '1'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// decodeBinaryString takes multibase binary representation
|
||||||
|
// and returns a byte array
|
||||||
|
func decodeBinaryString(s string) ([]byte, error) {
|
||||||
|
if len(s)&7 != 0 {
|
||||||
|
// prepend the padding
|
||||||
|
s = strings.Repeat("0", 8-len(s)&7) + s
|
||||||
|
}
|
||||||
|
|
||||||
|
data := make([]byte, len(s)>>3)
|
||||||
|
|
||||||
|
for i, dstIndex := 0, 0; i < len(s); i = i + 8 {
|
||||||
|
value, err := strconv.ParseInt(s[i:i+8], 2, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error while conversion: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data[dstIndex] = byte(value)
|
||||||
|
dstIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package multibase
|
package multibase
|
||||||
|
|
||||||
import (
|
import (
|
||||||
b32 "github.com/whyrusleeping/base32"
|
b32 "github.com/multiformats/go-base32"
|
||||||
)
|
)
|
||||||
|
|
||||||
var base32StdLowerPad = b32.NewEncodingCI("abcdefghijklmnopqrstuvwxyz234567")
|
var base32StdLowerPad = b32.NewEncodingCI("abcdefghijklmnopqrstuvwxyz234567")
|
||||||
|
|||||||
63
encoder.go
Normal file
63
encoder.go
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// MustNewEncoder is like NewEncoder but will panic if the encoding is
|
||||||
|
// invalid.
|
||||||
|
func MustNewEncoder(base Encoding) Encoder {
|
||||||
|
_, ok := EncodingToStr[base]
|
||||||
|
if !ok {
|
||||||
|
panic("Unsupported multibase encoding")
|
||||||
|
}
|
||||||
|
return Encoder{base}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
51
encoder_test.go
Normal file
51
encoder_test.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package multibase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInvalidCode(t *testing.T) {
|
||||||
|
_, err := NewEncoder('q')
|
||||||
|
if err == nil {
|
||||||
|
t.Error("expected failure")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidName(t *testing.T) {
|
||||||
|
values := []string{"invalid", "", "q"}
|
||||||
|
for _, val := range values {
|
||||||
|
_, err := EncoderByName(val)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("EncoderByName(%v) expected failure", val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEncoder(t *testing.T) {
|
||||||
|
for name, code := range Encodings {
|
||||||
|
encoder, err := NewEncoder(code)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Make sure the MustNewEncoder doesn't panic
|
||||||
|
MustNewEncoder(code)
|
||||||
|
str, err := Encode(code, sampleBytes)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
str2 := encoder.Encode(sampleBytes)
|
||||||
|
if str != str2 {
|
||||||
|
t.Errorf("encoded string mismatch: %s != %s", str, str2)
|
||||||
|
}
|
||||||
|
_, err = EncoderByName(name)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("EncoderByName(%s) failed: %v", name, err)
|
||||||
|
}
|
||||||
|
// Test that an encoder can be created from the single letter
|
||||||
|
// prefix
|
||||||
|
_, err = EncoderByName(str[0:1])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("EncoderByName(%s) failed: %v", str[0:1], err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
10
go.mod
Normal file
10
go.mod
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
module github.com/multiformats/go-multibase
|
||||||
|
|
||||||
|
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
|
||||||
|
github.com/urfave/cli/v2 v2.3.0
|
||||||
|
)
|
||||||
19
go.sum
Normal file
19
go.sum
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||||
|
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=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
|
||||||
|
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||||
|
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||||
|
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
||||||
|
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
@@ -14,8 +14,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var newBase multibase.Encoding
|
var newBase multibase.Encoding
|
||||||
if baseParm := os.Args[1]; len(baseParm) != 0 {
|
if baseParam := os.Args[1]; len(baseParam) != 0 {
|
||||||
newBase = multibase.Encoding(baseParm[0])
|
newBase = multibase.Encoding(baseParam[0])
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(os.Stderr, "<new-base> is empty")
|
fmt.Fprintln(os.Stderr, "<new-base> is empty")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
58
multibase.go
58
multibase.go
@@ -6,16 +6,17 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
b58 "github.com/mr-tron/base58/base58"
|
b58 "github.com/mr-tron/base58/base58"
|
||||||
b32 "github.com/whyrusleeping/base32"
|
b32 "github.com/multiformats/go-base32"
|
||||||
|
b36 "github.com/multiformats/go-base36"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Encoding identifies the type of base-encoding that a multibase is carrying.
|
// Encoding identifies the type of base-encoding that a multibase is carrying.
|
||||||
type Encoding int
|
type Encoding int
|
||||||
|
|
||||||
// These are the supported encodings
|
// These are the encodings specified in the standard, not are all
|
||||||
|
// supported yet
|
||||||
const (
|
const (
|
||||||
Identity = 0x00
|
Identity = 0x00
|
||||||
Base1 = '1'
|
|
||||||
Base2 = '0'
|
Base2 = '0'
|
||||||
Base8 = '7'
|
Base8 = '7'
|
||||||
Base10 = '9'
|
Base10 = '9'
|
||||||
@@ -29,14 +30,49 @@ const (
|
|||||||
Base32hexUpper = 'V'
|
Base32hexUpper = 'V'
|
||||||
Base32hexPad = 't'
|
Base32hexPad = 't'
|
||||||
Base32hexPadUpper = 'T'
|
Base32hexPadUpper = 'T'
|
||||||
Base58Flickr = 'Z'
|
Base36 = 'k'
|
||||||
|
Base36Upper = 'K'
|
||||||
Base58BTC = 'z'
|
Base58BTC = 'z'
|
||||||
|
Base58Flickr = 'Z'
|
||||||
Base64 = 'm'
|
Base64 = 'm'
|
||||||
Base64url = 'u'
|
Base64url = 'u'
|
||||||
Base64pad = 'M'
|
Base64pad = 'M'
|
||||||
Base64urlPad = 'U'
|
Base64urlPad = 'U'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EncodingToStr is a map of the supported encoding, unsupported encoding
|
||||||
|
// specified in standard are left out
|
||||||
|
var EncodingToStr = map[Encoding]string{
|
||||||
|
0x00: "identity",
|
||||||
|
'0': "base2",
|
||||||
|
'f': "base16",
|
||||||
|
'F': "base16upper",
|
||||||
|
'b': "base32",
|
||||||
|
'B': "base32upper",
|
||||||
|
'c': "base32pad",
|
||||||
|
'C': "base32padupper",
|
||||||
|
'v': "base32hex",
|
||||||
|
'V': "base32hexupper",
|
||||||
|
't': "base32hexpad",
|
||||||
|
'T': "base32hexpadupper",
|
||||||
|
'k': "base36",
|
||||||
|
'K': "base36upper",
|
||||||
|
'z': "base58btc",
|
||||||
|
'Z': "base58flickr",
|
||||||
|
'm': "base64",
|
||||||
|
'u': "base64url",
|
||||||
|
'M': "base64pad",
|
||||||
|
'U': "base64urlpad",
|
||||||
|
}
|
||||||
|
|
||||||
|
var Encodings = map[string]Encoding{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
for e, n := range EncodingToStr {
|
||||||
|
Encodings[n] = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ErrUnsupportedEncoding is returned when the selected encoding is not known or
|
// ErrUnsupportedEncoding is returned when the selected encoding is not known or
|
||||||
// implemented.
|
// implemented.
|
||||||
var ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
|
var ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
|
||||||
@@ -48,7 +84,9 @@ func Encode(base Encoding, data []byte) (string, error) {
|
|||||||
switch base {
|
switch base {
|
||||||
case Identity:
|
case Identity:
|
||||||
// 0x00 inside a string is OK in golang and causes no problems with the length calculation.
|
// 0x00 inside a string is OK in golang and causes no problems with the length calculation.
|
||||||
return string(Identity) + string(data), nil
|
return string(rune(Identity)) + string(data), nil
|
||||||
|
case Base2:
|
||||||
|
return string(Base2) + binaryEncodeToString(data), nil
|
||||||
case Base16:
|
case Base16:
|
||||||
return string(Base16) + hex.EncodeToString(data), nil
|
return string(Base16) + hex.EncodeToString(data), nil
|
||||||
case Base16Upper:
|
case Base16Upper:
|
||||||
@@ -69,6 +107,10 @@ func Encode(base Encoding, data []byte) (string, error) {
|
|||||||
return string(Base32hexPad) + base32HexLowerPad.EncodeToString(data), nil
|
return string(Base32hexPad) + base32HexLowerPad.EncodeToString(data), nil
|
||||||
case Base32hexPadUpper:
|
case Base32hexPadUpper:
|
||||||
return string(Base32hexPadUpper) + base32HexUpperPad.EncodeToString(data), nil
|
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:
|
case Base58BTC:
|
||||||
return string(Base58BTC) + b58.EncodeAlphabet(data, b58.BTCAlphabet), nil
|
return string(Base58BTC) + b58.EncodeAlphabet(data, b58.BTCAlphabet), nil
|
||||||
case Base58Flickr:
|
case Base58Flickr:
|
||||||
@@ -98,6 +140,9 @@ func Decode(data string) (Encoding, []byte, error) {
|
|||||||
switch enc {
|
switch enc {
|
||||||
case Identity:
|
case Identity:
|
||||||
return Identity, []byte(data[1:]), nil
|
return Identity, []byte(data[1:]), nil
|
||||||
|
case Base2:
|
||||||
|
bytes, err := decodeBinaryString(data[1:])
|
||||||
|
return enc, bytes, err
|
||||||
case Base16, Base16Upper:
|
case Base16, Base16Upper:
|
||||||
bytes, err := hex.DecodeString(data[1:])
|
bytes, err := hex.DecodeString(data[1:])
|
||||||
return enc, bytes, err
|
return enc, bytes, err
|
||||||
@@ -113,6 +158,9 @@ func Decode(data string) (Encoding, []byte, error) {
|
|||||||
case Base32hexPad, Base32hexPadUpper:
|
case Base32hexPad, Base32hexPadUpper:
|
||||||
bytes, err := b32.HexEncoding.DecodeString(data[1:])
|
bytes, err := b32.HexEncoding.DecodeString(data[1:])
|
||||||
return enc, bytes, err
|
return enc, bytes, err
|
||||||
|
case Base36, Base36Upper:
|
||||||
|
bytes, err := b36.DecodeString(data[1:])
|
||||||
|
return enc, bytes, err
|
||||||
case Base58BTC:
|
case Base58BTC:
|
||||||
bytes, err := b58.DecodeAlphabet(data[1:], b58.BTCAlphabet)
|
bytes, err := b58.DecodeAlphabet(data[1:], b58.BTCAlphabet)
|
||||||
return Base58BTC, bytes, err
|
return Base58BTC, bytes, err
|
||||||
|
|||||||
115
multibase/main.go
Normal file
115
multibase/main.go
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/multiformats/go-multibase"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := &cli.App{
|
||||||
|
Name: "multibase",
|
||||||
|
Usage: "base encoding and transcoding tool",
|
||||||
|
Commands: []*cli.Command{
|
||||||
|
{
|
||||||
|
Name: "encode",
|
||||||
|
ArgsUsage: "<base>",
|
||||||
|
Usage: "encode data in multibase",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.PathFlag{
|
||||||
|
Name: "input",
|
||||||
|
Aliases: []string{"i"},
|
||||||
|
Usage: "the file that should be encoded",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: func(context *cli.Context) error {
|
||||||
|
if !context.Args().Present() || context.NArg() > 2 {
|
||||||
|
return cli.ShowCommandHelp(context, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := context.Path("input")
|
||||||
|
if (p == "" && context.NArg() != 2) || (p != "" && context.NArg() != 1) {
|
||||||
|
return cli.ShowCommandHelp(context, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
base, err := multibase.EncoderByName(context.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if p != "" {
|
||||||
|
fileData, err := ioutil.ReadFile(p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(base.Encode(fileData))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(base.Encode([]byte(context.Args().Get(1))))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "decode",
|
||||||
|
ArgsUsage: "<data>",
|
||||||
|
Usage: "encode data in multibase",
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
&cli.PathFlag{
|
||||||
|
Name: "output",
|
||||||
|
Aliases: []string{"o"},
|
||||||
|
Usage: "output decoded data to a file",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Action: func(context *cli.Context) error {
|
||||||
|
if context.NArg() != 1 {
|
||||||
|
return cli.ShowCommandHelp(context, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, data, err := multibase.Decode(context.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p := context.Path("output")
|
||||||
|
if p == "" {
|
||||||
|
fmt.Printf(string(data))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return ioutil.WriteFile(p, data, os.ModePerm)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "transcode",
|
||||||
|
ArgsUsage: "<new-base> <data>",
|
||||||
|
Usage: "transcode multibase data",
|
||||||
|
Action: func(context *cli.Context) error {
|
||||||
|
if context.NArg() != 2 {
|
||||||
|
return cli.ShowCommandHelp(context, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
newbase, err := multibase.EncoderByName(context.Args().Get(0))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, data, err := multibase.Decode(context.Args().Get(1))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(newbase.Encode(data))
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
err := app.Run(os.Args)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,12 +3,29 @@ package multibase
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"sort"
|
||||||
"testing"
|
"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 sampleBytes = []byte("Decentralize everything!!!")
|
||||||
var encodedSamples = map[Encoding]string{
|
var encodedSamples = map[Encoding]string{
|
||||||
Identity: string(0x00) + "Decentralize everything!!!",
|
Identity: string(rune(0x00)) + "Decentralize everything!!!",
|
||||||
|
Base2: "00100010001100101011000110110010101101110011101000111001001100001011011000110100101111010011001010010000001100101011101100110010101110010011110010111010001101000011010010110111001100111001000010010000100100001",
|
||||||
Base16: "f446563656e7472616c697a652065766572797468696e67212121",
|
Base16: "f446563656e7472616c697a652065766572797468696e67212121",
|
||||||
Base16Upper: "F446563656E7472616C697A652065766572797468696E67212121",
|
Base16Upper: "F446563656E7472616C697A652065766572797468696E67212121",
|
||||||
Base32: "birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijbee",
|
Base32: "birswgzloorzgc3djpjssazlwmvzhs5dinfxgoijbee",
|
||||||
@@ -19,7 +36,12 @@ var encodedSamples = map[Encoding]string{
|
|||||||
Base32hexUpper: "V8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144",
|
Base32hexUpper: "V8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144",
|
||||||
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
|
Base32hexPad: "t8him6pbeehp62r39f9ii0pbmclp7it38d5n6e89144======",
|
||||||
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
|
Base32hexPadUpper: "T8HIM6PBEEHP62R39F9II0PBMCLP7IT38D5N6E89144======",
|
||||||
|
Base36: "km552ng4dabi4neu1oo8l4i5mndwmpc3mkukwtxy9",
|
||||||
|
Base36Upper: "KM552NG4DABI4NEU1OO8L4I5MNDWMPC3MKUKWTXY9",
|
||||||
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
|
Base58BTC: "z36UQrhJq9fNDS7DiAHM9YXqDHMPfr4EMArvt",
|
||||||
|
Base58Flickr: "Z36tpRGiQ9Endr7dHahm9xwQdhmoER4emaRVT",
|
||||||
|
Base64: "mRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||||
|
Base64url: "uRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE",
|
||||||
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||||
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchISE=",
|
||||||
}
|
}
|
||||||
@@ -31,7 +53,7 @@ func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string)
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Errorf("encoding failed for %c (%d), expected: %s, got: %s", encoding, encoding, expected, actual)
|
t.Errorf("encoding failed for %c (%d / %s), expected: %s, got: %s", encoding, encoding, EncodingToStr[encoding], expected, actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,78 +72,178 @@ func testDecode(t *testing.T, expectedEncoding Encoding, expectedBytes []byte, d
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEncode(t *testing.T) {
|
func TestEncode(t *testing.T) {
|
||||||
for encoding, data := range encodedSamples {
|
for encoding := range EncodingToStr {
|
||||||
testEncode(t, encoding, sampleBytes, data)
|
testEncode(t, encoding, sampleBytes, encodedSamples[encoding])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDecode(t *testing.T) {
|
func TestDecode(t *testing.T) {
|
||||||
for encoding, data := range encodedSamples {
|
for encoding := range EncodingToStr {
|
||||||
testDecode(t, encoding, sampleBytes, data)
|
testDecode(t, encoding, sampleBytes, encodedSamples[encoding])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRoundTrip(t *testing.T) {
|
func TestRoundTrip(t *testing.T) {
|
||||||
buf := make([]byte, 17)
|
|
||||||
rand.Read(buf)
|
|
||||||
|
|
||||||
baseList := []Encoding{Identity, Base16, Base32, Base32hex, Base32pad, Base32hexPad, Base58BTC, Base58Flickr, Base64pad, Base64urlPad}
|
for base := range EncodingToStr {
|
||||||
|
if int(base) == 0 {
|
||||||
for _, base := range baseList {
|
// skip identity: any byte goes there
|
||||||
enc, err := Encode(base, buf)
|
continue
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
e, out, err := Decode(enc)
|
_, _, err := Decode(string(rune(base)) + "\u00A0")
|
||||||
if err != nil {
|
if err == nil {
|
||||||
t.Fatal(err)
|
t.Fatal(EncodingToStr[base] + " decode should fail on low-unicode")
|
||||||
}
|
}
|
||||||
|
|
||||||
if e != base {
|
_, _, err = Decode(string(rune(base)) + "\u1F4A8")
|
||||||
t.Fatal("got wrong encoding out")
|
if err == nil {
|
||||||
|
t.Fatal(EncodingToStr[base] + " decode should fail on emoji")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(buf, out) {
|
_, _, err = Decode(string(rune(base)) + "!")
|
||||||
t.Fatal("input wasnt the same as output", buf, out)
|
if err == nil {
|
||||||
|
t.Fatal(EncodingToStr[base] + " decode should fail on punctuation")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, err = Decode(string(rune(base)) + "\xA0")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal(EncodingToStr[base] + " decode should fail on high-latin1")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
buf := make([]byte, 137+16) // sufficiently large prime number of bytes + another 16 to test leading 0s
|
||||||
|
rand.Read(buf[16:])
|
||||||
|
|
||||||
|
for base := range EncodingToStr {
|
||||||
|
|
||||||
|
// test roundtrip from the full zero-prefixed buffer down to a single byte
|
||||||
|
for i := 0; i < len(buf); i++ {
|
||||||
|
|
||||||
|
// use a copy to verify we are not overwriting the supplied buffer
|
||||||
|
newBuf := make([]byte, len(buf)-i)
|
||||||
|
copy(newBuf, buf[i:])
|
||||||
|
|
||||||
|
enc, err := Encode(base, newBuf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e, out, err := Decode(enc)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if e != base {
|
||||||
|
t.Fatal("got wrong encoding out")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(newBuf, buf[i:]) {
|
||||||
|
t.Fatal("the provided buffer was modified", buf[i:], out)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(buf[i:], out) {
|
||||||
|
t.Fatal("input wasnt the same as output", buf[i:], out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// When we have 3 leading zeroes, do a few extra tests
|
||||||
|
// ( choice of leading zeroes is arbitrary - just cutting down on test permutations )
|
||||||
|
|
||||||
|
if i == 13 {
|
||||||
|
|
||||||
|
// if this is a case-insensitive codec semi-randomly swap case in enc and try again
|
||||||
|
name := EncodingToStr[base]
|
||||||
|
if name[len(name)-5:] == "upper" || Encodings[name+"upper"] > 0 {
|
||||||
|
caseTamperedEnc := []byte(enc)
|
||||||
|
|
||||||
|
for _, j := range []int{3, 5, 8, 13, 21, 23, 29, 47, 52} {
|
||||||
|
if caseTamperedEnc[j] >= 65 && caseTamperedEnc[j] <= 90 {
|
||||||
|
caseTamperedEnc[j] += 32
|
||||||
|
} else if caseTamperedEnc[j] >= 97 && caseTamperedEnc[j] <= 122 {
|
||||||
|
caseTamperedEnc[j] -= 32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
e, out, err := Decode(string(caseTamperedEnc))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if e != base {
|
||||||
|
t.Fatal("got wrong encoding out")
|
||||||
|
}
|
||||||
|
if !bytes.Equal(buf[i:], out) {
|
||||||
|
t.Fatal("input wasn't the same as output", buf[i:], out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test that nothing overflows
|
||||||
|
maxValueBuf := make([]byte, 131)
|
||||||
|
for i := 0; i < len(maxValueBuf); i++ {
|
||||||
|
maxValueBuf[i] = 0xFF
|
||||||
|
}
|
||||||
|
|
||||||
|
for base := range EncodingToStr {
|
||||||
|
|
||||||
|
// test roundtrip from the complete buffer down to a single byte
|
||||||
|
for i := 0; i < len(maxValueBuf); i++ {
|
||||||
|
|
||||||
|
enc, err := Encode(base, maxValueBuf[i:])
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
e, out, err := Decode(enc)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if e != base {
|
||||||
|
t.Fatal("got wrong encoding out")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !bytes.Equal(maxValueBuf[i:], out) {
|
||||||
|
t.Fatal("input wasn't the same as output", maxValueBuf[i:], out)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err := Decode("")
|
_, _, err := Decode("")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("shouldnt be able to decode empty string")
|
t.Fatal("shouldn't be able to decode empty string")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var benchmarkBuf [36]byte // typical CID size
|
||||||
|
var benchmarkCodecs []string
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rand.Read(benchmarkBuf[:])
|
||||||
|
|
||||||
|
benchmarkCodecs = make([]string, 0, len(Encodings))
|
||||||
|
for n := range Encodings {
|
||||||
|
|
||||||
|
// // Only bench b36 and b58
|
||||||
|
// if len(n) < 6 || (n[4:6] != "36" && n[4:6] != "58") {
|
||||||
|
// continue
|
||||||
|
// }
|
||||||
|
|
||||||
|
benchmarkCodecs = append(benchmarkCodecs, n)
|
||||||
|
}
|
||||||
|
sort.Strings(benchmarkCodecs)
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkRoundTrip(b *testing.B) {
|
func BenchmarkRoundTrip(b *testing.B) {
|
||||||
buf := make([]byte, 32)
|
|
||||||
rand.Read(buf)
|
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
|
||||||
bases := map[string]Encoding{
|
for _, name := range benchmarkCodecs {
|
||||||
"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) {
|
b.Run(name, func(b *testing.B) {
|
||||||
|
base := Encodings[name]
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
enc, err := Encode(base, buf)
|
enc, err := Encode(base, benchmarkBuf[:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -135,8 +257,40 @@ func BenchmarkRoundTrip(b *testing.B) {
|
|||||||
b.Fatal("got wrong encoding out")
|
b.Fatal("got wrong encoding out")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(buf, out) {
|
if !bytes.Equal(benchmarkBuf[:], out) {
|
||||||
b.Fatal("input wasnt the same as output", buf, out)
|
b.Fatal("input wasnt the same as output", benchmarkBuf, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkEncode(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for _, name := range benchmarkCodecs {
|
||||||
|
b.Run(name, func(b *testing.B) {
|
||||||
|
base := Encodings[name]
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, err := Encode(base, benchmarkBuf[:])
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkDecode(b *testing.B) {
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for _, name := range benchmarkCodecs {
|
||||||
|
b.Run(name, func(b *testing.B) {
|
||||||
|
enc, _ := Encode(Encodings[name], benchmarkBuf[:])
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
_, _, err := Decode(enc)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
22
package.json
22
package.json
@@ -3,28 +3,8 @@
|
|||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/multiformats/go-multibase"
|
"url": "https://github.com/multiformats/go-multibase"
|
||||||
},
|
},
|
||||||
"gx": {
|
|
||||||
"dvcsimport": "github.com/multiformats/go-multibase"
|
|
||||||
},
|
|
||||||
"gxDependencies": [
|
|
||||||
{
|
|
||||||
"author": "whyrusleeping",
|
|
||||||
"hash": "QmfVj3x4D6Jkq9SEoi5n2NmoUomLwoeiwnYz2KQa15wRw6",
|
|
||||||
"name": "base32",
|
|
||||||
"version": "0.0.2"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"author": "mr-tron",
|
|
||||||
"hash": "QmWFAMPqsEyUX7gDUsRVmMWz59FxSpJ1b2v6bJ1yYzo7jY",
|
|
||||||
"name": "go-base58-fast",
|
|
||||||
"version": "0.1.1"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"gxVersion": "0.8.0",
|
|
||||||
"language": "go",
|
"language": "go",
|
||||||
"license": "",
|
"license": "",
|
||||||
"name": "go-multibase",
|
"name": "go-multibase",
|
||||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
"version": "0.3.0"
|
||||||
"version": "0.2.6"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
1
spec
Submodule
1
spec
Submodule
Submodule spec added at 3806057e6f
147
spec_test.go
Normal file
147
spec_test.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package multibase
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/csv"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"unicode/utf8"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSpec(t *testing.T) {
|
||||||
|
file, err := os.Open("spec/multibase.csv")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
reader := csv.NewReader(file)
|
||||||
|
reader.LazyQuotes = false
|
||||||
|
reader.FieldsPerRecord = 4
|
||||||
|
reader.TrimLeadingSpace = true
|
||||||
|
|
||||||
|
values, err := reader.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
expectedEncodings := make(map[Encoding]string, len(values)-1)
|
||||||
|
for _, v := range values[1:] {
|
||||||
|
encoding := v[0]
|
||||||
|
codeStr := v[1]
|
||||||
|
|
||||||
|
var code Encoding
|
||||||
|
if strings.HasPrefix(codeStr, "0x") {
|
||||||
|
i, err := strconv.ParseUint(codeStr[2:], 16, 64)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("invalid multibase byte %q", codeStr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
code = Encoding(i)
|
||||||
|
} else {
|
||||||
|
codeRune, length := utf8.DecodeRuneInString(codeStr)
|
||||||
|
if code == utf8.RuneError {
|
||||||
|
t.Errorf("multibase %q wasn't valid utf8", codeStr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if length != len(codeStr) {
|
||||||
|
t.Errorf("multibase %q wasn't a single character", codeStr)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
code = Encoding(codeRune)
|
||||||
|
}
|
||||||
|
expectedEncodings[code] = encoding
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, enc := range Encodings {
|
||||||
|
expectedName, ok := expectedEncodings[enc]
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("encoding %q (%c) not defined in the spec", name, enc)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if expectedName != name {
|
||||||
|
t.Errorf("encoding %q (%c) has unexpected name %q", expectedName, enc, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func TestSpecVectors(t *testing.T) {
|
||||||
|
files, err := filepath.Glob("spec/tests/test[0-9]*.csv")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, fname := range files {
|
||||||
|
t.Run(fname, func(t *testing.T) {
|
||||||
|
file, err := os.Open(fname)
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
reader := csv.NewReader(file)
|
||||||
|
reader.LazyQuotes = false
|
||||||
|
reader.FieldsPerRecord = 2
|
||||||
|
reader.TrimLeadingSpace = true
|
||||||
|
|
||||||
|
values, err := reader.ReadAll()
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
if len(values) == 0 {
|
||||||
|
t.Error("no test values")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
header := values[0]
|
||||||
|
|
||||||
|
var decodeOnly bool
|
||||||
|
switch header[0] {
|
||||||
|
case "encoding":
|
||||||
|
case "non-canonical encoding":
|
||||||
|
decodeOnly = true
|
||||||
|
default:
|
||||||
|
t.Errorf("invalid test spec %q", fname)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
testValue, err := strconv.Unquote("\"" + header[1] + "\"")
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to unquote testcase:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, testCase := range values[1:] {
|
||||||
|
encodingName := testCase[0]
|
||||||
|
expected := testCase[1]
|
||||||
|
|
||||||
|
t.Run(encodingName, func(t *testing.T) {
|
||||||
|
encoder, err := EncoderByName(encodingName)
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("skipping %s: not supported", encodingName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !decodeOnly {
|
||||||
|
t.Logf("encoding %q with %s", testValue, encodingName)
|
||||||
|
actual := encoder.Encode([]byte(testValue))
|
||||||
|
if expected != actual {
|
||||||
|
t.Errorf("expected %q, got %q", expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Logf("decoding %q", expected)
|
||||||
|
encoding, decoded, err := Decode(expected)
|
||||||
|
if err != nil {
|
||||||
|
t.Error("failed to decode:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
expectedEncoding := Encodings[encodingName]
|
||||||
|
if encoding != expectedEncoding {
|
||||||
|
t.Errorf("expected encoding to be %c, got %c", expectedEncoding, encoding)
|
||||||
|
}
|
||||||
|
if string(decoded) != testValue {
|
||||||
|
t.Errorf("failed to decode %q to %q, got %q", expected, testValue, string(decoded))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user