forked from TheTrunk/hushgen
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b75ed50
commit 22d7394
Showing
14 changed files
with
872 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
base58 | ||
========== | ||
|
||
[![Build Status](http://img.shields.io/travis/btcsuite/btcutil.svg)] | ||
(https://travis-ci.org/btcsuite/btcutil) [![ISC License] | ||
(http://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org) | ||
[![GoDoc](https://godoc.org/github.com/btcsuite/btcutil/base58?status.png)] | ||
(http://godoc.org/github.com/btcsuite/btcutil/base58) | ||
|
||
Package base58 provides an API for encoding and decoding to and from the | ||
modified base58 encoding. It also provides an API to do Base58Check encoding, | ||
as described [here](https://en.bitcoin.it/wiki/Base58Check_encoding). | ||
|
||
A comprehensive suite of tests is provided to ensure proper functionality. | ||
|
||
## Installation and Updating | ||
|
||
```bash | ||
$ go get -u github.com/btcsuite/btcutil/base58 | ||
``` | ||
|
||
## Examples | ||
|
||
* [Decode Example] | ||
(http://godoc.org/github.com/btcsuite/btcutil/base58#example-Decode) | ||
Demonstrates how to decode modified base58 encoded data. | ||
* [Encode Example] | ||
(http://godoc.org/github.com/btcsuite/btcutil/base58#example-Encode) | ||
Demonstrates how to encode data using the modified base58 encoding scheme. | ||
* [CheckDecode Example] | ||
(http://godoc.org/github.com/btcsuite/btcutil/base58#example-CheckDecode) | ||
Demonstrates how to decode Base58Check encoded data. | ||
* [CheckEncode Example] | ||
(http://godoc.org/github.com/btcsuite/btcutil/base58#example-CheckEncode) | ||
Demonstrates how to encode data using the Base58Check encoding scheme. | ||
|
||
## License | ||
|
||
Package base58 is licensed under the [copyfree](http://copyfree.org) ISC | ||
License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2015 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
// AUTOGENERATED by genalphabet.go; do not edit. | ||
|
||
package base58 | ||
|
||
const ( | ||
// alphabet is the modified base58 alphabet used by Bitcoin. | ||
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" | ||
|
||
alphabetIdx0 = '1' | ||
) | ||
|
||
var b58 = [256]byte{ | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 0, 1, 2, 3, 4, 5, 6, | ||
7, 8, 255, 255, 255, 255, 255, 255, | ||
255, 9, 10, 11, 12, 13, 14, 15, | ||
16, 255, 17, 18, 19, 20, 21, 255, | ||
22, 23, 24, 25, 26, 27, 28, 29, | ||
30, 31, 32, 255, 255, 255, 255, 255, | ||
255, 33, 34, 35, 36, 37, 38, 39, | ||
40, 41, 42, 43, 255, 44, 45, 46, | ||
47, 48, 49, 50, 51, 52, 53, 54, | ||
55, 56, 57, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
255, 255, 255, 255, 255, 255, 255, 255, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) 2013-2015 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package base58 | ||
|
||
import ( | ||
"math/big" | ||
) | ||
|
||
//go:generate go run genalphabet.go | ||
|
||
var bigRadix = big.NewInt(58) | ||
var bigZero = big.NewInt(0) | ||
|
||
// Decode decodes a modified base58 string to a byte slice. | ||
func Decode(b string) []byte { | ||
answer := big.NewInt(0) | ||
j := big.NewInt(1) | ||
|
||
scratch := new(big.Int) | ||
for i := len(b) - 1; i >= 0; i-- { | ||
tmp := b58[b[i]] | ||
if tmp == 255 { | ||
return []byte("") | ||
} | ||
scratch.SetInt64(int64(tmp)) | ||
scratch.Mul(j, scratch) | ||
answer.Add(answer, scratch) | ||
j.Mul(j, bigRadix) | ||
} | ||
|
||
tmpval := answer.Bytes() | ||
|
||
var numZeros int | ||
for numZeros = 0; numZeros < len(b); numZeros++ { | ||
if b[numZeros] != alphabetIdx0 { | ||
break | ||
} | ||
} | ||
flen := numZeros + len(tmpval) | ||
val := make([]byte, flen, flen) | ||
copy(val[numZeros:], tmpval) | ||
|
||
return val | ||
} | ||
|
||
// Encode encodes a byte slice to a modified base58 string. | ||
func Encode(b []byte) string { | ||
x := new(big.Int) | ||
x.SetBytes(b) | ||
|
||
answer := make([]byte, 0, len(b)*136/100) | ||
for x.Cmp(bigZero) > 0 { | ||
mod := new(big.Int) | ||
x.DivMod(x, bigRadix, mod) | ||
answer = append(answer, alphabet[mod.Int64()]) | ||
} | ||
|
||
// leading zero bytes | ||
for _, i := range b { | ||
if i != 0 { | ||
break | ||
} | ||
answer = append(answer, alphabetIdx0) | ||
} | ||
|
||
// reverse | ||
alen := len(answer) | ||
for i := 0; i < alen/2; i++ { | ||
answer[i], answer[alen-1-i] = answer[alen-1-i], answer[i] | ||
} | ||
|
||
return string(answer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2013-2014 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package base58_test | ||
|
||
import ( | ||
"bytes" | ||
"encoding/hex" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcutil/base58" | ||
) | ||
|
||
var stringTests = []struct { | ||
in string | ||
out string | ||
}{ | ||
{"", ""}, | ||
{" ", "Z"}, | ||
{"-", "n"}, | ||
{"0", "q"}, | ||
{"1", "r"}, | ||
{"-1", "4SU"}, | ||
{"11", "4k8"}, | ||
{"abc", "ZiCa"}, | ||
{"1234598760", "3mJr7AoUXx2Wqd"}, | ||
{"abcdefghijklmnopqrstuvwxyz", "3yxU3u1igY8WkgtjK92fbJQCd4BZiiT1v25f"}, | ||
{"00000000000000000000000000000000000000000000000000000000000000", "3sN2THZeE9Eh9eYrwkvZqNstbHGvrxSAM7gXUXvyFQP8XvQLUqNCS27icwUeDT7ckHm4FUHM2mTVh1vbLmk7y"}, | ||
} | ||
|
||
var invalidStringTests = []struct { | ||
in string | ||
out string | ||
}{ | ||
{"0", ""}, | ||
{"O", ""}, | ||
{"I", ""}, | ||
{"l", ""}, | ||
{"3mJr0", ""}, | ||
{"O3yxU", ""}, | ||
{"3sNI", ""}, | ||
{"4kl8", ""}, | ||
{"0OIl", ""}, | ||
{"!@#$%^&*()-_=+~`", ""}, | ||
} | ||
|
||
var hexTests = []struct { | ||
in string | ||
out string | ||
}{ | ||
{"61", "2g"}, | ||
{"626262", "a3gV"}, | ||
{"636363", "aPEr"}, | ||
{"73696d706c792061206c6f6e6720737472696e67", "2cFupjhnEsSn59qHXstmK2ffpLv2"}, | ||
{"00eb15231dfceb60925886b67d065299925915aeb172c06647", "1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L"}, | ||
{"516b6fcd0f", "ABnLTmg"}, | ||
{"bf4f89001e670274dd", "3SEo3LWLoPntC"}, | ||
{"572e4794", "3EFU7m"}, | ||
{"ecac89cad93923c02321", "EJDM8drfXA6uyA"}, | ||
{"10c8511e", "Rt5zm"}, | ||
{"00000000000000000000", "1111111111"}, | ||
} | ||
|
||
func TestBase58(t *testing.T) { | ||
// Encode tests | ||
for x, test := range stringTests { | ||
tmp := []byte(test.in) | ||
if res := base58.Encode(tmp); res != test.out { | ||
t.Errorf("Encode test #%d failed: got: %s want: %s", | ||
x, res, test.out) | ||
continue | ||
} | ||
} | ||
|
||
// Decode tests | ||
for x, test := range hexTests { | ||
b, err := hex.DecodeString(test.in) | ||
if err != nil { | ||
t.Errorf("hex.DecodeString failed failed #%d: got: %s", x, test.in) | ||
continue | ||
} | ||
if res := base58.Decode(test.out); bytes.Equal(res, b) != true { | ||
t.Errorf("Decode test #%d failed: got: %q want: %q", | ||
x, res, test.in) | ||
continue | ||
} | ||
} | ||
|
||
// Decode with invalid input | ||
for x, test := range invalidStringTests { | ||
if res := base58.Decode(test.in); string(res) != test.out { | ||
t.Errorf("Decode invalidString test #%d failed: got: %q want: %q", | ||
x, res, test.out) | ||
continue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) 2013-2014 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package base58_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/btcsuite/btcutil/base58" | ||
) | ||
|
||
func BenchmarkBase58Encode(b *testing.B) { | ||
b.StopTimer() | ||
data := bytes.Repeat([]byte{0xff}, 5000) | ||
b.SetBytes(int64(len(data))) | ||
b.StartTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
base58.Encode(data) | ||
} | ||
} | ||
|
||
func BenchmarkBase58Decode(b *testing.B) { | ||
b.StopTimer() | ||
data := bytes.Repeat([]byte{0xff}, 5000) | ||
encoded := base58.Encode(data) | ||
b.SetBytes(int64(len(encoded))) | ||
b.StartTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
base58.Decode(encoded) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2013-2014 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package base58 | ||
|
||
import ( | ||
"crypto/sha256" | ||
"errors" | ||
) | ||
|
||
// ErrChecksum indicates that the checksum of a check-encoded string does not verify against | ||
// the checksum. | ||
var ErrChecksum = errors.New("checksum error") | ||
|
||
// ErrInvalidFormat indicates that the check-encoded string has an invalid format. | ||
var ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing") | ||
|
||
// checksum: first four bytes of sha256^2 | ||
func checksum(input []byte) (cksum [4]byte) { | ||
h := sha256.Sum256(input) | ||
h2 := sha256.Sum256(h[:]) | ||
copy(cksum[:], h2[:4]) | ||
return | ||
} | ||
|
||
// CheckEncode prepends two version bytes and appends a four byte checksum. | ||
func CheckEncode(input []byte, version [2]byte) string { | ||
b := make([]byte, 0, 2+len(input)+4) | ||
b = append(b, version[:]...) | ||
b = append(b, input[:]...) | ||
cksum := checksum(b) | ||
b = append(b, cksum[:]...) | ||
return Encode(b) | ||
} | ||
|
||
// CheckDecode decodes a string that was encoded with CheckEncode and verifies | ||
// the checksum. | ||
func CheckDecode(input string) (result []byte, version [2]byte, err error) { | ||
decoded := Decode(input) | ||
if len(decoded) < 6 { | ||
return nil, [2]byte{0, 0}, ErrInvalidFormat | ||
} | ||
version = [2]byte{decoded[0], decoded[1]} | ||
var cksum [4]byte | ||
copy(cksum[:], decoded[len(decoded)-4:]) | ||
if checksum(decoded[:len(decoded)-4]) != cksum { | ||
return nil, [2]byte{0, 0}, ErrChecksum | ||
} | ||
payload := decoded[2 : len(decoded)-4] | ||
result = append(result, payload...) | ||
return | ||
} |
Oops, something went wrong.