basex

package module
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 2 Imported by: 0

README

Go-Basex

使用给定的任意字母表编码和解码数据

forked from eknkc/basex

包含: base2 base8 base11 base16 base32 base36 base56 base57 base58 base62 base67

以及任意自定义字母表

base62 (推荐)

forked from jxskiss/base62

// Basic usage.
Encode(src []byte) []byte
EncodeToString(src []byte) string
Decode(src []byte) ([]byte, error)
DecodeString(src string) ([]byte, error)
FormatInt(num int64) []byte
FormatUint(num uint64) []byte
ParseInt(src []byte) (int64, error)
ParseUint(src []byte) (uint64, error)

// Providing a dst buffer, you may reuse buffers to reduce memory allocation.
EncodeToBuf(dst []byte, src []byte) []byte
DecodeToBuf(dst []byte, src []byte) ([]byte, error)
AppendInt(dst []byte, num int64) []byte
AppendUint(dst []byte, num uint64) []byte

// Or you may use a custom encoding alphabet.
enc := NewEncoding("...my-62-byte-string-alphabet...")
enc.XXX()

base58

包含: Bitcoin IPFS Flickr Ripple

Bitcoin 推荐使用:

  • base58.Encode()
  • base58.Decode()
  • base58.CheckEncode()
  • base58.CheckDecode()

base58, bech32 forked from btcsuite/btcutil

使用

go get github.com/fufuok/basex

示例

base58
// base58 `Bitcoin`
fmt.Println(basex.B58Encode([]byte("中")), base58.Encode([]byte("中")))
fmt.Println(string(basex.B58Decode("2KprQ")), string(base58.Decode("2KprQ")))
fmt.Println(basex.B58Encode([]byte("密~&#1x")), base58.Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B58Decode("fREqFdERUAK")), string(base58.Decode("fREqFdERUAK")))
fmt.Println(base58.CheckEncode([]byte("中"), 20))
txt, ver, _ := base58.CheckDecode("4VhAoVNjGNT")
fmt.Println(string(txt), ver)
// base58 `Flickr`
fmt.Println(basex.B58fEncode([]byte("中")))
fmt.Println(string(basex.B58fDecode("2jPRp")))
fmt.Println(basex.B58fEncode([]byte("密~&#1x")))
fmt.Println(string(basex.B58fDecode("EqeQfCeqtaj")))
// base58 `Ripple`
fmt.Println(basex.B58rEncode([]byte("中")))
fmt.Println(string(basex.B58rDecode("pKFiQ")))
fmt.Println(basex.B58rEncode([]byte("密~&#1x")))
fmt.Println(string(basex.B58rDecode("CRNqEdNR7wK")))
base2, base8, base11, base16, base32, base36, base56, base57, base62, base67
fmt.Println(basex.B2Encode([]byte("中")))
fmt.Println(string(basex.B2Decode("111001001011100010101101")))
fmt.Println(basex.B2Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B2Decode("1110010110101111100001100111111000100110001000110011000101111000")))

fmt.Println(basex.B8Encode([]byte("中")))
fmt.Println(string(basex.B8Decode("71134255")))
fmt.Println(basex.B8Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B8Decode("1626574147704610630570")))

fmt.Println(basex.B11Encode([]byte("中")))
fmt.Println(string(basex.B11Decode("8508905")))
fmt.Println(basex.B11Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B11Decode("2a820986a07867a2533")))

fmt.Println(basex.B16Encode([]byte("中")))
fmt.Println(string(basex.B16Decode("e4b8ad")))
fmt.Println(basex.B16Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B16Decode("e5af867e26233178")))

fmt.Println(basex.B32Encode([]byte("中")))
fmt.Println(string(basex.B32Decode("E9E5D")))
fmt.Println(basex.B32Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B32Decode("EBBW6FRK26CBR")))
fmt.Println(basex.B32zEncode([]byte("中")))
fmt.Println(string(basex.B32zDecode("qjqfp")))
fmt.Println(basex.B32zEncode([]byte("密~&#1x")))
fmt.Println(string(basex.B32zDecode("qmmhgxaungcma")))

fmt.Println(basex.B36Encode([]byte("中")))
fmt.Println(string(basex.B36Decode("8x9yl")))
fmt.Println(basex.B36Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B36Decode("3hqrsahs0q9a0")))

fmt.Println(basex.B56Encode([]byte("中")))
fmt.Println(string(basex.B56Decode("3XMpP")))
fmt.Println(basex.B56Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B56Decode("yZmQK7eFEgi")))

fmt.Println(basex.B57Encode([]byte("中")))
fmt.Println(string(basex.B57Decode("3RwaS")))
fmt.Println(basex.B57Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B57Decode("oioDRyadoBe")))

fmt.Println(basex.B62Encode([]byte("中")))
fmt.Println(string(basex.B62Decode("10TrT")))
fmt.Println(basex.B62Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B62Decode("jIBTQOl1Xcs")))

fmt.Println(basex.B67Encode([]byte("中")))
fmt.Println(string(basex.B67Decode("x4Ks")))
fmt.Println(basex.B67Encode([]byte("密~&#1x")))
fmt.Println(string(basex.B67Decode("JFWOp05dgWR")))
任意字母表
alphabet := "x@-_*2"
enc, _ := basex.New(alphabet)
fmt.Println(enc.Encode([]byte("密~&#1x")))
src, _ := enc.Decode("_-2*-*_**@*-2**xx*-@_@*xx")
fmt.Println(string(src))

ff

Documentation

Overview

Package basex provides fast base encoding / decoding of any given alphabet using bitcoin style leading zero compression. It is a GO port of https://github.com/cryptocoinjs/base-x forked from eknkc/basex

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func B11Decode

func B11Decode(s string) []byte

Base11 解码

func B11Encode

func B11Encode(b []byte) string

Base11 编码

func B16Decode

func B16Decode(s string) []byte

Base16 解码

func B16Encode

func B16Encode(b []byte) string

Base16 编码

func B2Decode

func B2Decode(s string) []byte

Base2 解码

func B2Encode

func B2Encode(b []byte) string

Base2 编码

func B32Decode

func B32Decode(s string) []byte

Base32 解码

func B32Encode

func B32Encode(b []byte) string

Base32 编码

func B32zDecode

func B32zDecode(s string) []byte

Base32 解码 (z-base-32)

func B32zEncode

func B32zEncode(b []byte) string

Base32 编码 (z-base-32)

func B36Decode

func B36Decode(s string) []byte

Base36 解码

func B36Encode

func B36Encode(b []byte) string

Base36 编码

func B56Decode

func B56Decode(s string) []byte

Base56 解码

func B56Encode

func B56Encode(b []byte) string

Base56 编码

func B57Decode

func B57Decode(s string) []byte

Base57 解码

func B57Encode

func B57Encode(b []byte) string

Base57 编码

func B58Decode

func B58Decode(s string) []byte

Base58 解码, 推荐使用: base58.Decode()

func B58Encode

func B58Encode(b []byte) string

Base58 编码, 推荐使用: base58.Encode()

func B58fDecode

func B58fDecode(s string) []byte

Base56 Flickr 解码

func B58fEncode

func B58fEncode(b []byte) string

Base58 Flickr 编码

func B58rDecode

func B58rDecode(s string) []byte

Base56 Ripple 解码

func B58rEncode

func B58rEncode(b []byte) string

Base58 Ripple 编码

func B62Decode

func B62Decode(s string) []byte

Base62 解码

func B62Encode

func B62Encode(b []byte) string

Base62 编码

func B67Decode

func B67Decode(s string) []byte

Base67 解码

func B67Encode

func B67Encode(b []byte) string

Base67 编码

func B8Decode

func B8Decode(s string) []byte

Base8 解码

func B8Encode

func B8Encode(b []byte) string

Base8 编码

Types

type Encoding

type Encoding struct {
	// contains filtered or unexported fields
}

Encoding is a custom base encoding defined by an alphabet. It should bre created using New function

func New

func New(alphabet string) (*Encoding, error)

New returns a custom base encoder defined by the alphabet string. The alphabet should contain non-repeating characters. Ordering is important. Example alphabets:

  • base2: 01
  • base16: 0123456789abcdef
  • base32: 0123456789ABCDEFGHJKMNPQRSTVWXYZ
  • base36: 0123456789abcdefghijklmnopqrstuvwxyz
  • base58: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz
  • base62: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

func (*Encoding) Decode

func (e *Encoding) Decode(source string) ([]byte, error)

Decode function decodes a string previously obtained from Encode, using the same alphabet and returns a byte slice In case the input is not valid an arror will be returned

func (*Encoding) Encode

func (e *Encoding) Encode(source []byte) string

Encode function receives a byte slice and encodes it to a string using the alphabet provided

Directories

Path Synopsis
Package base58 provides an API for working with modified base58 and Base58Check encodings.
Package base58 provides an API for working with modified base58 and Base58Check encodings.
Package bech32 provides a Go implementation of the bech32 format specified in BIP 173.
Package bech32 provides a Go implementation of the bech32 format specified in BIP 173.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL