memshare

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2023 License: MIT Imports: 15 Imported by: 0

README

Go MemShare

donation link

A simple go module for sharing memory pointers as encoded strings.

This module not only converts a pointer to a string and back again, but also verifies if the receiving end is converting to a valid pointer and to the correct var type.

You may optionally encrypt the stringified pointer to keep the memory address safe (uses aes-cfb encryption method).

Installation

go get github.com/AspieSoft/go-memshare

Usage


import (
  "github.com/AspieSoft/go-memshare"
)

func main(){
  myVar := "hello, world"

  // get a new stringified pointer (also pushes the pointer to persistant memory)
  pointer := memshare.Stringify[string](&myVar)

  // optional: encrypt the pointer string with a key
  encryptedPointer := memshare.Stringify[string](&myVar, "myKey123")

  anotherInstance(pointer, encryptedPointer)

  fmt.Println(myVar) // "hello, there"

  // allow the garbage collector to remove this pointer automatically (removes it from the persistant list)
  memshare.Delete(&myVar)
}

func anotherInstance(pointer string, encryptedPointer string){
  // parse the stringified pointer to a new var (note: the garbage collector may ignore this var)
  myVar := memshare.Parse[string](pointer)
  if myVar == nil {
    panic("pointer was invalid or not converted to the correct type")
  }

  // you will need to pass your encryption key if you set one
  myVar := memshare.Parse[string](encryptedPointer, "myKey123")
  if myVar == nil {
    panic("pointer may have also failed to decrypt")
  }

  fmt.Println(*myVar) // "hello, world"

  *myVar = "hello, there"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete[T any](v *T)

Delete removes a var from persistant memory, and allows it to be handled by the garbage collector

notice: be careful when deleting a pointer from memory (parsed pointers might not allocate memory)

func Parse

func Parse[T any](ps string, key ...interface {
	CryptKey | string | []byte | any
}) *T

Parse converts a stringified pointer back to a pointer attached to a new var

notice: this new var may be ignored by the garbage collector

func RandBytes

func RandBytes(size int) string

RandBytes generates random bytes using crypto/rand

func Stringify

func Stringify[T any](v *T, key ...interface {
	CryptKey | string | []byte | any
}) (string, error)

Stringify converts a vars pointer to a string and pushes it to persistant memory

Types

type CryptKey

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

func KeyFile

func KeyFile(path string) (*CryptKey, error)

KeyFile grabs a file, and loads up an encryption key to be used in the parse and stringify functions

this method will return a struct, which contains the key as a private param

Jump to

Keyboard shortcuts

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