goobfuscated

package module
v0.0.0-...-b2d0753 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

README

Go Obfuscated ID

  • ID is a value object that use for identify a domain object. The domain entity is only aware of itself, and can never reach across its own object boundaries to find out if an ID it has generated is actually unique. Once a new entity gets persisted as a record in the database it will get an ID. That is, the entity has no identity until it has been persisted.

PROCESS

  • DB Query Result
    ID      Name
    100     golang
  • Server Obfuscated & Response
    // used obfuscated.ID receive
    type Result struct{
        ID obfuscated.ID `db:"id"`
        Name string `db:"name"`
    }
  • Client Received
    {
        "id":"Q7HnWooQAgA",
        "name":"golang"
    }
  • Client Send
    {
        "id":"Q7HnWooQAgA",
        "name":"golang"
    }
  • Server Received
    type Request struct{
        ID obfuscated.ID `db:"id"`
        Name string `db:"name"`
    }
    // Parse
    // ID > 100
    // Name > golang

USAGE

request model

import (
    obfuscated "github.com/19byte/goobfuscated"
)

// RequestModel http request parameters
type RequestModel struct {
    ID   obfuscated.ID `json:"id" query:"id"`
    Name string        `json:"name" query:"name"`
}

// DBModel database query model
type DBModel struct {
    ID obfuscated.ID   obfuscated.ID `json:"id"`
    Name string        `json:"name"`
}

func GetModel(r *http.Request,w http.ResponseWriter)error  {
    // obfuscated.ID Implemented MarshalJSON & UnmarshalJSON interface
    m := &RequestModel{}
    if e := json.NewDecoder(r.Body).Decode(&m);e != nil {
        panic(e)
    }
    s := &DBModel{}
    db.QueryRow(r.Context(),`SELECT id,name FROM model`).Scan(&s.ID,&s.Name)
    return json.NewEncoder(w).Encode(s)
}

Documentation

Index

Constants

View Source
const (
	// MaxInt represents the upper bound.
	// It should be 2^N.
	// It is set by default to the upper bound of an (2^53 - 1)
	// which represents the maximum safe integer(Number.MAX_SAFE_INTEGER) in JavaScript.
	MaxInt = 1<<53 - 1 // 2,147,483,647

	// MillerRabin is used to configure the ProbablyPrime function
	// which is used to verify prime numbers.
	// See: https://golang.org/pkg/math/big/#Int.ProbablyPrime
	MillerRabin = 20
)

Variables

This section is empty.

Functions

func DeObfuscate

func DeObfuscate(n uint64) uint64

DeObfuscate is used to decode n back to the original id. It will only decode correctly if the prime selectors is consistent with what was used to encode n.

func Obfuscate

func Obfuscate(id uint64) uint64

Obfuscate is used to encode id using Knuth's hashing algorithm.

Types

type ID

type ID uint64

func ParseID

func ParseID(s string) (ID, error)

ParseID is an inverse operation of ID.String(), returns zero if any error occurs during parsing.

func (*ID) Decode

func (id *ID) Decode(n uint64)

func (*ID) Encode

func (id *ID) Encode() uint64

Encode & Decode obfuscate and deObfuscate the ID.

func (*ID) IsZero

func (id *ID) IsZero() bool

IsZero reports if the id is the zero value.

func (*ID) MarshalJSON

func (id *ID) MarshalJSON() ([]byte, error)

MarshalJSON satisfies json.Marshaller and transparently obfuscates the value using Default prime

func (*ID) String

func (id *ID) String() string

String returns the obfuscated id in base64 string format and with little-endian byte order.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON satisfies json.Marshaller and transparently deobfuscates the value using inverse of Default prime

func (*ID) Value

func (id *ID) Value() uint64

Value returns the raw integer value.

Jump to

Keyboard shortcuts

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