{"id":17684598,"url":"https://github.com/mattrltrent/jsonencryption","last_synced_at":"2026-04-29T13:35:42.608Z","repository":{"id":218173407,"uuid":"745782519","full_name":"mattrltrent/jsonencryption","owner":"mattrltrent","description":"🔐 Golang utility package for model class ID-field serialization encryption.","archived":false,"fork":false,"pushed_at":"2024-01-20T19:59:01.000Z","size":8,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-29T07:37:51.995Z","etag":null,"topics":["json","json-api","json-ld","json-parser","package"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/mattrltrent/jsonencryption","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mattrltrent.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-20T05:52:36.000Z","updated_at":"2024-07-09T21:10:40.000Z","dependencies_parsed_at":"2024-01-20T08:40:32.858Z","dependency_job_id":"367d1571-ad5f-43da-8e69-1a2d7554e2ab","html_url":"https://github.com/mattrltrent/jsonencryption","commit_stats":null,"previous_names":["mattrltrent/jsonencryption"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/mattrltrent/jsonencryption","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fjsonencryption","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fjsonencryption/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fjsonencryption/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fjsonencryption/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattrltrent","download_url":"https://codeload.github.com/mattrltrent/jsonencryption/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattrltrent%2Fjsonencryption/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32427838,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-29T13:34:34.882Z","status":"ssl_error","status_checked_at":"2026-04-29T13:34:29.830Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","json-api","json-ld","json-parser","package"],"created_at":"2024-10-24T10:24:07.381Z","updated_at":"2026-04-29T13:35:42.583Z","avatar_url":"https://github.com/mattrltrent.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Golang package: `jsonencryption`\n\n![unit tests](https://github.com/mattrltrent/jsonencryption/actions/workflows/unit_tests.yml/badge.svg)\n\n[import \"github.com/mattrltrent/jsonencryption/jsonencryption\"](https://github.com/mattrltrent/jsonencryption)\n\n### What does it do?\n\nIt takes a struct like this:\n\n```go\nuser := User{ID: 123, Name: \"John\", Age: 20}\n```\n\nThat normally serializes as:\n\n```json\n{\n  \"ID\": 123 // \u003c-- bad! this reveals your sensitive ID\n  \"Name\": \"John\",\n  \"Age\": 20\n}\n```\n\nAnd allows you to instead populate the struct like this:\n\n```go\nuser := User{ID: jsonencryption.NewEncryptedID(123), Name: \"John\", Age: 20}\n```\n\nSuch that it now serializes like this with encrypted values:\n\n```json\n{\n  \"ID\": {\n    \"hash\": \"pmWkWSBCL51Bfkhn79xPuKBKHz__H6B-mY6G9_eieuM\",\n    \"mask\": \"GpvBdoyRDZR3vU1BC0ptrkvEhQ==\"\n  },\n  \"Name\": \"John\",\n  \"Age\": 20\n}\n```\n\n### Serialized fields: `hash` \u0026 `mask`\n\n**`\"hash\"`**: An irreversible string. Two of the same `uint` values will always have the same hash. Useful to ensure uniqueness on the client-side between different recieved JSON documents. \n\n**`\"mask\"`**: A string *only* reversible by your code since you set the 16-byte secret string that was used to mask it. Two of the same `uint` values will have different masks. Useful to have an opaque mask of the true `uint` that only you can read.\n\n### Working with: `EncryptedID` \u0026 `uint`\n\n```go\n// create a new EncryptedID (providing some uint argument)\nencryptedID := jsonencryption.NewEncryptedID(123) // type: jsonencryption.EncryptedID\n\n// get the uint back\nvalue := encryptedID.Val // type: uint\n```\n\n### Re: Databases\n\nWhen you're using a struct as a model class with an `EncryptedID`, it should automatically send to the database just the inner `uint`.\n\n### Model class usecase example\n\nYou have a model class that you put `EncryptedID` into as the primary key:\n\n```go\ntype BlogArticle struct {\n\tID         EncryptedID `json:\"id\"`\n\tPostedDate time.Time   `json:\"posted_date\"`\n\tTitle      string      `json:\"title\"`\n\tBody       string      `json:\"body\"`\n\tAuthor     string      `json:\"author\"`\n\tSlug       string      `json:\"slug\"`\n}\n```\n\nYou can then both use this to send to the client (as JSON) and interact with the database. It will conceal the inner `uint` value of the `EncryptedID` from the client (with a \"mask\" and \"hash\"), but use the real `uint` with the database.\n\n### Simple example usage\n\n**TLDR:** Set your PRIVATE AND SENSITIVE 16-byte encryption key (`jsonencryption.SetKey(\"16-byte-key-1234\")`) and then use `jsonencryption.EncryptedID` however you please.\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"log\"\n\n\t\"github.com/mattrltrent/jsonencryption/jsonencryption\"\n)\n\nfunc init() {\n\t// you must set a key (probably should be loaded via env var)\n\tjsonencryption.SetKey(\"16-byte-key-1234\")\n}\n\ntype User struct {\n\tID   jsonencryption.EncryptedID\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\t// create a new user with an encrypted ID\n\tuser := User{ID: jsonencryption.NewEncryptedID(123), Name: \"John\", Age: 20}\n\n\t// simulate turning it to JSON\n\tjsonData, err := json.Marshal(user)\n\tif err != nil {\n\t\tlog.Fatalf(\"Error occurred during marshaling. Error: %s\", err.Error())\n\t}\n\n\t// look... it's encrypted!\n\tfmt.Println(string(jsonData))\n}\n```\n\n### Other methods\n\nCheck out the [encryption_test.go](https://github.com/mattrltrent/jsonencryption/blob/main/jsonencryption/encryption_test.go) file to see what other methods can be utilized.\n\n### Unit tests\n\nThis will run all this package's unit tests:\n\n```sh\n./scripts/test ./...\n```\n\n### Disclaimer\n\nMade for fun as a non-security professional. Use at your own risk.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattrltrent%2Fjsonencryption","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattrltrent%2Fjsonencryption","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattrltrent%2Fjsonencryption/lists"}