10 Commits

Author SHA1 Message Date
Jeromy
af68ad2dd7 gx publish 0.2.4 2017-05-01 17:33:12 -07:00
Jeromy
b3341b58aa Add in base64 raw encodings 2017-04-19 18:02:57 -07:00
Jakub Sztandera
81ab0b304e Merge pull request #13 from multiformats/kevina/multibase-conv
Add simple utility to convert from one base to another.
2017-03-18 00:22:41 +01:00
Jakub Sztandera
2de609b735 Merge pull request #15 from multiformats/docs-improvements
README/golint: improve readme and make golint happy
2017-03-17 18:57:48 +01:00
Jakub Sztandera
67ee8b7fc0 address CR 2017-03-17 18:47:36 +01:00
Hector Sanjuan
d74f4f4a44 README/golint: improve readme and make golint happy
License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
2017-03-17 16:34:42 +01:00
Jakub Sztandera
8cb3334d95 Add streaming to multibase-conv 2017-03-17 15:39:31 +01:00
Kevin Atkinson
7e9a23df22 Add simple utility to convert from one base to another. 2017-03-17 15:35:26 +01:00
Jakub Sztandera
158b7d8e3c ci: fixup coverage collection 2017-03-17 15:35:26 +01:00
Jeromy
a20e296ca7 gx publish 0.2.3 2017-02-02 18:59:19 -08:00
8 changed files with 86 additions and 7 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
*.swp
multibase-conv/multibase-conv

View File

@@ -1 +1 @@
0.2.2: QmXzWFN4iLdX1Vq8Sc13mET7aXsHkTyJoMbaJJD3NGRhiJ
0.2.4: Qme4T6BE4sQxg7ZouamF5M7Tx1ZFTqzcns7BkyQPXpoT99

View File

@@ -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:

View File

@@ -7,17 +7,34 @@
[![Travis CI](https://img.shields.io/travis/multiformats/go-multibase.svg?style=flat-square&branch=master)](https://travis-ci.org/multiformats/go-multibase)
[![codecov.io](https://img.shields.io/codecov/c/github/multiformats/go-multibase.svg?style=flat-square&branch=master)](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
View 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)
}
}

View File

@@ -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
}

View File

@@ -9,9 +9,9 @@
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "Qmb1DA2A9LS2wR4FFweB4uEDomFsdmnw1VLawLE1yQzudj",
"hash": "QmZvZSVtvxak4dcTkhsQhqd1SQ6rg5UzaSTu62WfWKjj93",
"name": "base32",
"version": "0.0.0"
"version": "0.0.1"
},
{
"author": "whyrusleeping",
@@ -24,6 +24,7 @@
"language": "go",
"license": "",
"name": "go-multibase",
"version": "0.2.2"
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.2.4"
}