Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af68ad2dd7 | ||
|
|
b3341b58aa | ||
|
|
81ab0b304e | ||
|
|
2de609b735 | ||
|
|
67ee8b7fc0 | ||
|
|
d74f4f4a44 | ||
|
|
8cb3334d95 | ||
|
|
7e9a23df22 | ||
|
|
158b7d8e3c |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
*.swp
|
||||
|
||||
multibase-conv/multibase-conv
|
||||
|
||||
@@ -1 +1 @@
|
||||
0.2.3: QmcxkxTVuURV2Ptse8TvkqH5BQDwV62X1x19JqqvbBzwUM
|
||||
0.2.4: Qme4T6BE4sQxg7ZouamF5M7Tx1ZFTqzcns7BkyQPXpoT99
|
||||
|
||||
@@ -14,7 +14,7 @@ install:
|
||||
|
||||
script:
|
||||
- gx-go rewrite
|
||||
- go test -race -coverprofile=unittest.coverprofile -covermode=atomic ./...
|
||||
- go test -race -coverprofile=unittest.coverprofile -covermode=atomic .
|
||||
|
||||
|
||||
after_success:
|
||||
|
||||
21
README.md
21
README.md
@@ -7,17 +7,34 @@
|
||||
[](https://travis-ci.org/multiformats/go-multibase)
|
||||
[](https://codecov.io/github/multiformats/go-multibase?branch=master)
|
||||
|
||||
> Implementation of [multibase](https://github.com/multiformats/multibase) parser in go
|
||||
> Implementation of [multibase](https://github.com/multiformats/multibase) -self identifying base encodings- in Go.
|
||||
|
||||
|
||||
## Install
|
||||
|
||||
`go-multibase` is a standard Go module which can be installed with:
|
||||
|
||||
```sh
|
||||
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
|
||||
|
||||
TODO
|
||||
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
|
||||
|
||||
|
||||
41
multibase-conv/main.go
Normal file
41
multibase-conv/main.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
multibase "github.com/multiformats/go-multibase"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 3 {
|
||||
fmt.Printf("usage: %s <new-base> <multibase-str>...\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var newBase multibase.Encoding
|
||||
if baseParm := os.Args[1]; len(baseParm) != 0 {
|
||||
newBase = multibase.Encoding(baseParm[0])
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, "<new-base> is empty")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
input := os.Args[2:]
|
||||
|
||||
for _, strmbase := range input {
|
||||
_, data, err := multibase.Decode(strmbase)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error while decoding: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
newCid, err := multibase.Encode(newBase, data)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error while encoding: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(newCid)
|
||||
}
|
||||
|
||||
}
|
||||
19
multibase.go
19
multibase.go
@@ -9,8 +9,10 @@ import (
|
||||
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
|
||||
const (
|
||||
Identity = 0x00
|
||||
Base1 = '1'
|
||||
@@ -35,8 +37,13 @@ const (
|
||||
Base64urlPad = 'U'
|
||||
)
|
||||
|
||||
// ErrUnsupportedEncoding is returned when the selected encoding is not known or
|
||||
// implemented.
|
||||
var ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
|
||||
|
||||
// Encode encodes a given byte slice with the selected encoding and returns a
|
||||
// multibase string (<encoding><base-encoded-string>). It will return
|
||||
// an error if the selected base is not known.
|
||||
func Encode(base Encoding, data []byte) (string, error) {
|
||||
switch base {
|
||||
case Identity:
|
||||
@@ -60,11 +67,17 @@ func Encode(base Encoding, data []byte) (string, error) {
|
||||
return string(Base64pad) + base64.StdEncoding.EncodeToString(data), nil
|
||||
case Base64urlPad:
|
||||
return string(Base64urlPad) + base64.URLEncoding.EncodeToString(data), nil
|
||||
case Base64url:
|
||||
return string(Base64url) + base64.RawURLEncoding.EncodeToString(data), nil
|
||||
case Base64:
|
||||
return string(Base64) + base64.RawStdEncoding.EncodeToString(data), nil
|
||||
default:
|
||||
return "", ErrUnsupportedEncoding
|
||||
}
|
||||
}
|
||||
|
||||
// Decode takes a multibase string and decodes into a bytes buffer.
|
||||
// It will return an error if the selected base is not known.
|
||||
func Decode(data string) (Encoding, []byte, error) {
|
||||
if len(data) == 0 {
|
||||
return 0, nil, fmt.Errorf("cannot decode multibase for zero length string")
|
||||
@@ -98,6 +111,12 @@ func Decode(data string) (Encoding, []byte, error) {
|
||||
case Base64urlPad:
|
||||
bytes, err := base64.URLEncoding.DecodeString(data[1:])
|
||||
return Base64urlPad, bytes, err
|
||||
case Base64:
|
||||
bytes, err := base64.RawStdEncoding.DecodeString(data[1:])
|
||||
return Base64, bytes, err
|
||||
case Base64url:
|
||||
bytes, err := base64.RawURLEncoding.DecodeString(data[1:])
|
||||
return Base64url, bytes, err
|
||||
default:
|
||||
return -1, nil, ErrUnsupportedEncoding
|
||||
}
|
||||
|
||||
@@ -25,6 +25,6 @@
|
||||
"license": "",
|
||||
"name": "go-multibase",
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.2.3"
|
||||
"version": "0.2.4"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user