Aismail7 / Go Helpers
The Aismail7/GoHelpers
package is a collection of functions in the go language.
Table of Contents
Install
To use The Aismail7/GoHelpers
package, you must follow the steps below:
go get -u github.com/Aismail7/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": "Ismail", "Last Name": "Ahmad"}))
Output:
{"First Name":"Ismail","Last Name":"Ahmad"}
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, "Ismail")
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("Ismail" + gohelpers.GetNewLine() + "Ahmad")
Output:
Andrea
Adam
Merge Maps
map1 := map[string]interface{}{"FirstName": "Ismail", "LastName": "Ahmad"}
map2 := map[string]interface{}{"Age": 21}
map3 := map[string]interface{}{"FirstName": "Ismail", "MidName": nil, "LastName": "Ahmad"}
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{"Ismail", "Ahmad"}, "_", 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{"Ismail", "Ahmad"}, "_", 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("Ismail Ahmad", 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("Ismail Ahmad", 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
Ismail Ahmad - Aismail7
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/.