wiz

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2019 License: CC0-1.0 Imports: 24 Imported by: 0

README

Wiz Repository

This project collects generic sample go code, which can be used to accelerate other projects.

import gitlab.com/AMPK/wiz

wiz.Out("Charging my laser...") wiz.Success("Summoning was successful")

What's Here

Each file is a cluster of utility functions under a single theme:

  • Argon.go = Argon2 password hashing (create hash, and check match)
    • ArgonHashPassword(pw string) (string, error)
    • ArgonCheckPassword(pw string, hash string) bool
  • Client.go = Makes basic HTTP GET and POST requests. Can also send and receive structs directly with JSON.
    • Get(url string) ([]byte, error)
    • Post(url string, requestBody []byte) ([]byte, error)
    • StructGet(url string, responsePayload interface{}) error
    • StructPost(burl string, requestPayload interface{}, responsePayload interface{}) error
  • Console.go = Prints colored text to the console. Prompts for user input. Includes silent (password) input.
    • SilentPrompt(prompt string) string
    • Prompt(prompt string) string
    • Warning(bold string, a ...interface{})
    • Success(bold string, a ...interface{})
    • Out(a ...interface{})
  • Ed25519.go = Ed25519 Elliptic Curve Cryptography. (Public key) Lighter than RSA. More secure than ECDSA (see file).
    • NewEdKeyPair(seed []byte) ([]byte, []byte, error)
    • EdSign(data []byte, privateKey []byte, publicKey []byte) ([]byte, error)
    • EdVerify(data []byte, signature []byte, publicKey []byte) error
  • Files.go = Read and write to files, or make directories, from your code.
    • DeleteFile(file string) error
    • MkDir(dir string) error
    • WriteFile(file string, data []byte) error
    • ReadFile(file string) ([]byte, error)
  • Hash.go = SHA3-512 hash function and shortcut 'does the hash match' function.
    • Hash(data []byte) []byte
    • HashMatch(data []byte, hash []byte) bool
  • Hex.go = Convert bytes to uppercase hexadecimal strings and back.
    • BytesToHex(data []byte) string
    • HexToBytes(data string) ([]byte, error)
  • JSON.go = Convert data items in go (e.g. structs) to JSON. Also formats neatly.
    • MarshalNeat(payload interface{}) ([]byte, error)
    • StringMarshalNeat(payload interface{}) (string, error)
    • MarshalNeatMatch(data []byte, prototype interface{}) error
  • Random.go = Generate random bytes.
    • RandomBytes(len int) ([]byte, error)
  • SQLite.go = Create and use SQLite databases, specifically for simple key=INT, value=STRING tables
    • type Database struct
    • SQLiteOpen(dbName string) (Database, error)
    • Database.Close()
    • Database.MakeTable(tableName string) error
    • Database.ClearTable(tableName string) error
    • Database.AddItemAt(tableName string, primaryKey uint64, data string) error
    • Database.GetItemAt(tableName string, primaryKey uint64) (string, error)
    • Database.DeleteItemAt(tableName string, primaryKey uint64) error
    • Database.GetKeys(tableName string) ([]uint64, error)
    • Database.CheckOrder(tableName string) (uint64, error)
  • Server.go = Respond to incoming HTTP requests by passing them to the handler functions you pass.
    • Serve(addr string, getter func([]string) (int, []byte), poster func([]string, []byte) (int, []byte)) (string, error)
  • Sleep.go = Pause thread execution
    • Sleep(n int)
  • Time.go = Get the current unix time
    • Now() uint64

Why use this?

It's a collection of shortcut utilities all under one import. Make stuff faster with it.

Also it makes good example code to cannibalize.

Organization

Each file is named according to its primary function.

Each file contains a mini-readme at the top.

Author's policy: Each file must stand alone, so they can each be pasted into another project without breaking. Regrettably this approach makes mixing functionality (like using [JSON file] MarshalNeat in [Client file] StructPost) a no-go for me.

Files shall be arranged in the following order:

  • Imports
  • Constants & Variables
  • Exposed functions
  • Supporting functions

All functions will return structured errors whenever errors are returned, using github.com/pkg/errors (notably errors.Wrap and errors.New)

License

CC0 1.0

The person who associated a work with this deed has dedicated the work to the public domain by waiving all of his or her rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law.

You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Credits

Documentation

Index

Constants

View Source
const (
	PublicKeySize  = 32
	PrivateKeySize = 64
	SeedSize       = 32
)

Variables

This section is empty.

Functions

func ArgonCheckPassword

func ArgonCheckPassword(pw string, hash string) bool

func ArgonHashPassword

func ArgonHashPassword(pw string) (string, error)

func BytesToHex

func BytesToHex(data []byte) string

BytesToHex Converts byte slice to string representation (hexadecimal).

func DeleteFile

func DeleteFile(file string) error

DeleteFile deletes a file (or empty directory) at a location

func EdSign

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

func EdVerify

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

func Get

func Get(url string) ([]byte, error)

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 MarshalNeat

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

func MarshalNeatMatch

func MarshalNeatMatch(data []byte, prototype interface{}) error

func MkDir

func MkDir(dir string) error

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

func NewEdKeyPair

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

func Now

func Now() uint64

Now returns unix timestamp of now.

func Out

func Out(a ...interface{})

Prints grey / "Faint white". Spaces, not newlines, between items passed.

func Post

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

func Prompt

func Prompt(prompt string) string

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

func RandomBytes

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

RandomBytes returns byte slice of length n filled with random data

func ReadFile

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

ReadFile ReadFile

func Serve

func Serve(addr string, getter func([]string) (int, []byte), poster func([]string, []byte) (int, []byte)) (string, error)

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(n int)

Sleep sleeps for n seconds(int)

func SplitURL

func SplitURL(url string) []string

func StringMarshalNeat

func StringMarshalNeat(payload interface{}) (string, error)

func StructGet

func StructGet(url string, responsePayload interface{}) error

func StructPost

func StructPost(url string, requestPayload interface{}, responsePayload interface{}) error

func Success

func Success(bold string, a ...interface{})

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

func Warning

func Warning(bold string, a ...interface{})

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

func WriteFile

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

WriteFile WriteFile

Types

type Database

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

func SQLiteOpen

func SQLiteOpen(dbName string) (Database, error)

func (*Database) AddItemAt

func (db *Database) AddItemAt(tableName string, primaryKey uint64, data string) error

func (*Database) CheckOrder

func (db *Database) CheckOrder(tableName string) (uint64, error)

If your database is expected to have items at primary keys [1,2,3,4...] then this function will return the next open key following that order. Error is nil only if all (non-zero, non-negative) keys were in order with no gaps between them. IGNORES NEGATIVE AND ZERO KEYS.

func (*Database) ClearTable

func (db *Database) ClearTable(tableName string) error

func (*Database) Close

func (db *Database) Close()

func (*Database) DeleteItemAt

func (db *Database) DeleteItemAt(tableName string, primaryKey uint64) error

func (*Database) GetItemAt

func (db *Database) GetItemAt(tableName string, primaryKey uint64) (string, error)

func (*Database) GetKeys

func (db *Database) GetKeys(tableName string) ([]uint64, error)

func (*Database) MakeTable

func (db *Database) MakeTable(tableName string) error

Jump to

Keyboard shortcuts

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