gohelpers

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2021 License: MIT Imports: 16 Imported by: 4

README

MrAndreID / Go Helpers

Go Reference

The MrAndreID/GoHelpers package is a collection of functions in the go language.


Table of Contents


Install

To use The MrAndreID/GoHelpers package, you must follow the steps below:

go get -u github.com/MrAndreID/gohelpers

Usage

Create an Error Message
gohelpers.ErrorMessage("error loading the .env file", ".env file not found.")

Output:

2021-02-15 18:45:18 [ ERROR ] Message : error loading the .env file.
2021-02-15 18:45:18 [ ERROR ] Detail : .env file not found.
JSON Encode
fmt.Println(gohelpers.JSONEncode(map[string]interface{}{"First Name": "Andrea", "Last Name": "Adam"}))

Output:

{"First Name":"Andrea","Last Name":"Adam"}
Generate a Bytes
fmt.Println(gohelpers.Bytes(4))

Output:

[95 113 200 231]
Generate a Random Bytes
fmt.Println(gohelpers.RandomByte(32))

Output:

1UlrTYbNJioQPyBEKpV5BFtgqV6t5fEvjSaO8ApGRHs=
Generate a Random Strings
fmt.Println(gohelpers.Random("str", 10))

Output:

XBMUH3qvXh
Generate a Random Int
fmt.Println(gohelpers.Random("int", 4))

Output:

6111
Generate Key
fmt.Println(gohelpers.GenerateKey(32))

Output:

7f2f9d692d200e20133428c832b80f8e21702437fcd28ba2ac8c5aaa3a978b2d
Encrypt
key := gohelpers.GenerateKey(32)

encryptedData, err := gohelpers.Encrypt(key, "Andrea Adam")
if err != nil {
    gohelpers.ErrorMessage("something went wrong when encrypting data", err)
}

fmt.Println(encryptedData)

Output:

b9ab3d8bde4092791b50142be86dfdc70688d81f42fa4aa06c88bcb1af6dfaa4f6c920ec157874
Decrypt
key := gohelpers.GenerateKey(32)

plainText, err := gohelpers.Decrypt(key, "b9ab3d8bde4092791b50142be86dfdc70688d81f42fa4aa06c88bcb1af6dfaa4f6c920ec157874")
if err != nil {
    gohelpers.ErrorMessage("something went wrong when decrypting data", err)
}

fmt.Println(plainText)

Output:

Andrea Adam
Get New Line
fmt.Println("Andrea" + gohelpers.GetNewLine() + "Adam")

Output:

Andrea
Adam
Merge Maps
map1 := map[string]interface{}{"FirstName": "Andrea", "LastName": "Adam"}
map2 := map[string]interface{}{"Age": 21}
map3 := map[string]interface{}{"FirstName": "Andrea", "MidName": nil, "LastName": "Adam"}

fmt.Println(gohelpers.MergeMaps(map1, map2, map3))

Output:

map[Age:21 FirstName:Andrea LastName:Adam MidName:<nil>]
Generate Encrypted Key
key := gohelpers.GenerateKey(32)

encryptedKey, err := gohelpers.GenerateEncryptedKey([]string{"Andrea", "Adam"}, "_", key)
if err != nil {
    gohelpers.ErrorMessage("something went wrong when generating encrypted key", err)
}

fmt.Println(encryptedKey)

Output:

ccaa9be63b4699a53166e1cf4a0086ff3ced25dca2f0672b9cb22309f5270087e7947cb643e579
Generate Encrypted Key With Datetime
key := gohelpers.GenerateKey(32)

encryptedKey, err := gohelpers.GenerateEncryptedKeyWithDatetime([]string{"Andrea", "Adam"}, "_", key, time.Now())
if err != nil {
    gohelpers.ErrorMessage("something went wrong when generating encrypted key with datetime", err)
}

fmt.Println(encryptedKey)

Output:

4e9206fc51372eb6f983a06abdfdb23e7cff0cf32d8f418997428547ed6aef438274fa6054e9d63c96c9c929a6da4c2268700da5d0fe3f06f348a3
Ungenerate Encrypted Key
key := gohelpers.GenerateKey(32)

data, err := gohelpers.UngenerateEncryptedKey("ccaa9be63b4699a53166e1cf4a0086ff3ced25dca2f0672b9cb22309f5270087e7947cb643e579", "_", key)
if err != nil {
    gohelpers.ErrorMessage("something went wrong when ungenerating encrypted key", err)
}

fmt.Println(data)

Output:

[Andrea Adam]
Generate Hash And Salt
key := gohelpers.GenerateKey(32)

encryptedHash, encryptedSalt, err := gohelpers.GenerateHashAndSalt("Andrea Adam", 32, key, 5)
if err != nil {
    gohelpers.ErrorMessage("something went wrong when generating hash and salt", err)
}

fmt.Println("Hash : " + encryptedHash + ", Salt : " + encryptedSalt)

Output:

Hash : 18f65095a8a2ad99851072aee8801c73eea67e2fb18866e8b96aaf4fdd996a879ee1a7987dbd2e9cf6803de30b8224eec77c63e0b9fac91e8c36b1c7fbe54589bd28bec89c774d3f6b8ea7b411d6edd8ef07630cf9689e4b, Salt : 2b4e42cf347c74ff1b1e28b99a3d50102313b022b5eac24ba1ba1287c3913c4d878cc94ae3def9908f6574e9e2d78777c993dc147dc93a7e13eccd9ecc418e4205acb763bc623693
Verify Hash And Salt
key := gohelpers.GenerateKey(32)

encryptedHash, encryptedSalt, err := gohelpers.GenerateHashAndSalt("Andrea Adam", 32, key, 5)
if err != nil {
    gohelpers.ErrorMessage("something went wrong when generating hash and salt", err)
}

fmt.Println(gohelpers.VerifyHashAndSalt("Andrea Adam", encryptedHash, encryptedSalt, key))

Output:

true

Full Example

Full Example can be found on the Go Playground website.

Versioning

I use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

Andrea Adam - MrAndreID

License

MIT licensed. See the LICENSE file for details.

Official Documentation for Go Language

Documentation for Go Language can be found on the Go Language website.

More

Documentation can be found on https://go.dev/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes added in v1.1.0

func Bytes(n int) []byte

func Decrypt added in v1.3.0

func Decrypt(key string, encryptedString string) (string, error)

func Encrypt added in v1.3.0

func Encrypt(key string, plainText string) (string, error)

func ErrorMessage

func ErrorMessage(message string, err interface{})

func GenerateEncryptedKey added in v1.5.0

func GenerateEncryptedKey(data []string, separator, key string) (string, error)

func GenerateEncryptedKeyWithDatetime added in v1.5.0

func GenerateEncryptedKeyWithDatetime(data []string, separator, key string, date time.Time) (string, error)

func GenerateHashAndSalt added in v1.5.0

func GenerateHashAndSalt(plainText string, customByte int, key string, cost int) (string, string, error)

func GenerateKey added in v1.3.0

func GenerateKey(length int) string

func GetNewLine added in v1.4.0

func GetNewLine() string

func JSONEncode added in v1.1.0

func JSONEncode(data interface{}) string

func MergeMaps added in v1.5.0

func MergeMaps(maps ...map[string]interface{}) map[string]interface{}

func Random added in v1.1.1

func Random(randomType string, length int) string

func RandomByte added in v1.1.0

func RandomByte(n int) string

func UngenerateEncryptedKey added in v1.5.0

func UngenerateEncryptedKey(data, separator, key string) ([]string, error)

func VerifyHashAndSalt added in v1.5.0

func VerifyHashAndSalt(data, encryptedHash, encryptedSalt, key string) bool

Types

This section is empty.

Jump to

Keyboard shortcuts

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