cid

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

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

Go to latest
Published: Oct 7, 2019 License: MIT Imports: 5 Imported by: 0

README

CID

Coded/obfuscated IDs for when you don't want to expose your database keys when marshaling to JSON. Supports int64 and uint64.

It works by "hashing" the original identifier using a modular multiplicative inverse operation then encoding with Crockford's base32 when marshaling to JSON. This is reversible, so when you get a valid string, it is unmarshaled to the original id.

Install:

go get github.com/marcelomd/cid

Thanks

This borrows heavily from github.com/c2h5oh/hide and github.com/richardlehane/crock32. Thanks guys!

Set your own primes

It is recommended to set your own primes, so you don't end up with the same obfuscated IDs as everyone else. Xor is used to further scramble the resulting ID.

err := cid.Int64SetPrime(myPrime, myXor)

Example

Just use cid.Int64 or cid.Uint64 in place of the corresponding types you want obfuscated.

package main

import (
    "encoding/json"
    "fmt"
    "github.com/marcelomd/cid"
)

type User struct {
    Id   cid.Int64 `db:"id" json:"id"`
    Name string    `db:"name" json:"name"`
}

var (
    myPrime int64 = 0x1337133713373317
    myXor   int64 = 0x7b1ab1ab1ab1ab1a
)

func main() {
    fmt.Println("CID")
    err := cid.Int64SetPrime(myPrime, myXor)
    if err != nil {
        panic(err)
    }

    x := int64(123123123)
    y := Cid.EncodeInt64(x)
    z, err := Cid.DecodeInt64(y)
    if err != nil {
        panic(err)
    }
    fmt.Printf("Direct obfuscation: %d -> %s -> %d\n", x, y, z)

    fmt.Println("JSON marshal/unmarshal:")
    for i := 0; i < 10; i++ {
        toJson := User{Id: cid.Int64(i), Name: "Bla"}
        j, err := json.Marshal(toJson)
        if err != nil {
            panic(err)
        }

        fromJson := User{}
        err = json.Unmarshal(j, &fromJson)
        if err != nil {
            panic(err)
        }

        fmt.Printf("User: %s -> %s -> %s\n", toJson, string(j), fromJson)
    }

CID
Direct obfuscation: 123123123 -> 2T1F6M9DW0NRF -> 123123123
JSON marshal/unmarshal:
User: {0 Bla} -> {"id":"7P6NHNCDB3ART","name":"Bla"} -> {0 Bla}
User: {1 Bla} -> {"id":"6GBD2KG4RD60D","name":"Bla"} -> {1 Bla}
User: {2 Bla} -> {"id":"5TX4QRMYDZK9M","name":"Bla"} -> {2 Bla}
User: {3 Bla} -> {"id":"45FW81RHH8CJZ","name":"Bla"} -> {3 Bla}
User: {4 Bla} -> {"id":"3FHQXEXB6TST6","name":"Bla"} -> {4 Bla}
User: {5 Bla} -> {"id":"1P2EHQ1XA4N39","name":"Bla"} -> {5 Bla}
User: {6 Bla} -> {"id":"0GM62W5MZN6CG","name":"Bla"} -> {6 Bla}
User: {7 Bla} -> {"id":"7V6SQ5AE37KNV","name":"Bla"} -> {7 Bla}
User: {8 Bla} -> {"id":"658H82E1GGCX2","name":"Bla"} -> {8 Bla}
User: {9 Bla} -> {"id":"5FX8X8JV42R6N","name":"Bla"} -> {9 Bla}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOutOfRange      = errors.New("Prime is greater than max value for the type")
	ErrNotAPrime       = errors.New("It is not a prime number")
	ErrCannotModInvert = errors.New("Cannot mod invert")
)

Knuth multiplicative hash Adapted from github.com/c2h5oh/hide

View Source
var (
	ErrInvalidString = errors.New("Invalid character in string")
	ErrTooBig        = errors.New("String too big")
)
View Source
var ErrScanInt64 = errors.New("Incompatible type")
View Source
var ErrScanUint64 = errors.New("Incompatible type")

Functions

func DecodeInt64

func DecodeInt64(s string) (int64, error)

func DecodeUint64

func DecodeUint64(s string) (uint64, error)

func EncodeInt64

func EncodeInt64(n int64) string

func EncodeUint64

func EncodeUint64(n uint64) string

func Int64Hash

func Int64Hash(n int64) int64

func Int64SetPrime

func Int64SetPrime(prime, xor int64) error

func Int64ToString

func Int64ToString(n int64) string

func Int64Unhash

func Int64Unhash(n int64) int64

func SetLowerCase

func SetLowerCase()

func SetUpperCase

func SetUpperCase()

func StringToInt64

func StringToInt64(s string) (int64, error)

func StringToUint64

func StringToUint64(s string) (uint64, error)

func Uint64Hash

func Uint64Hash(n uint64) uint64

func Uint64SetPrime

func Uint64SetPrime(prime, xor uint64) error

func Uint64ToString

func Uint64ToString(n uint64) string

func Uint64Unhash

func Uint64Unhash(n uint64) uint64

Types

type Int64

type Int64 int64

func (Int64) MarshalJSON

func (id Int64) MarshalJSON() ([]byte, error)

func (*Int64) Scan

func (id *Int64) Scan(value interface{}) error

func (Int64) String

func (id Int64) String() string

func (*Int64) UnmarshalJSON

func (id *Int64) UnmarshalJSON(data []byte) error

func (Int64) Value

func (id Int64) Value() (driver.Value, error)

type Uint64

type Uint64 uint64

func (Uint64) MarshalJSON

func (id Uint64) MarshalJSON() ([]byte, error)

func (*Uint64) Scan

func (id *Uint64) Scan(value interface{}) error

func (Uint64) String

func (id Uint64) String() string

func (*Uint64) UnmarshalJSON

func (id *Uint64) UnmarshalJSON(data []byte) error

func (Uint64) Value

func (id Uint64) Value() (driver.Value, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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