Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a20e296ca7 | ||
|
|
a723c120b9 | ||
|
|
4cedb5a79e | ||
|
|
6a1554bbad | ||
|
|
9a56b2b802 | ||
|
|
374a93370a | ||
|
|
c264cdd0ae | ||
|
|
e11767a490 | ||
|
|
c4f49cd356 | ||
|
|
3b16e9048c | ||
|
|
ee1176c739 | ||
|
|
8e28da0e2a | ||
|
|
3d3cb9341e | ||
|
|
64b00b0f1a | ||
|
|
ed7d4b3b12 | ||
|
|
2c9be8ec36 | ||
|
|
528883f051 | ||
|
|
2fd4b5b828 | ||
|
|
98f7ae6128 |
@@ -1 +1 @@
|
||||
0.1.0: QmRdt1nxGDL6UWo2ziX9no4BDmMd8YZoDP88FgXBpKDZZi
|
||||
0.2.3: QmcxkxTVuURV2Ptse8TvkqH5BQDwV62X1x19JqqvbBzwUM
|
||||
|
||||
25
.travis.yml
Normal file
25
.travis.yml
Normal file
@@ -0,0 +1,25 @@
|
||||
os:
|
||||
- linux
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.7
|
||||
|
||||
install:
|
||||
- go get -u github.com/whyrusleeping/gx
|
||||
- go get -u github.com/whyrusleeping/gx-go
|
||||
- gx install
|
||||
|
||||
|
||||
script:
|
||||
- gx-go rewrite
|
||||
- go test -race -coverprofile=unittest.coverprofile -covermode=atomic ./...
|
||||
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash) -f unittest.coverprofile -F unittest
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/src/gx
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Protocol Labs Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
36
README.md
Normal file
36
README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# go-multibase
|
||||
|
||||
[](http://ipn.io)
|
||||
[](https://github.com/multiformats/multiformats)
|
||||
[](https://webchat.freenode.net/?channels=%23ipfs)
|
||||
[](https://github.com/RichardLitt/standard-readme)
|
||||
[](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
|
||||
|
||||
## Install
|
||||
|
||||
```sh
|
||||
go get github.com/multiformats/go-multibase
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
TODO
|
||||
|
||||
## Maintainers
|
||||
|
||||
Captain: [@whyrusleeping](https://github.com/whyrusleeping).
|
||||
|
||||
## Contribute
|
||||
|
||||
Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multibase/issues).
|
||||
|
||||
Check out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).
|
||||
|
||||
Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE) © 2016 Protocol Labs Inc.
|
||||
1
codecov.yml
Normal file
1
codecov.yml
Normal file
@@ -0,0 +1 @@
|
||||
comment: off
|
||||
86
multibase.go
86
multibase.go
@@ -1,39 +1,103 @@
|
||||
package multibase
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
b58 "github.com/jbenet/go-base58"
|
||||
b32 "github.com/whyrusleeping/base32"
|
||||
)
|
||||
|
||||
type Encoding int
|
||||
|
||||
const (
|
||||
Base1 = '1'
|
||||
Base2 = '0'
|
||||
Base8 = '7'
|
||||
Base10 = '9'
|
||||
Base16 = 'f'
|
||||
Base58Flickr = 'Z'
|
||||
Base58BTC = 'z'
|
||||
Identity = 0x00
|
||||
Base1 = '1'
|
||||
Base2 = '0'
|
||||
Base8 = '7'
|
||||
Base10 = '9'
|
||||
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 ErrUnsupportedEncoding = fmt.Errorf("selected encoding not supported")
|
||||
|
||||
func Encode(base int, data []byte) (string, error) {
|
||||
func Encode(base Encoding, data []byte) (string, error) {
|
||||
switch base {
|
||||
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 Base16, Base16Upper:
|
||||
return string(Base16) + hex.EncodeToString(data), nil
|
||||
case Base32, Base32Upper:
|
||||
return string(Base32) + b32.RawStdEncoding.EncodeToString(data), nil
|
||||
case Base32hex, Base32hexUpper:
|
||||
return string(Base32hex) + b32.RawHexEncoding.EncodeToString(data), nil
|
||||
case Base32pad, Base32padUpper:
|
||||
return string(Base32pad) + b32.StdEncoding.EncodeToString(data), nil
|
||||
case Base32hexPad, Base32hexPadUpper:
|
||||
return string(Base32hexPad) + b32.HexEncoding.EncodeToString(data), nil
|
||||
case Base58BTC:
|
||||
return string(Base58BTC) + b58.EncodeAlphabet(data, b58.BTCAlphabet), nil
|
||||
case Base16:
|
||||
return string(Base16) + hex.EncodeToString(data), nil
|
||||
case Base58Flickr:
|
||||
return string(Base58Flickr) + b58.EncodeAlphabet(data, b58.FlickrAlphabet), nil
|
||||
case Base64pad:
|
||||
return string(Base64pad) + base64.StdEncoding.EncodeToString(data), nil
|
||||
case Base64urlPad:
|
||||
return string(Base64urlPad) + base64.URLEncoding.EncodeToString(data), nil
|
||||
default:
|
||||
return "", ErrUnsupportedEncoding
|
||||
}
|
||||
}
|
||||
|
||||
func Decode(data string) (int, []byte, error) {
|
||||
func Decode(data string) (Encoding, []byte, error) {
|
||||
if len(data) == 0 {
|
||||
return 0, nil, fmt.Errorf("cannot decode multibase for zero length string")
|
||||
}
|
||||
|
||||
switch data[0] {
|
||||
case Identity:
|
||||
return Identity, []byte(data[1:]), nil
|
||||
case Base16, Base16Upper:
|
||||
bytes, err := hex.DecodeString(data[1:])
|
||||
return Base16, bytes, err
|
||||
case Base32, Base32Upper:
|
||||
bytes, err := b32.RawStdEncoding.DecodeString(data[1:])
|
||||
return Base32, bytes, err
|
||||
case Base32hex, Base32hexUpper:
|
||||
bytes, err := b32.RawHexEncoding.DecodeString(data[1:])
|
||||
return Base32hex, bytes, err
|
||||
case Base32pad, Base32padUpper:
|
||||
bytes, err := b32.StdEncoding.DecodeString(data[1:])
|
||||
return Base32pad, bytes, err
|
||||
case Base32hexPad, Base32hexPadUpper:
|
||||
bytes, err := b32.HexEncoding.DecodeString(data[1:])
|
||||
return Base32hexPad, bytes, err
|
||||
case Base58BTC:
|
||||
return Base58BTC, b58.DecodeAlphabet(data[1:], b58.BTCAlphabet), nil
|
||||
case Base58Flickr:
|
||||
return Base58Flickr, b58.DecodeAlphabet(data[1:], b58.FlickrAlphabet), nil
|
||||
case Base64pad:
|
||||
bytes, err := base64.StdEncoding.DecodeString(data[1:])
|
||||
return Base64pad, bytes, err
|
||||
case Base64urlPad:
|
||||
bytes, err := base64.URLEncoding.DecodeString(data[1:])
|
||||
return Base64urlPad, bytes, err
|
||||
default:
|
||||
return -1, nil, ErrUnsupportedEncoding
|
||||
}
|
||||
|
||||
@@ -6,25 +6,80 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBase58RoundTrip(t *testing.T) {
|
||||
buf := make([]byte, 16)
|
||||
rand.Read(buf)
|
||||
var sampleBytes = []byte("Decentralize everything!!")
|
||||
var encodedSamples = map[Encoding]string{
|
||||
Identity: string(0x00) + "Decentralize everything!!",
|
||||
Base16: "f446563656e7472616c697a652065766572797468696e672121",
|
||||
Base58BTC: "zUXE7GvtEk8XTXs1GF8HSGbVA9FCX9SEBPe",
|
||||
Base64pad: "MRGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchIQ==",
|
||||
Base64urlPad: "URGVjZW50cmFsaXplIGV2ZXJ5dGhpbmchIQ==",
|
||||
}
|
||||
|
||||
enc, err := Encode(Base58BTC, buf)
|
||||
func testEncode(t *testing.T, encoding Encoding, bytes []byte, expected string) {
|
||||
actual, err := Encode(encoding, bytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
e, out, err := Decode(enc)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if e != Base58BTC {
|
||||
t.Fatal("got wrong encoding out")
|
||||
}
|
||||
|
||||
if !bytes.Equal(buf, out) {
|
||||
t.Fatal("input wasnt the same as output", buf, out)
|
||||
if actual != expected {
|
||||
t.Errorf("encoding failed for %c (%d), expected: %s, got: %s", encoding, encoding, expected, actual)
|
||||
}
|
||||
}
|
||||
|
||||
func testDecode(t *testing.T, expectedEncoding Encoding, expectedBytes []byte, data string) {
|
||||
actualEncoding, actualBytes, err := Decode(data)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if actualEncoding != expectedEncoding {
|
||||
t.Errorf("wrong encoding code, expected: %c (%d), got %c (%d)", expectedEncoding, expectedEncoding, actualEncoding, actualEncoding)
|
||||
}
|
||||
if !bytes.Equal(actualBytes, expectedBytes) {
|
||||
t.Errorf("decoding failed for %c (%d), expected: %v, got %v", actualEncoding, actualEncoding, expectedBytes, actualBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncode(t *testing.T) {
|
||||
for encoding, data := range encodedSamples {
|
||||
testEncode(t, encoding, sampleBytes, data)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecode(t *testing.T) {
|
||||
for encoding, data := range encodedSamples {
|
||||
testDecode(t, encoding, sampleBytes, data)
|
||||
}
|
||||
}
|
||||
|
||||
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 baseList {
|
||||
enc, err := Encode(base, buf)
|
||||
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(buf, out) {
|
||||
t.Fatal("input wasnt the same as output", buf, out)
|
||||
}
|
||||
}
|
||||
|
||||
_, _, err := Decode("")
|
||||
if err == nil {
|
||||
t.Fatal("shouldnt be able to decode empty string")
|
||||
}
|
||||
}
|
||||
|
||||
18
package.json
18
package.json
@@ -6,9 +6,25 @@
|
||||
"gx": {
|
||||
"dvcsimport": "github.com/multiformats/go-multibase"
|
||||
},
|
||||
"gxDependencies": [
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmZvZSVtvxak4dcTkhsQhqd1SQ6rg5UzaSTu62WfWKjj93",
|
||||
"name": "base32",
|
||||
"version": "0.0.1"
|
||||
},
|
||||
{
|
||||
"author": "whyrusleeping",
|
||||
"hash": "QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf",
|
||||
"name": "go-base58",
|
||||
"version": "0.0.0"
|
||||
}
|
||||
],
|
||||
"gxVersion": "0.8.0",
|
||||
"language": "go",
|
||||
"license": "",
|
||||
"name": "go-multibase",
|
||||
"version": "0.1.0"
|
||||
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
|
||||
"version": "0.2.3"
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user