util

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2020 License: MIT Imports: 15 Imported by: 2

Documentation

Overview

Package common contains various helper functions.

Index

Constants

View Source
const (
	// StandardScryptN is the N parameter of Scrypt encryption algorithm, using 256MB
	// memory and taking approximately 1s CPU time on a modern processor.
	StandardScryptN = 1 << 18

	// StandardScryptP is the P parameter of Scrypt encryption algorithm, using 256MB
	// memory and taking approximately 1s CPU time on a modern processor.
	StandardScryptP = 1
)
View Source
const (
	MaxUint64 = uint64(1<<64 - 1)

	// number of bits in a big.Word
	WordBits = 32 << (uint64(^big.Word(0)) >> 63)
	// number of bytes in a big.Word
	WordBytes = WordBits / 8
	// number of bytes in a vm word
	WordSize = 32
)

Variables

View Source
var (
	Big0   = big.NewInt(0)
	Big1   = big.NewInt(1)
	Big2   = big.NewInt(2)
	Big10  = big.NewInt(10)
	Big31  = big.NewInt(31)
	Big32  = big.NewInt(32)
	Big256 = big.NewInt(256)
	Big257 = big.NewInt(257)

	Tt255   = BigPow(2, 255)
	Tt256   = BigPow(2, 256)
	Tt256m1 = new(big.Int).Sub(Tt256, big.NewInt(1))
)

Functions

func AllZero

func AllZero(b []byte) bool

func BE_Bytes2Int

func BE_Bytes2Int(buf []byte) int64

func BE_BytesToUint32

func BE_BytesToUint32(buf []byte) uint32

func BE_BytesToUint64

func BE_BytesToUint64(buf []byte) uint64

func BE_Int2Bytes

func BE_Int2Bytes(i int64) []byte

func BE_Uint32ToBytes

func BE_Uint32ToBytes(i uint32) []byte

func BE_Uint64ToBytes

func BE_Uint64ToBytes(i uint64) []byte

func BigMax

func BigMax(x, y *big.Int) *big.Int

BigMax returns the larger of x or y.

func BigMin

func BigMin(x, y *big.Int) *big.Int

BigMin returns the smaller of x or y.

func BigPow

func BigPow(a, b int64) *big.Int

BigPow returns a ** b as a big integer.

func BigUint64

func BigUint64(v *big.Int) (uint64, bool)

BigUint64 returns the integer casted to a uint64 and returns whether it overflowed in the process.

func Bool2Bytes

func Bool2Bytes(b bool) []byte

func Byte

func Byte(bigint *big.Int, padlength, n int) byte

Byte returns the byte at position n, with the supplied padlength in Little-Endian encoding. n==0 returns the MSB Example: bigint '5', padlength 32, n=31 => 5

func Bytes2Hex

func Bytes2Hex(d []byte) string

Bytes2Hex returns the hexadecimal encoding of d.

func BytesEqual

func BytesEqual(a, b []byte) bool

func BytesToString

func BytesToString(data []byte) string

func CopyBytes

func CopyBytes(b []byte) (copiedBytes []byte)

CopyBytes returns an exact copy of the provided bytes.

func CreateDirIfNotExist

func CreateDirIfNotExist(dir string) error

CreateDirIfNotExist create given folder

func Decrypt

func Decrypt(cryptograph string, passphrase string) (string, error)

func DecryptBytes

func DecryptBytes(encryptedJSON []byte, passphrase []byte) ([]byte, error)

DecryptBytes decrypt raw json to raw

func Encrypt

func Encrypt(raw string, passphrase string) (string, error)

func EncryptBytes

func EncryptBytes(raw []byte, passphrase []byte) ([]byte, error)

EncryptBytes encrypts raw by passphrase to json binary

func Exp

func Exp(base, exponent *big.Int) *big.Int

Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits.

func FromHex

func FromHex(s string) []byte

FromHex returns the bytes represented by the hexadecimal string s. s may be prefixed with "0x".

func Hash

func Hash(size int, data ...[]byte) []byte

func Hash256

func Hash256(data ...[]byte) []byte

func Hex2Bytes

func Hex2Bytes(str string) []byte

Hex2Bytes returns the bytes represented by the hexadecimal string str.

func Hex2BytesFixed

func Hex2BytesFixed(str string, flen int) []byte

Hex2BytesFixed returns bytes of a specified fixed length flen.

func Hex32ToBytes

func Hex32ToBytes(s string) [32]byte

func Hex64ToBytes

func Hex64ToBytes(s string) [64]byte

func HexToBytes

func HexToBytes(s string) []byte

func JoinBytes

func JoinBytes(data ...[]byte) []byte

func LE_BytesToInt64

func LE_BytesToInt64(buf []byte) int64

func LE_BytesToUint32 added in v1.3.0

func LE_BytesToUint32(buf []byte) uint32

func LE_BytesToUint64

func LE_BytesToUint64(buf []byte) uint64

func LE_EncodeVarInt added in v1.3.0

func LE_EncodeVarInt(val uint64) []byte

func LE_Int64ToBytes

func LE_Int64ToBytes(i int64) []byte

func LE_Uint32ToBytes added in v1.3.0

func LE_Uint32ToBytes(i uint32) []byte

func LE_Uint64ToBytes

func LE_Uint64ToBytes(i uint64) []byte

func LeftPadBytes

func LeftPadBytes(slice []byte, l int) []byte

LeftPadBytes zero-pads slice to the left up to length l.

func Max

func Max(x, y uint64) uint64

func Min

func Min(x, y uint64) uint64

func PaddedBigBytes

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func RandomFixedBytes added in v1.4.0

func RandomFixedBytes(length int) []byte

func RandomFixedString

func RandomFixedString(length int) string

func ReadBits

func ReadBits(bigint *big.Int, buf []byte)

ReadBits encodes the absolute amount of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.

func ReverseBytes

func ReverseBytes(str []byte) (result []byte)

func RightPadBytes

func RightPadBytes(slice []byte, l int) []byte

RightPadBytes zero-pads slice to the right up to length l.

func S256

func S256(x *big.Int) *big.Int

S256 interprets x as a two's complement number. x must not exceed 256 bits (the result is undefined if it does) and is not modified.

S256(0)        = 0
S256(1)        = 1
S256(2**255)   = -2**255
S256(2**256-1) = -1

func SafeAdd

func SafeAdd(x, y uint64) (uint64, bool)

SafeAdd returns the result and whether overflow occurred.

func SafeMul

func SafeMul(x, y uint64) (uint64, bool)

SafeMul returns multiplication result and whether overflow occurred.

func SafeSub

func SafeSub(x, y uint64) (uint64, bool)

SafeSub returns subtraction result and whether overflow occurred.

func String2Bytes

func String2Bytes(s string) []byte

func StringToBigInt

func StringToBigInt(str *string) (*big.Int, error)

func ToHex deprecated

func ToHex(b []byte) string

ToHex returns the hex representation of b, prefixed with '0x'. For empty slices, the return value is "0x0".

Deprecated: use hexutil.Encode instead.

func ToIndentString

func ToIndentString(v interface{}) string

func ToString

func ToString(v interface{}) string

func ToWordSize

func ToWordSize(size uint64) uint64

ToWordSize returns the ceiled word size required for memory expansion.

func TrimBuffToString

func TrimBuffToString(bytes []byte) string

trim the '\00' byte

func TrimQuotes

func TrimQuotes(s string) string

TrimQuotes trim quotes of string if quotes exist

func U256

func U256(x *big.Int) *big.Int

U256 encodes as a 256 bit two's complement number. This operation is destructive.

Types

This section is empty.

Directories

Path Synopsis
Package hexutil implements hex encoding with 0x prefix.
Package hexutil implements hex encoding with 0x prefix.

Jump to

Keyboard shortcuts

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