17 Commits

Author SHA1 Message Date
Jeromy
a20e296ca7 gx publish 0.2.3 2017-02-02 18:59:19 -08:00
Jeromy
a723c120b9 release 0.2.2 2017-02-02 18:24:17 -08:00
Jakub Sztandera
4cedb5a79e Merge pull request #12 from multiformats/feat/standardize-readme
Edited Badges, small edits
2016-12-26 21:55:11 +01:00
Richard Littauer
6a1554bbad Edited Badges, small edits
Added Travis, CodeCov, Standard Readme, project badge, fixed https links, made description match repo, fixed maintainers title, added year to license.
2016-12-26 15:47:58 -05:00
Jeromy Johnson
9a56b2b802 Merge pull request #10 from multiformats/feat/add-base-null
Implement Identity encoding
2016-12-25 03:02:04 -08:00
Wigy
374a93370a Implement Identity encoding 2016-12-22 17:16:05 +01:00
Jakub Sztandera
c264cdd0ae Use custom Encoding type
Previously base variable were having type `int` which could be confusing
as with literal base number (16 for hex).

By using custom base variable I hope to make it more clear than it was
before.
2016-12-21 20:42:49 +01:00
Jakub Sztandera
e11767a490 Merge pull request #8 from multiformats/feat/ci
Fix CI
2016-12-17 00:01:35 +01:00
Jakub Sztandera
c4f49cd356 codecov: diable comments 2016-12-16 23:56:46 +01:00
Jakub Sztandera
3b16e9048c Fix CI 2016-12-16 23:54:26 +01:00
Jeromy Johnson
ee1176c739 Merge pull request #5 from mateon1/master
Add support for more base encodings
2016-12-16 14:45:06 -08:00
mateon1
8e28da0e2a Updated codes to values from csv, added padded b32
License: MIT
Signed-off-by: Mateusz Naściszewski <matin1111@wp.pl>
2016-12-12 09:01:18 +01:00
mateon1
3d3cb9341e Added support for more base encodings
License: MIT
Signed-off-by: Mateusz Naściszewski <matin1111@wp.pl>
2016-11-27 23:56:23 +01:00
Jeromy
64b00b0f1a gx publish v0.2.1 2016-10-24 15:32:13 -07:00
Jeromy
ed7d4b3b12 guard against empty strings 2016-10-24 15:31:20 -07:00
Jeromy Johnson
2c9be8ec36 Merge pull request #4 from multiformats/feat/license-and-doc
Add license and standardize README
2016-10-14 10:54:33 -04:00
Richard Littauer
528883f051 Add license and standardize README
Still needs Usage section. I wasnt able to see this on godoc.org; how do I publish the godoc? Also, the license needs to be checked. This will close #3
2016-10-14 10:53:31 -04:00
8 changed files with 237 additions and 34 deletions

View File

@@ -1 +1 @@
0.2.0: QmYiTi9mKBMjfiup7na7PhJK7QEZPdMTJenLdgFYVQ2NUv
0.2.3: QmcxkxTVuURV2Ptse8TvkqH5BQDwV62X1x19JqqvbBzwUM

25
.travis.yml Normal file
View 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
View 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.

View File

@@ -1,7 +1,36 @@
go-multibase
==============
# go-multibase
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs)
[![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme)
[![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)
> Go implementation of the [multibase](https://github.com/multiformats/multibase) specification.
> 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
View File

@@ -0,0 +1 @@
comment: off

View File

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

View File

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

View File

@@ -7,6 +7,12 @@
"dvcsimport": "github.com/multiformats/go-multibase"
},
"gxDependencies": [
{
"author": "whyrusleeping",
"hash": "QmZvZSVtvxak4dcTkhsQhqd1SQ6rg5UzaSTu62WfWKjj93",
"name": "base32",
"version": "0.0.1"
},
{
"author": "whyrusleeping",
"hash": "QmT8rehPR3F6bmwL6zjUN8XpiDBFFpMP2myPdC6ApsWfJf",
@@ -18,5 +24,7 @@
"language": "go",
"license": "",
"name": "go-multibase",
"version": "0.2.0"
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.2.3"
}