{"id":1948,"url":"https://github.com/TakeScoop/SwiftyRSA","last_synced_at":"2025-08-02T05:33:22.589Z","repository":{"id":34520161,"uuid":"38462337","full_name":"TakeScoop/SwiftyRSA","owner":"TakeScoop","description":"RSA public/private key encryption in Swift","archived":false,"fork":false,"pushed_at":"2025-01-11T04:07:56.000Z","size":5139,"stargazers_count":1298,"open_issues_count":86,"forks_count":354,"subscribers_count":50,"default_branch":"master","last_synced_at":"2025-07-26T11:56:44.552Z","etag":null,"topics":["encryption","ios","mobile","rsa","security","swift","tvos","watchos"],"latest_commit_sha":null,"homepage":null,"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/TakeScoop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2015-07-02T23:49:45.000Z","updated_at":"2025-07-03T10:53:41.000Z","dependencies_parsed_at":"2024-04-23T19:06:00.968Z","dependency_job_id":"21e77213-0079-4da1-a470-0537c60e30e0","html_url":"https://github.com/TakeScoop/SwiftyRSA","commit_stats":{"total_commits":118,"total_committers":27,"mean_commits":4.37037037037037,"dds":"0.34745762711864403","last_synced_commit":"6521919ad0e99dff927685f142d8136479acb452"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"purl":"pkg:github/TakeScoop/SwiftyRSA","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TakeScoop%2FSwiftyRSA","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TakeScoop%2FSwiftyRSA/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TakeScoop%2FSwiftyRSA/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TakeScoop%2FSwiftyRSA/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TakeScoop","download_url":"https://codeload.github.com/TakeScoop/SwiftyRSA/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TakeScoop%2FSwiftyRSA/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268339405,"owners_count":24234544,"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","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["encryption","ios","mobile","rsa","security","swift","tvos","watchos"],"created_at":"2024-01-05T20:15:59.660Z","updated_at":"2025-08-02T05:33:21.669Z","avatar_url":"https://github.com/TakeScoop.png","language":"Swift","funding_links":[],"categories":["Security","Swift"],"sub_categories":["Encryption","Other free courses"],"readme":"SwiftyRSA\n=========\n\n**Maintainer(s):** [@ikeith](https://github.com/ikeith)\n\n[![](https://img.shields.io/cocoapods/v/SwiftyRSA.svg)](https://cocoapods.org/pods/SwiftyRSA)\n![](https://img.shields.io/badge/carthage-compatible-brightgreen.svg)\n![](https://img.shields.io/cocoapods/p/SwiftyRSA.svg)\n![](https://img.shields.io/badge/language-swift_3.2/4.2-brightgreen.svg)\n[![](https://circleci.com/gh/TakeScoop/SwiftyRSA.png?style=shield\u0026circle-token=03e95e0cd05dab2e159cf65d6e62de1d1d84148e)](https://circleci.com/gh/TakeScoop/SwiftyRSA)\n\n**Public key RSA encryption in Swift.**\n\nSwiftyRSA is used in the [Scoop](https://www.takescoop.com/) [iOS app](https://itunes.apple.com/us/app/scoop-easy-custom-carpooling/id997978145?mt=8) to encrypt driver license numbers before submitting them to Checkr through our API.\n\nInstallation\n------------\n\n### Swift 5.0+\n\nSwiftyRSA uses Swift 5.0 and requires Xcode 10.2+.\n\nWith Cocoapods:\n\n```\npod 'SwiftyRSA'\n```\n\nWith Carthage:\n\n```\ngithub \"TakeScoop/SwiftyRSA\"\n```\n\n### Objective-C\n\n```\npod 'SwiftyRSA/ObjC'\n```\n\n\nQuick Start\n-----------\n\n### Encrypt with a public key\n\n```swift\ndo {\n    let publicKey = try PublicKey(pemNamed: \"public\")\n\n    let str = \"Clear String\"\n    let clear = try ClearMessage(string: str, using: .utf8)\n    let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)\n\n    let data = encencrypted.data\n    print(data)\n    \n    let base64String = encrypted.base64String\n    print(base64String)\n\n} catch {\n    print(error)\n}\n```\n\n### Decrypt with a private key\n\n```swift\nlet privateKey = try PrivateKey(pemNamed: \"private\")\nlet encrypted = try EncryptedMessage(base64Encoded: \"AAA===\")\nlet clear = try encrypted.decrypted(with: privateKey, padding: .PKCS1)\n\n// Then you can use:\nlet data = clear.data\nlet base64String = clear.base64String\nlet string = clear.string(using: .utf8)\n```\n\n\nAdvanced Usage\n--------------\n\n### Create a public/private key representation\n\n#### With a DER file\n\n```swift\nlet publicKey = try PublicKey(derNamed: \"public\")\nlet privateKey = try PrivateKey(derNamed: \"private\")\n```\n\n#### With a PEM file\n\n```swift\nlet publicKey = try PublicKey(pemNamed: \"public\")\nlet privateKey = try PrivateKey(pemNamed: \"private\")\n```\n\n#### With a PEM string\n\n```swift\nlet publicKey = try PublicKey(pemEncoded: str)\nlet privateKey = try PrivateKey(pemEncoded: str)\n```\n\n#### With a Base64 string\n\n```swift\nlet publicKey = try PublicKey(base64Encoded: base64String)\nlet privateKey = try PrivateKey(base64Encoded: base64String)\n```\n\n#### With data\n\n```swift\nlet publicKey = try PublicKey(data: data)\nlet privateKey = try PrivateKey(data: data)\n```\n\n#### With a SecKey\n\n```swift\nlet publicKey = try PublicKey(reference: secKey)\nlet privateKey = try PrivateKey(reference: secKey)\n```\n\n### Encrypt with a public key\n\n```swift\nlet str = \"Clear Text\"\nlet clear = try ClearMessage(string: str, using: .utf8)\nlet encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)\n\nlet data = encrypted.data\nlet base64String = encrypted.base64Encoded\n```\n\n### Decrypt with a private key\n\n```swift\nlet encrypted = try EncryptedMessage(base64Encoded: base64String)\nlet clear = try encrypted.decrypted(with: privateKey, padding: .PKCS1)\n\nlet data = clear.data\nlet base64String = clear.base64Encoded\nlet string = try clear.string(using: .utf8)\n```\n\n### Sign with a private key\n\nSwiftyRSA can sign data with a private key. SwiftyRSA will calculate a SHA digest of the supplied `String`/`Data` and use this to generate the digital signature.\n\n```swift\nlet clear = try ClearMessage(string: \"Clear Text\", using: .utf8)\nlet signature = clear.signed(with: privateKey, digestType: .sha1)\n\nlet data = signature.data\nlet base64String = signature.base64String\n```\n\n### Verify with a public key\n\nSwiftyRSA can verify digital signatures with a public key. SwiftyRSA will calculate a digest of the supplied `String`/`Data` and use this to verify the digital signature.\n\n```swift\nlet signature = try Signature(base64Encoded: \"AAA===\")\nlet isSuccessful = try clear.verify(with: publicKey, signature: signature, digestType: .sha1)\n```\n\n### Create a public/private RSA key pair\n\n```swift\nlet keyPair = SwiftyRSA.generateRSAKeyPair(sizeInBits: 2048)\nlet privateKey = keyPair.privateKey\nlet publicKey = keyPair.publicKey\n```\n\n### Export a key or access its content\n\n```swift\nlet pem = try key.pemString()\nlet base64 = try key.base64String()\nlet data = try key.data()\nlet reference = key.reference\nlet originalData = key.originalData\n```\n\n### Use X.509 certificate \nSwiftyRSA supports X.509 certificate for public keys. SwiftyRSA can add the X.509 header to a headerless public key, or on the contrary  strip it to get a key without a header.\n#### Add an X.509 header to a public key \n```swift\nlet publicKey = PublicKey(data: data)\nlet publicKeyData = try publicKey.data()\nlet publicKey_with_X509_header = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData)\n```\n#### Strip the X.509 header from a public key \n```swift\nlet publicKey_headerLess: Data = try SwiftyRSA.stripKeyHeader(keyData: publicKey_with_X509_header)\n```\n\n**Warning** : Storing  (with SwiftyRSA's methods) or creating a ```PublicKey``` instance will automatically strip the header from the key. For more info, see *Under the hood* above.\n\nCreate public and private RSA keys\n----------------------------------\n\nUse `ssh-keygen` to generate a PEM public key and a PEM private key. SwiftyRSA also supports DER public keys.\n\n```\n$ ssh-keygen -t rsa -m PEM -f ~/mykey -N ''\n$ cat ~/mykey \u003e ~/private.pem\n$ ssh-keygen -f ~/mykey.pub -e -m pem \u003e ~/public.pem\n```\n\nYour keys are now in `~/public.pem` and `~/private.pem`. Don't forget to move `~/mykey` and `~/mykey.pub` to a secure place.\n\nUnder the hood\n--------------\n\nTo enable using public/private RSA keys on iOS, SwiftyRSA uses a couple techniques like X.509 header stripping so that the keychain accepts them.\n\n\u003cdetails\u003e\n\t\u003csummary\u003eClick here for more details\u003c/summary\u003e\n\nWhen encrypting using a public key:\n\n - If the key is in PEM format, get rid of its meta data and convert it to Data\n - Strip the public key X.509 header, otherwise the keychain won't accept it\n - Add the public key to the keychain, with a random tag\n - Get a reference on the key using the key tag\n - Use `SecKeyEncrypt` to encrypt a `ClearMessage` using the key reference and the message data.\n - Store the resulting encrypted data to an `EncryptedMessage`\n - When the key gets deallocated, delete the public key from the keychain using its tag\n\nWhen decrypting using a private key:\n\n - Get rid of PEM meta data and convert to Data\n - Add the private key to the app keychain, with a random tag\n - Get a reference on the key using the key tag\n - Use `SecKeyDecrypt` to decrypt an `EncryptedMessage` using the key reference and the encrypted message data\n - Store the resulting decrypted data to a `ClearMessage`\n - Delete private key from keychain using tag\n\u003c/details\u003e\n\nInspired from\n-------------\n\n - \u003chttp://blog.flirble.org/2011/01/05/rsa-public-key-openssl-ios/\u003e\n - \u003chttps://github.com/lancy/RSADemo\u003e\n - \u003chttps://github.com/btnguyen2k/swift-rsautils\u003e\n\nLicense\n-------\n\nThis project is copyrighted under the MIT license. Complete license can be found here: \u003chttps://github.com/TakeScoop/SwiftyRSA/blob/master/LICENSE\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTakeScoop%2FSwiftyRSA","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FTakeScoop%2FSwiftyRSA","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FTakeScoop%2FSwiftyRSA/lists"}