ID type with marshalling to/from hash to prevent sending IDs to clients.

Overview

Hide IDs

Go Reference CircleCI Go Report Card Chat on Discord

Hide is a simple package to provide an ID type that is marshalled to/from a hash string. This prevents sending technical IDs to clients and converts them on the API layer. Hide uses hashids as its default hash function. But you can provide your own by implementing the Hash interface and configure it using hide.UseHash.

Read our full article on Medium.

Installation

go get github.com/emvi/hide

Example

Consider the following struct:

type User struct {
    Id       int64  `json:"id"`
    Username string `json:"username"`
}

When marshalling this struct to JSON, the ID will be represented by a number:

{
    "id": 123,
    "username": "foobar"
}

In this case you expose the technical user ID to your clients. By changing the type of the ID, you get a better result:

type User struct {
    Id       hide.ID `json:"id"`
    Username string  `json:"username"`
}

Notice that the int64 ID got replaced by the hide.ID, which internally is represented as an int64 as well, but implements the marshal interface. This allows you to cast between them and use hide.ID as a replacement. The resulting JSON changes to the following:

{
  "id": "beJarVNaQM",
  "username": "foobar"
}

If you send the new ID (which is a string now) back to the server and unmarshal it into the hide.ID type, you'll get the original technical ID back. It's also worth mentioning that a value of 0 is translated to null when an ID is marshalled to JSON or stored in database.

View the full demo

Contribute

See CONTRIBUTING.md

License

MIT

You might also like...
low level data type and utils in Golang.

low low level data type and utils in Golang. A stable low level function set is the basis of a robust architecture. It focuses on stability and requir

SliceX provides functional operations on Go slices using Go 1.18 type parameters.

SliceX provides functional operations on Go slices using Go 1.18 type parameters.

A slice backed binary heap with support for generic type parameters.

go-binaryheap A slice backed binary heap where the order can be customized by a comparison function. The main branch now requires go 1.18 because the

A drop-in replacement to any Writer type, which also calculates a hash using the provided hash type.

writehasher A drop-in replacement to any Writer type, which also calculates a hash using the provided hash type. Example package main import ( "fmt"

Sending emails using email server talking to RabbitMQ and send grid server sending emails to email ids consumed from RabbitMQ
Sending emails using email server talking to RabbitMQ and send grid server sending emails to email ids consumed from RabbitMQ

Sending emails using email server talking to RabbitMQ and send grid server sending emails to email ids consumed from RabbitMQ

Vso-hash-cuda - CUDA implementation of the BuildXL paged hash (a.k.a. VSO-Hash)

vso-hash-cuda Golang implementation of the BuildXL paged hash function (a.k.a. VSO-Hash) See https://github.com/microsoft/BuildXL/blob/master/Document

Prevent unauthorised access of public endpoints by for example bots or bad clients.
Prevent unauthorised access of public endpoints by for example bots or bad clients.

Anonymus API Auth Provider Inspired by: https://hackernoon.com/improve-the-security-of-api-keys-v5kp3wdu Architecture The basic idea is, to prevent un

Prevent unauthorised access of public endpoints by for example bots or bad clients.
Prevent unauthorised access of public endpoints by for example bots or bad clients.

Anonymous API Auth Provider Inspired by: https://hackernoon.com/improve-the-security-of-api-keys-v5kp3wdu Architecture The basic idea is, to prevent u

Vso-hash - Golang implementation of the BuildXL paged hash function

vso-hash Golang implementation of the BuildXL paged hash function See https://gi

Work pool channlege - An url hash retriever worker pool for getting hash digest for a collection of urls

Code challenge The aim of this project is to provide an url hash retriever worke

[TOOL, CLI] - Filter and examine Go type structures, interfaces and their transitive dependencies and relationships. Export structural types as TypeScript value object or bare type representations.

typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi

Go generator to copy values from type to type and fields from struct to struct. Copier without reflection.

Copygen is a command-line code generator that generates type-to-type and field-to-field struct code without adding any reflection or dependenc

convert JSON of a specific format to a type structure(Typescript type or more)

json2type convert JSON of a specific format to a type structure(Typescript type or more) Quick Start CLI Install use go tool install go install github

go-xss is a module used to filter input from users to prevent XSS attacks

go-xss 根据白名单过滤 HTML(防止 XSS 攻击) go-xss is a module used to filter input from users to prevent XSS attacks go-xss是一个用于对用户输入的内容进行过滤,以避免遭受 XSS 攻击的模块

Prevent Kubernetes misconfigurations from ever making it  (again 😤) to production! The CLI integration provides policy enforcement solution to run automatic checks for rule violations.  Docs: https://hub.datree.io
Prevent Kubernetes misconfigurations from ever making it (again 😤) to production! The CLI integration provides policy enforcement solution to run automatic checks for rule violations. Docs: https://hub.datree.io

What is Datree? Datree helps to prevent Kubernetes misconfigurations from ever making it to production. The CLI integration can be used locally or in

A server that proxies requests and uses fhttp & my fork of CycleTLS to modify your clienthello and prevent your requests from being fingerprinted.

TLS-Fingerprint-API A server that proxies requests and uses my fork of CycleTLS & fhttp (fork of net/http) to prevent your requests from being fingerp

Run proprietary modpack in built in Darwin/macOS sandbox-exec to prevent it from doing malicious things.
Run proprietary modpack in built in Darwin/macOS sandbox-exec to prevent it from doing malicious things.

sandbox-exec lunarclient Run LunarClient in built in Darwin/macOS sandbox-exec to prevent lunar from taking screenshots of your desktop. LunarClient l

An attempt to manage session and prevent ddos attack

This is an attempt to manage the session and prevent ddos attack. A session_id is an int64, inscrease by 1 when issue a new session. Server has a tabl

Releases(v1.1.4)
Owner
Emvi
Knowledge management platform for companies and teams — currently in Beta.
Emvi
Data Structures in Go: Hash Table

Data Structures in Go: Hash Table he time has come to implement one of the most commonly used data structures in software development - the Hash Table

Milad Samani 3 Oct 20, 2021
Collections for Golang using generics. Currently containing Hash Set.

Implementation of missing colections for Golang using Generics. Free of dependencies. Curently in early development phase. Requirements Go 1.18+ Insta

Vinícius Ferreira 0 Dec 30, 2021
Go translations of the algorithms and clients in the textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

Overview Go translations of the Java source code for the algorithms and clients in the textbook Algorithms, 4th Edition by Robert Sedgewick and Kevin

youngzy 172 Nov 24, 2022
A simple set type for the Go language. Trusted by Docker, 1Password, Ethereum and Hashicorp.

golang-set The missing set collection for the Go language. Until Go has sets built-in...use this. Coming from Python one of the things I miss is the s

Ralph Caraveo III 2.8k Nov 23, 2022
Null Types, Safe primitive type conversion and fetching value from complex structures.

Typ Typ is a library providing a powerful interface to impressive user experience with conversion and fetching data from built-in types in Golang Feat

Gurukami 33 Sep 26, 2022
Type-agnostic partitioning for Go's indexable collections and strings.

Go Type-Agnostic Collection Partitioning Type-agnostic partitioning for anything that can be indexed in Go - slices, arrays,strings. Inspired by Guava

Meir Fischer 35 Aug 11, 2021
flexible data type for Go

Generic flexible data type for Go support: Go 1.12+ Install standard go get: go get -u github.com/usk81/generic/v2 Usage encode/decode: package main

Yusuke Komatsu 45 Oct 26, 2022
Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types.

Package set is a small wrapper around the official reflect package that facilitates loose type conversion and assignment into native Go types. Read th

null 42 Nov 5, 2022
A faster method to get elements from an interface (Struct or Slice type) for Go.

A faster method to get elements from an interface (Struct or Slice type) for Go.

karminski-牙医 35 May 13, 2022
Type-safe, zero-allocation sets for Go

Set Package set is a type-safe, zero-allocation port of the excellent package fatih/set. It contains sets for most of the basic types and you can gene

ScyllaDB 793 Nov 27, 2022