encoding

package
v0.1.3-0...-3c80581 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2020 License: MIT Imports: 11 Imported by: 0

README

Encoding

encoding.MustEncode(keys.RandBytes(16), encoding.Base32, encoding.NoPadding(), encoding.Lowercase())

Documentation

Overview

Package encoding provides encoding and decoding of different formats like Base62, Saltpack, BIP39.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidBIP39Input = errors.New("bip39 only accepts 16, 20, 24, 28, 32 bytes")

ErrInvalidBIP39Input if invalid number of bytes for encoding.

Functions

func BreakString

func BreakString(msg string, wordLen int, lineLen int) string

BreakString breaks words and lines.

func BytesToPhrase

func BytesToPhrase(b []byte) (string, error)

BytesToPhrase returns a phrase for bytes

func Decode

func Decode(s string, encoding Encoding) ([]byte, error)

Decode decodes string to bytes using encoding

func DecodeBase32

func DecodeBase32(s string) ([]byte, error)

DecodeBase32 decode base32.

func DecodeBase62

func DecodeBase62(s string) ([]byte, error)

DecodeBase62 decode base62.

func DecodeBase64

func DecodeBase64(s string) ([]byte, error)

DecodeBase64 decode base64.

func DecodeHex

func DecodeHex(s string) ([]byte, error)

DecodeHex decode base16 (hex).

func DecodeSaltpack

func DecodeSaltpack(msg string, isHTML bool) ([]byte, string, error)

DecodeSaltpack decodes saltpack message.

func Encode

func Encode(b []byte, encoding Encoding, opt ...EncodeOption) (string, error)

Encode encodes bytes to an Encoding.

func EncodeBase32

func EncodeBase32(b []byte) string

EncodeBase32 encode base32.

func EncodeBase62

func EncodeBase62(b []byte) string

EncodeBase62 encode base62.

func EncodeBase64

func EncodeBase64(b []byte) string

EncodeBase64 encode base64.

func EncodeHex

func EncodeHex(b []byte) string

EncodeHex encode base16 (hex).

func EncodeSaltpack

func EncodeSaltpack(b []byte, brand string) string

EncodeSaltpack encodes bytes to saltpack message.

func FindSaltpack

func FindSaltpack(msg string, isHTML bool) (string, string)

FindSaltpack finds saltpack message in a string.

func HasUpper

func HasUpper(s string) bool

HasUpper returns true if string has an uppercase character.

func IsASCII

func IsASCII(b []byte) bool

IsASCII returns true if bytes are ASCII.

func IsAlphaNumeric

func IsAlphaNumeric(s string, extra string) bool

IsAlphaNumeric returns true if string is only a-z, A-Z, 0-9 with optional extra characters.

func IsValidPhrase

func IsValidPhrase(phrase string, sanitize bool) bool

IsValidPhrase checks is phrase is valid

func MustDecode

func MustDecode(s string, encoding Encoding) []byte

MustDecode decodes or panics.

func MustEncode

func MustEncode(b []byte, encoding Encoding, opt ...EncodeOption) string

MustEncode returns encoding or panics on error.

func PhraseToBytes

func PhraseToBytes(phrase string, sanitize bool) (*[32]byte, error)

PhraseToBytes decodes a bip39 mnemonic into bytes

func TrimSaltpack

func TrimSaltpack(msg string, allowSpace bool) string

TrimSaltpack removes non base63 characters from a string.

Types

type EncodeOption

type EncodeOption struct {
	Apply     func(*EncodeOptions)
	Encodings []Encoding
	Name      string
}

EncodeOption ...

func Lowercase

func Lowercase() EncodeOption

Lowercase ...

func NoPadding

func NoPadding() EncodeOption

NoPadding ...

type EncodeOptions

type EncodeOptions struct {
	// NoPadding, for encodings that can disable padding (base64, base32).
	NoPadding bool
	// Lowercase, for encodings that can be upper or lower case, use lowercase (base32).
	Lowercase bool
}

EncodeOptions ...

type Encoding

type Encoding string

Encoding is an encoding for bytes to and from a string

const (
	// NoEncoding ...
	NoEncoding Encoding = ""
	// Hex (Base16) encoding
	Hex Encoding = "base16"
	// Base32 encoding
	Base32 Encoding = "base32"
	// Base58 encoding
	Base58 Encoding = "base58"
	// Base62 encoding
	Base62 Encoding = "base62"
	// Base64 encoding (with padding)
	Base64 Encoding = "base64"
	// Saltpack encoding
	Saltpack Encoding = "saltpack"
	// BIP39 encoding
	BIP39 Encoding = "bip39"
)

func NewEncoding

func NewEncoding(s string) Encoding

NewEncoding returns an Encoding from a string.

func Parse

func Parse(s string) (Encoding, error)

Parse returns an Encoding from a string.

func ParseOr

func ParseOr(s string, d Encoding) (Encoding, error)

ParseOr returns an Encoding from a string, or if empty, a default.

type ErrInvalidPhrase

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

ErrInvalidPhrase if phrase is invalid.

func (ErrInvalidPhrase) Cause

func (e ErrInvalidPhrase) Cause() error

Cause for ErrInvalidPhrase

func (ErrInvalidPhrase) Error

func (e ErrInvalidPhrase) Error() string

Jump to

Keyboard shortcuts

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