{"id":20631950,"url":"https://github.com/meehow/securebytes","last_synced_at":"2025-04-15T18:54:20.351Z","repository":{"id":57493247,"uuid":"168326311","full_name":"meehow/securebytes","owner":"meehow","description":"⚡️ Take any Go data type, serialize it to JSON or GOB and encrypt it with AES. Works 2.5x faster than securecookie and generates 40% smaller cookies.","archived":false,"fork":false,"pushed_at":"2020-04-18T19:54:41.000Z","size":24,"stargazers_count":14,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-28T23:51:14.583Z","etag":null,"topics":["aes","authenticated-encryption","cookie","encryption","securecookie","serialization"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/meehow.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}},"created_at":"2019-01-30T10:44:18.000Z","updated_at":"2025-03-20T03:39:41.000Z","dependencies_parsed_at":"2022-08-28T17:02:43.112Z","dependency_job_id":null,"html_url":"https://github.com/meehow/securebytes","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meehow%2Fsecurebytes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meehow%2Fsecurebytes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meehow%2Fsecurebytes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/meehow%2Fsecurebytes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/meehow","download_url":"https://codeload.github.com/meehow/securebytes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249135763,"owners_count":21218365,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["aes","authenticated-encryption","cookie","encryption","securecookie","serialization"],"created_at":"2024-11-16T14:14:27.357Z","updated_at":"2025-04-15T18:54:20.307Z","avatar_url":"https://github.com/meehow.png","language":"Go","readme":"# Secure Bytes [![GoDoc](https://godoc.org/github.com/github.com/meehow/securebytes?status.svg)](http://godoc.org/github.com/meehow/securebytes) [![Go Report Card](https://goreportcard.com/badge/github.com/meehow/securebytes)](https://goreportcard.com/report/github.com/meehow/securebytes#SecureBytes)\n\nSecure Bytes takes any Go data type, serializes it to DER-ASN.1, JSON or GOB and encrypts it with AES-192-GCM.\nThe goal of this library is to generate smaller cookies than\n[securecookie](https://github.com/gorilla/securecookie) does.\nIt's achieved by using [Authenticated Encryption](https://en.wikipedia.org/wiki/Authenticated_encryption)\ninstead of HMAC and optionally DER-ASN.1 instead of GOB or JSON.\n\n## Installation\n\n```\ngo get -u github.com/meehow/securebytes\n```\n\n## Usage\n\nFirst, create Secure Bytes instance and set encryption key.\nSuggested key length is at least 50 characters.\nYou can choose one of build-in serializers: GOBSerializer, JSONSerializer\nor ASN1Serializer. Usually ASN1Serializer gives the smallest output, but it's\nnot compatible with some data types (for example uint).\n\n```go\nvar sb = securebytes.New(\n\t[]byte(\"choo}ng-o9quoh6oodurabishoh9haeWee~neeyaRoqu6Chue1\"),\n\tsecurebytes.ASN1Serializer{})\n```\n\nWrite a cookie:\n\n```go\ntype Session struct {\n\tUserID int\n\tName   string\n}\n\nfunc SetCookieHandler(w http.ResponseWriter, r *http.Request) {\n\tsession := Session{\n\t\tUserID: 1234567890,\n\t\tName:   \"meehow\",\n\t}\n\tb64, err := sb.EncodeToBase64(session)\n\tif err != nil {\n\t\tfmt.Fprintf(w, \"Encryption error: %v\", err)\n\t\treturn\n\t}\n\tcookie := \u0026http.Cookie{\n\t\tName:  \"cookie-name\",\n\t\tValue: b64,\n\t\tPath:  \"/\",\n\t\tHttpOnly: true,\n\t}\n\thttp.SetCookie(w, cookie)\n}\n```\n\nRead a cookie:\n\n```go\nfunc ReadCookieHandler(w http.ResponseWriter, r *http.Request) {\n\tvar session Session\n\tcookie, err := r.Cookie(\"cookie-name\")\n\tif err != nil {\n\t\tfmt.Fprintf(w, \"Cookie not found: %v\", err)\n\t\treturn\n\t}\n\tif err = sb.DecryptBase64(cookie.Value, \u0026session); err != nil {\n\t\tfmt.Fprintf(w, \"Decryption error: %v\", err)\n\t\treturn\n\t}\n\tfmt.Fprintf(w, \"Your session cookie: %#v\", session)\n}\n```\n\nYou can also check [example](examples/cookie.go) to see full code of http server.\n\nIf you need plain `[]byte` output, you can use `Encrypt` and `Decrypt` functions instead.\n\nYou can find more information in the [documentation](https://godoc.org/github.com/meehow/securebytes#SecureBytes).\n\n## Benchmark\n\nSecureBytes works **2.5 times faster** than SecureCookie and generates **40% smaller** cookies.\n\n```\ngoos: linux\ngoarch: amd64\npkg: github.com/meehow/securebytes\nBenchmarkSecureBytesJSON-4    \t  300000\t      3810 ns/op\nBenchmarkSecureBytesGOB-4     \t  300000\t      5476 ns/op\nBenchmarkSecureCookieJSON-4   \t  200000\t      9959 ns/op\nBenchmarkSecureCookieGOB-4    \t  100000\t     11807 ns/op\nPASS\nok  \tgithub.com/meehow/securebytes\t6.303s\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeehow%2Fsecurebytes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmeehow%2Fsecurebytes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmeehow%2Fsecurebytes/lists"}