arb

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: MIT Imports: 7 Imported by: 0

README

arb package

Installation

go get -u github.com/itrepablik/arb

Arb

Manages your arbitrary data to be securely encrypted and decrypted accordingly in your next Go's app. In most cases, you can use this when you want to encrypt your plain text password at any of your configuration files, or you wanted to encrypt your sensitive information before it submits to the online forms like login events at the client side.

Although the SSL is the frontline security protocol for our site, it's good to have a plan b type of security level just to ensure that when the SSL is not being renewed automatically, at least we have some degree of protection against the cyberattack when our vulnerable and valuable data being transmitted back and forth to our web server.

Usage

This is how you can use the arb package to generate secure encrypted text and able to decode it securely as well.

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/itrepablik/arb"
	"github.com/itrepablik/tago"
)

func main() {
	sensitiveData := "This is a secret message"

	// Generate a secure 32 bytes random salt
	secretKey, err := tago.GenerateSecretKey(32)
	if err != nil {
		log.Fatal(err)
	}

	ciphertext, iv, err := tago.Encrypt(sensitiveData, string(secretKey))
	if err != nil {
		log.Fatal(err)
	}

	// Create a new arb key
	arbData := arb.ArbData{
		ArbKey:    ciphertext,
		SecretKey: secretKey,
		IV:        iv,
		ExpiresIn: time.Now().Add(time.Minute * 30).Unix(), // If zero, the arb key will never expire
	}

	arbKey, err := arb.CreateArbKey(ciphertext, arbData)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("arbKey:", arbKey)

	// To decode the arb key, we need to know the secret key and iv stored in the memory of the server.
	decodedArbKey, err := arb.DecodeArbKey(arbKey)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("decodedArbKey:", decodedArbKey)

    	// To initialize the arb package to automatically removed expired arb keys stored 
    	// in the memories of the server
	arb.RunClearExpiredArbKeys()
}

For now, there is no persistence layer for arb keys in the database or local storage. This is the current limitation of the arb package and will be addressed in the future.

Subscribe to Maharlikans Code Youtube Channel:

Please consider subscribing to my Youtube Channel to recognize my work on any of my tutorial series. Thank you so much for your support! https://www.youtube.com/c/MaharlikansCode?sub_confirmation=1

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AF = ArbFacts{Facts: make(map[string][]byte)}

AF is the arbritary facts map

Functions

func ClearExpiredArbKeys

func ClearExpiredArbKeys()

ClearExpiredArbKeys removes all the expired arb keys

func CreateArbKey

func CreateArbKey(arbKey string, payLoad ArbData) (string, error)

CreateArbKey creates a new arbritary key

func DecodeArbKey

func DecodeArbKey(arbKey string) (string, error)

DecodeArbKey decodes the arbitrary key

func EncodePayload

func EncodePayload(payLoad ArbData) ([]byte, error)

EncodePayload encodes the arbitrary payload using gob

func RunClearExpiredArbKeys

func RunClearExpiredArbKeys()

RunClearExpiredArbKeys sets the 'ArbFacts' map

Types

type ArbData

type ArbData struct {
	ArbKey    string `json:"arb_key"`    // The encrypted arbritary data as key
	SecretKey string `json:"secret_key"` // The secret key to decrypt the arbritary data
	IV        []byte `json:"iv"`         // The initialization vector to decrypt the arbritary data
	ExpiresIn int64  `json:"expires_in"` // Unix timestamp, auto-set to 30 minutes
}

ArbData is the arbritary data model to be stored in the memory

func DecodePayload

func DecodePayload(arbKey string) (ArbData, error)

DecodePayload extracts the arbitrary payload

type ArbFacts

type ArbFacts struct {
	Facts map[string][]byte
	// contains filtered or unexported fields
}

ArbFacts is the arbritary facts model to be stored in the memory

func (*ArbFacts) Add

func (t *ArbFacts) Add(arbKey string, encBytes []byte) error

Add insert a new arbitrary into the 'ArbFacts' map

func (*ArbFacts) Get

func (t *ArbFacts) Get(arbKey string) ([]byte, bool)

Get gets the arbitrary from the 'ArbFacts' map by arb key

func (*ArbFacts) Remove

func (t *ArbFacts) Remove(arbKey string) (bool, error)

Remove any single stored arbitrary from the 'ArbFacts' map

type RawData

type RawData struct {
	SensitiveData string `json:"sensitive_data"`
}

RawData returns the raw arbritary data

Jump to

Keyboard shortcuts

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