util

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2019 License: MIT Imports: 13 Imported by: 0

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

func AllZero(b []byte) bool

func BigMax added in v0.0.9

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

BigMax returns the larger of x or y.

func BigMin added in v0.0.9

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

BigMin returns the smaller of x or y.

func BigPow added in v0.0.9

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

BigPow returns a ** b as a big integer.

func BigUint64 added in v0.0.9

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

func Bool2Bytes(b bool) []byte

func Byte added in v0.0.9

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

func BytesEqual(a, b []byte) bool

func BytesToString added in v0.0.9

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

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

func DecryptBytes added in v0.0.5

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

DecryptBytes decrypt raw json to raw

func Encrypt added in v0.0.5

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

func EncryptBytes added in v0.0.5

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

EncryptBytes encrypts raw by passphrase to json binary

func Exp added in v0.0.9

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

func Int2Bytes(i int64) []byte

func Int64ToBytes added in v0.0.9

func Int64ToBytes(i int64) []byte

func JoinBytes added in v0.0.9

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

func LeftPadBytes

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

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

func Max added in v0.0.9

func Max(x, y uint64) uint64

func Min added in v0.0.9

func Min(x, y uint64) uint64

func PaddedBigBytes added in v0.0.9

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

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

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

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

SafeAdd returns the result and whether overflow occurred.

func SafeMul added in v0.0.9

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

SafeMul returns multiplication result and whether overflow occurred.

func SafeSub added in v0.0.9

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

SafeSub returns subtraction result and whether overflow occurred.

func String2Bytes added in v0.0.9

func String2Bytes(s string) []byte

func StringToBigInt added in v0.0.9

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 added in v1.0.1

func ToIndentString(v interface{}) string

func ToString added in v0.0.5

func ToString(v interface{}) string

func ToWordSize added in v0.0.9

func ToWordSize(size uint64) uint64

ToWordSize returns the ceiled word size required for memory expansion.

func TrimBuffToString added in v0.0.9

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

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

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

func Uint64ToBytes added in v0.0.9

func Uint64ToBytes(i uint64) []byte

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