{"id":20886482,"url":"https://github.com/gsurma/diffie_hellman_key_exchange","last_synced_at":"2025-05-12T19:32:20.966Z","repository":{"id":90787531,"uuid":"128516016","full_name":"gsurma/diffie_hellman_key_exchange","owner":"gsurma","description":"Swift implementation of classic cryptographic key exchange method.","archived":false,"fork":false,"pushed_at":"2021-07-09T08:57:35.000Z","size":191,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-01T10:14:41.389Z","etag":null,"topics":["cryptography","diffie-hellman","ios","macos","prime-numbers","swift","symmetric-key-cryptography"],"latest_commit_sha":null,"homepage":"https://gsurma.github.io","language":"Swift","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/gsurma.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","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},"funding":{"patreon":"gsurma"}},"created_at":"2018-04-07T09:58:07.000Z","updated_at":"2022-05-07T14:49:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"db1b51a3-2ce2-4a47-8cff-eb832bfd778b","html_url":"https://github.com/gsurma/diffie_hellman_key_exchange","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsurma%2Fdiffie_hellman_key_exchange","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsurma%2Fdiffie_hellman_key_exchange/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsurma%2Fdiffie_hellman_key_exchange/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gsurma%2Fdiffie_hellman_key_exchange/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gsurma","download_url":"https://codeload.github.com/gsurma/diffie_hellman_key_exchange/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253808321,"owners_count":21967527,"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":["cryptography","diffie-hellman","ios","macos","prime-numbers","swift","symmetric-key-cryptography"],"created_at":"2024-11-18T08:17:07.063Z","updated_at":"2025-05-12T19:32:20.955Z","avatar_url":"https://github.com/gsurma.png","language":"Swift","funding_links":["https://patreon.com/gsurma"],"categories":[],"sub_categories":[],"readme":"\u003ch3 align=\"center\"\u003e\n  \u003cimg src=\"assets/diffie_hellman_icon_web.png\" width=\"300\"\u003e\n\u003c/h3\u003e\n\n# Diffie-Hellman Key Exchange\n\nSwift implementation of classic cryptographic key exchange method.\n\n## About\nDiffie-Hellman Key Exchange allow parties to jointly establish a secure private key without sharing it in any way ([Forward secrecy](https://en.wikipedia.org/wiki/Forward_secrecy)) and then use it for a symmetric key cipher. \n\n## How does it work?\n\n1. Both parties agree on a common component, which consists of two natural numbers p (modulus) and g (base). They can be completely random to make this work, but in order to make the process significantly harder to break, **p should be a prime and g should be primitive root modulo of p**. Check `DHParameters.swift` for more info.\n2. Then both parties generate random private keys and then compute public keys which they share with each other. Public keys are computed as follows **publicKey = g^privateKey mod p**\n3. Afterward, both parties can compute common secret key using own private key and peer's public key. They can do it using the following formula **secretKey = peerPublicKey^ownPrivateKey mod p** \u003cbr\u003e\u003cbr\u003e Underlying math: \u003cbr\u003e **(g^a mod p)^b mod p = g^ab mod p** \u003cbr\u003e **(g^b mod p)^a mod p = g^ba mod p**\n\n4. Now both parties can communicate using symmetric cryptography using a jointly established private key.\n\n\u003cbr\u003e\n\n\u003ch3 align=\"center\"\u003e\n  \u003cimg src=\"assets/dh_illustration.png\" width=\"400\"\u003e\n\u003c/h3\u003e\n\n## What's so special about it?\n\nThis protocol is considered secure (check disclaimer), because it's relatively hard for eavesdroppers to compute a common secret key knowing only public keys if p is big enough.\n\n\n### Disclaimer\nDon't use it in a production environment. Generated keys are very small (Int64) thus making them easily breakable.\nUse already generated [RFC primes](https://www.ietf.org/rfc/rfc3526.txt), but even them [may not be strong enough](https://arstechnica.com/information-technology/2015/10/how-the-nsa-can-break-trillions-of-encrypted-web-and-vpn-connections/).\n\n## Author\n\n**Greg (Grzegorz) Surma**\n\n[**PORTFOLIO**](https://gsurma.github.io)\n\n[**GITHUB**](https://github.com/gsurma)\n\n[**BLOG**](https://medium.com/@gsurma)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsurma%2Fdiffie_hellman_key_exchange","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgsurma%2Fdiffie_hellman_key_exchange","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgsurma%2Fdiffie_hellman_key_exchange/lists"}