wiz

package module
v0.0.0-...-6170c7b Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2021 License: CC0-1.0 Imports: 25 Imported by: 2

README

Wiz Repository

Collection of boilerplate Go code

Best practices note:

It is best practice for go packages to have a single purpose which is evident from their name (e.g. net/http)

This is the exact opposite of ambiguously named utilities packages, like this one.

Nevertheless, I have yet to encounter a single project which could not be vastly accelerated by a utilities package. Sacrifices must be made.

Exposed Functions By File

AES.go

AESEncrypt(data []byte, key []byte) ([]byte, error)
AESDecrypt(stream []byte, key []byte) ([]byte, error)

Args.go

Args() []string //Return command line arguments passed to program

ASCII.go

ASCII(b []byte) (string, bool)
Printable(b []byte) (string, bool)
StripNonASCII(in string) string
StripNonPrintableASCII(in string) string

Console.go

SilentPrompt(prompt string) string
Prompt(prompt string) string
Red(items ...interface{})
Yellow(items ...interface{})
Green(items ...interface{})
Blue(items ...interface{})
Purple(items ...interface{})
White(items ...interface{})
Print(items ...interface{})

Ed25519.go

NewEdKeyPair(seed []byte) ([]byte, []byte, error)
EdSign(data, publicKey, privateKey []byte) ([]byte, error)
EdVerify(data, signature, publicKey []byte) error

Files.go

Executable() string
ProgramName() string
Dir() string
FileExists(file string) bool
FolderExists(file string) bool
DeleteFile(file string) error
MkDir(dir string) error
WriteFile(file string, data []byte) error
ReadFile(file string) ([]byte, error)

Hash.go

Hash(data []byte) []byte
HashMatch(data []byte, hash []byte) bool

Hex.go

BytesToHex(data []byte) string
HexToBytes(data string) ([]byte, error)

HTTP.go

SplitURL(url string) []string
ServeSimple(ln net.Listener, getter func([]string) (int, []byte), poster func([]string, []byte) (int, []byte)) error
NewClient(c *http.Client, timeout int) Client

type Client
Client.Get(url string) ([]byte, error)
Client.GetStruct(url string, responseVessel interface{}) error
Client.Post(url string, requestBody []byte) ([]byte, error)
Client.PostStruct(url string, requestPayload interface{}, responseVessel interface{}) error

JSON.go

CompactJSON(data []byte) ([]byte, error)
NeatJSON(data []byte) ([]byte, error)
Marshal(payload interface{}) ([]byte, error)
MarshalNeat(payload interface{}) ([]byte, error)

Random.go

RandomBytes(len int) ([]byte, error)

Strings.go

Lowercase(string) string
Uppercase(string) string
String(interface{}) string

Time.go

Now() uint64
Sleep(seconds int)

Uint64.go

Uint64(interface{}) (uint64, error)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESDecrypt

func AESDecrypt(stream []byte, key []byte) ([]byte, error)

Decrypts given data using a key. AES256, GCM mode.

func AESEncrypt

func AESEncrypt(data []byte, key []byte) ([]byte, error)

Encrypts given data using a key. AES256, GCM mode. Uses crypto/rand for nonce.

func ASCII

func ASCII(b []byte) (string, bool)

Converts binary to ASCII - returns false if non-ASCII bytes found

func Antipanic

func Antipanic(e *error, label string)

Antipanic - usage: defer Antipanic(&e, "label") at the beginning of any function. Label is usually the name of the function prefixed by its package name, e.g. "package.func". This results in traditionally wrapped errors (mimics errors.Wrap). Main purpose: deferring this function transforms panics into errors when they occur inside the function it was deferred in, and requires only one line. Additionally it allows gung-ho use of panics inside parent function to replace error checking, which can shorten code somewhat (as panics force a return).

func Args

func Args() []string

Returns the command line arguments passed to the program. Omits the first element of os.Args, which is the program's filename.

func Assert

func Assert(condition bool, label string)

Panics if a condition is false. The "label" string is the message of the panic.

func Blue

func Blue(items ...interface{})

Prints blue. The first item passed is bolded. Each item gets a new line.

func BytesToHex

func BytesToHex(data []byte) string

BytesToHex Converts byte slice to uppercase hexadecimal string representation

func Check

func Check(e error)

Panics if an error is non-nil. Use after defer Antipanic to shorten code.

func CompactJSON

func CompactJSON(input []byte) ([]byte, error)

Takes some json and removes unnecessary whitespace

func DeleteFile

func DeleteFile(file string) error

DeleteFile deletes a file (or empty directory) at a given relative path

func Dir

func Dir() string

Returns the absolute path of the directory containing the executable

func EdSign

func EdSign(data, publicKey, privateKey []byte) ([]byte, error)

Signs data using an Ed25519 key pair. While only the private key is needed for signing, this function also verifies the signature using the public key provided and returns an error if verification failed, thus guarding against misuse.

func EdVerify

func EdVerify(data, signature, publicKey []byte) error

Verifies that a 64byte signature is a valid Ed25519 signature of given data (of any length) using a 32 byte Ed25519 public key. Error is non-nil only if verification succeeds.

func Executable

func Executable() string

Returns full path + filename of currently running executable

func FileExists

func FileExists(file string) bool

Returns true if a file exists at a given relative path

func FolderExists

func FolderExists(file string) bool

Returns true if a folder exists at a given relative path

func Green

func Green(items ...interface{})

Prints green. The first item passed is bolded. Each item gets a new line.

func Hash

func Hash(data []byte) []byte

Hash returns the SHA3-512 of a given byte slice

func HashMatch

func HashMatch(data []byte, hash []byte) bool

HashMatch checks if the SHA3-512 of a given byte slice matches a given 64 byte array

func HexToBytes

func HexToBytes(data string) ([]byte, error)

HexToBytes Converts string to byte slice if it is valid hexadecimal. Slice length is zero on fail.

func Lowercase

func Lowercase(s string) string

Converts string to lowercase

func Marshal

func Marshal(payload interface{}) ([]byte, error)

Takes an object, and turns it into JSON

func MarshalNeat

func MarshalNeat(payload interface{}) ([]byte, error)

Takes an object, and turns it into neatly formatted JSON

func MkDir

func MkDir(dir string) error

MkDir Creates a folder with specified relative path + name ('dir') with 0755 permissions

func NeatJSON

func NeatJSON(input []byte) ([]byte, error)

Takes some json and adds newlines and three-space indentation for readability

func NewEdKeyPair

func NewEdKeyPair(seed []byte) ([]byte, []byte, error)

Creates a new Ed25519 keypair from a given 32 byte seed. The seed should be generated securely (e.g. using crypto/rand) The returns are public key, then private key. Don't mess up the order.

func Now

func Now() uint64

Now returns unix timestamp of now.

func Panic

func Panic(s string)

Panics with a string message

func Print

func Print(items ...interface{})

Prints normally - does not set or unset any color parameters.

func Printable

func Printable(b []byte) (string, bool)

Converts binary to printable string - returns false if non-printable characters found

func ProgramName

func ProgramName() string

Returns just the name of the running executable (no path)

func Prompt

func Prompt(prompt string) string

Displays a prompt to the interface and returns what the user enters

func Purple

func Purple(items ...interface{})

Prints magenta. The first item passed is bolded. Each item gets a new line.

func RandomBytes

func RandomBytes(len int) ([]byte, error)

RandomBytes returns byte slice of length n filled with random data (uses crypto/rand)

func ReadFile

func ReadFile(file string) ([]byte, error)

ReadFile read a whole file at a given relative path and returns it as []byte

func Red

func Red(items ...interface{})

Prints red. The first item passed is bolded. Each item gets a new line.

func ServeSimple

func ServeSimple(ln net.Listener, getter func([]string) (int, []byte), poster func([]string, []byte) (int, []byte)) error

Serve on a given address, and forward GET and POST requests to the separate handlers provided

func SilentPrompt

func SilentPrompt(prompt string) string

Displays a prompt to the interface and returns what the user enters. Hides user input from the console. Use for password entry for example.

func Sleep

func Sleep(seconds int)

Sleep sleeps for n seconds(int)

func SplitURL

func SplitURL(url string) []string

Splits a url at '/' characters

func StipNonASCII

func StipNonASCII(in string) string

Strips non-ascii characters from a string

func String

func String(input interface{}) string

Attmepts to convert an object to string. Tries type assertion to string first, then looks for a .Stringer interface, then handles byte slices and arrays as base64, and finally attempts json marshaling. If none of those workes, returns a string representation of the input's type using reflect package.

func StripNonPrintableASCII

func StripNonPrintableASCII(in string) string

Strips non-printable and non-ascii characters from a string

func Uint64

func Uint64(input interface{}) (uint64, error)

Converts certain number types and strings to uint64. Returns an error on failed conversion explaining why.

func Uppercase

func Uppercase(s string) string

Converts string to upperxase

func White

func White(items ...interface{})

Prints white. The first item passed is bolded. Each item gets a new line.

func WriteFile

func WriteFile(file string, data []byte) error

WriteFile writes to (or overwrites) a file at a given relative path

func Yellow

func Yellow(items ...interface{})

Prints yellow. The first item passed is bolded. Each item gets a new line.

Types

type Client

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

Wrapper for http client with additional methods

func NewClient

func NewClient(c *http.Client, timeout int) Client

Creates a new Client from an http.Client, with a request timeout in seconds. Changes any negative or zero timeout to 1 second. Passing nil client causes the function to use the default &http.Client{} (then add the timeout)

func (*Client) Get

func (c *Client) Get(url string) ([]byte, error)

Classic HTTP Get, returning response body (error if status code outside 200-299 range)

func (*Client) GetStruct

func (c *Client) GetStruct(url string, responseStruct interface{}) error

HTTP Get, but immediately unmarshals response body into a struct if 200-299 status. Note that json.Unmarshal requires the responseStruct be passed as a pointer to function properly

func (*Client) Post

func (c *Client) Post(url string, requestBody []byte) ([]byte, error)

Classic HTTP Post, returning response body (error if status code outside 200-299 range)

func (*Client) PostStruct

func (c *Client) PostStruct(url string, requestStruct interface{}, responseStruct interface{}) error

HTTP Post, but automatically marshals a struct as request body, and immediately unmarshals response body into a struct if 200-299 status. Note that json.Unmarshal requires the responseStruct be passed as a pointer to function properly

Jump to

Keyboard shortcuts

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