encoding

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: MIT Imports: 12 Imported by: 19

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 added in v0.1.1

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

DecodeBase32 decode base32.

func DecodeBase62 added in v0.1.1

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

DecodeBase62 decode base62.

func DecodeBase64 added in v0.1.1

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

DecodeBase64 decode base64.

func DecodeHex added in v0.1.1

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 added in v0.1.1

func EncodeBase32(b []byte) string

EncodeBase32 encode base32.

func EncodeBase62 added in v0.1.1

func EncodeBase62(b []byte) string

EncodeBase62 encode base62.

func EncodeBase64 added in v0.1.1

func EncodeBase64(b []byte) string

EncodeBase64 encode base64.

func EncodeHex added in v0.1.1

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 starting with "BEGIN {BRAND }MESSAGE." and ending with "END {BRAND }MESSAGE". {BRAND } is optional. If isHTML is true, we html unescape the string first.

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, allow []rune) 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

type TextMarshaler added in v0.1.4

type TextMarshaler = stdencoding.TextMarshaler

TextMarshaler alias for encoding.TextMarshaler.

Jump to

Keyboard shortcuts

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