{"id":16387236,"url":"https://github.com/alexeichhorn/jsonworkproof","last_synced_at":"2026-04-17T14:38:00.876Z","repository":{"id":93773757,"uuid":"351604323","full_name":"alexeichhorn/JSONWorkProof","owner":"alexeichhorn","description":"JSON Work Proof - proof-of-work algorithm encoded like a JWT","archived":false,"fork":false,"pushed_at":"2022-09-08T16:29:41.000Z","size":49,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-03-28T21:30:18.101Z","etag":null,"topics":["claims","ddos-prevention","hashcash","json","jwt","proof-of-work","swift","swiftpm"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexeichhorn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-03-25T23:30:31.000Z","updated_at":"2025-01-17T17:03:47.000Z","dependencies_parsed_at":"2023-03-30T10:36:11.930Z","dependency_job_id":null,"html_url":"https://github.com/alexeichhorn/JSONWorkProof","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/alexeichhorn/JSONWorkProof","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeichhorn%2FJSONWorkProof","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeichhorn%2FJSONWorkProof/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeichhorn%2FJSONWorkProof/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeichhorn%2FJSONWorkProof/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexeichhorn","download_url":"https://codeload.github.com/alexeichhorn/JSONWorkProof/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexeichhorn%2FJSONWorkProof/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31933724,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-17T12:37:54.787Z","status":"ssl_error","status_checked_at":"2026-04-17T12:37:25.095Z","response_time":62,"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":["claims","ddos-prevention","hashcash","json","jwt","proof-of-work","swift","swiftpm"],"created_at":"2024-10-11T04:25:50.005Z","updated_at":"2026-04-17T14:38:00.859Z","avatar_url":"https://github.com/alexeichhorn.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSON Work Proof\n\nJSON Work Proof is a proof-of-work algorithm that creates a token after doing some workload. This token contains certain claims defined by you and verifies that you did this work at this time and for these claims.\n\nIt packs the security of the Hashcash algorithm *(used for Bitcoin in a similar way)* into a modern JWT-like token.\n\n## Structure of Token\n\nA token looks like this: `eyJ0eXAiOiJKV1AiLCJhbGciOiJTSEEyNTYiLCJkaWYiOjIwfQ.eyJleHAiOjE2MTY4NTA1NzAuNjU1MTQ3MSwiaGVsbG8iOiJ3b3JsZCJ9.VE6YYxIQ46lPzxyNuRYAmAMkEM`. It has the same structure as a JWT token and can therefore also be inspected on the Debugger on [jwt.io](https://jwt.io).\nIt contains three elements which are each base64url encoded. The header contains the type of the token (JWP), the hash algorithm used for the challenge (currently only SHA256 supported) and the difficulty at which the token was mined. The payload consists of the claims you specified and optionally an expiration date. The last part contains a salt and a big number (named counter in Hashcash). The work needed to generate a token is actually to find this number. It's hard to find this number, but easy to verify it's correct. (Read more about how it works on [Wikipedia](https://en.wikipedia.org/wiki/Hashcash))\n\n\n## Possible Applications\n\nCan be used to prevent DDOS attacks or as an alternative to rate limiting or captchas. \n\nE.g. you can use this to prevent brute forcing user logins: The client generates a token with the claims including username and password and sends it along with the login request. The server then first checks if the token is valid before it does any lookup. The scale of bruteforcing can therefore be massively reduced.\n\n\n\n## Usage\n\n### General\n\nTo generate and validate tokens you need to use a `JWP`-object. On creation you can specify the `difficulty`, which determines how hard the challenge should be. It defaults to `20`, which takes about a second to compute on an average computer. Each increment by one, doubles the difficulty and therefore the time it takes to generate.\n```swift\nimport JSONWorkProof\n\nlet jwp = JWP()   // defaults to difficulty 20\nlet jwpHarder = JWP(difficulty: 25)\n```\n\n### Generation\n\nTo generate a token, that proves you did work, create a `JWP`-object and call it with your dictionary of claims like this:\n```swift\nlet jwp = JWP()\nlet token = try jwp.generate(claims: [ \"hello\": \"world\", \"count\": 88 ])\n```\n\n**Note:** A token expires 5 minutes after creation on default. You can change this by giving a custom expiration date:\n```swift\nlet expiration = Date() + 3600  // 1 hour from now\nlet token = try jwp.generate(claims: claims, expiration: expiration)\n\nlet token2 = try jwp.generate(claims: claims, expiration: nil)  // no expiration\n```\n\n\n\n### Validation\n\nTo check if a token is valid for a certain difficulty and to read the claims:\n```swift\nlet jwp = JWP()\ndo {\n  let claims = try jwp.decode(token)\n} catch let error {\n  switch error as? JWP.DecodeError {\n  case .invalidFormat:\n    print(\"The token is formatted incorrectly\")\n  case .invalidProof:\n    print(\"The difficulty this token was created at is lower than what is specified in your JWP object\")\n  case .expired:\n    print(\"The token expiration is too old or too new\")\n  default: break\n  }\n}\n```\n\n\nIf you just want to read the claims without verifying the proof and expiration date, you can use this instead:\n```swift\nlet claims = try jwp.decode(token, verify: false)\n```\n\nBy default it expects the expiration date to be between now and 30 minutes in the future. You can also specify your own range of valid expiration dates like this:\n```swift\nlet claims1 = try jwp.decode(token, expirationRange: JWP.DateRange(start: startDate, end: endDate))  // must be in [startDate, endDate]\nlet claims2 = try jwp.decode(token, expirationRange: JWP.DateRange(fromNow: 3600))  // must be in [now(), now()+3600]\nlet claims3 = try jwp.decode(token, expirationRange: .unlimited))  // all expirations are accepted\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexeichhorn%2Fjsonworkproof","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexeichhorn%2Fjsonworkproof","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexeichhorn%2Fjsonworkproof/lists"}