https://github.com/indrasaputra/hashids
Golang ID to Hash converter using Hashids algorithm.
https://github.com/indrasaputra/hashids
hacktoberfest hacktoberfest-accepted hashid hashids
Last synced: about 1 year ago
JSON representation
Golang ID to Hash converter using Hashids algorithm.
- Host: GitHub
- URL: https://github.com/indrasaputra/hashids
- Owner: indrasaputra
- License: mit
- Created: 2020-10-10T09:45:37.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-05-25T22:44:44.000Z (about 5 years ago)
- Last Synced: 2025-03-29T13:11:12.506Z (over 1 year ago)
- Topics: hacktoberfest, hacktoberfest-accepted, hashid, hashids
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hashids
[](https://goreportcard.com/report/github.com/indrasaputra/hashids)
[](https://github.com/indrasaputra/hashids/actions)
[](https://codecov.io/gh/indrasaputra/hashids)
[](https://codeclimate.com/github/indrasaputra/hashids/maintainability)
[](https://sonarcloud.io/dashboard?id=indrasaputra_hashids)
[](https://pkg.go.dev/github.com/indrasaputra/hashids)
Hashids is a package to convert ID into a random string to obfuscate the real ID from user.
In the implementation itself, the ID will still be an integer. But, when it is shown to the user,
it becomes a random string. The generated random string can be decoded back to the original ID.
This project uses [https://github.com/speps/go-hashids](https://github.com/speps/go-hashids) as the backend.
## Installation
```
go get github.com/indrasaputra/hashids
```
## Example
Let there be a struct:
```go
type Product struct {
ID hashids.ID `json:"id"`
Name string `json:"name"`
}
```
Then we have an instance of Product like this:
```go
product := &Product{
ID: hashids.ID(66),
Name: "Product's name",
}
```
When the `product` is marshalled into a JSON, the ID will not be a plain integer. It will become a random string like this:
```json
{
"id": "kmzwa8awaa",
"name": "product's name"
}
```
Upon decoding the ID, Hashids will decode back the random string to the original ID.
```go
var product Product
b := []byte(`{"id": "kmzwa8awaa","name": "product's name"}`)
json.Unmarshal(b, &product)
```
The code above will fill the product's attributes like this:
```cmd
{66 product's name}
```
## Limitation
For now, this package only support encoding to JSON, decoding from JSON, and encode as a string.