{"id":1946,"url":"https://github.com/soyersoyer/SwCrypt","last_synced_at":"2025-08-02T05:33:09.224Z","repository":{"id":55488197,"uuid":"56019354","full_name":"soyersoyer/SwCrypt","owner":"soyersoyer","description":"RSA public/private key generation, RSA, AES encryption/decryption, RSA sign/verify in Swift with CommonCrypto in iOS and OS X","archived":false,"fork":false,"pushed_at":"2024-07-11T15:22:27.000Z","size":169,"stargazers_count":723,"open_issues_count":26,"forks_count":123,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-12-05T22:05:47.307Z","etag":null,"topics":["aes","aes-gcm","apple","commoncrypto","crypt","decrypt","ecc","encrypt","gcm","ios","mac","openssl","osx","pem","rsa","swift"],"latest_commit_sha":null,"homepage":"","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/soyersoyer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-12T01:03:32.000Z","updated_at":"2024-12-04T12:32:26.000Z","dependencies_parsed_at":"2023-02-12T06:15:34.158Z","dependency_job_id":null,"html_url":"https://github.com/soyersoyer/SwCrypt","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyersoyer%2FSwCrypt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyersoyer%2FSwCrypt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyersoyer%2FSwCrypt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soyersoyer%2FSwCrypt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soyersoyer","download_url":"https://codeload.github.com/soyersoyer/SwCrypt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228443737,"owners_count":17920776,"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","aes-gcm","apple","commoncrypto","crypt","decrypt","ecc","encrypt","gcm","ios","mac","openssl","osx","pem","rsa","swift"],"created_at":"2024-01-05T20:15:59.618Z","updated_at":"2024-12-06T09:30:39.562Z","avatar_url":"https://github.com/soyersoyer.png","language":"Swift","readme":"[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nSwCrypt\n=========\n\n### Create public and private RSA keys in DER format\n```\nlet (privateKey, publicKey) = try! CC.RSA.generateKeyPair(2048)\n```\n### Convert them to PEM format\n```\nlet privateKeyPEM = try SwKeyConvert.PrivateKey.derToPKCS1PEM(privateKey)\nlet publicKeyPEM = SwKeyConvert.PublicKey.derToPKCS8PEM(publicKey)\n```\n### Or read them from strings with PEM data\n```\nlet privateKeyDER = SwKeyConvert.PrivateKey.pemToPKCS1DER(privateKeyPEM)\nlet publicKeyDER = SwKeyConvert.PublicKey.pemToPKCS1DER(publicKeyPEM)\n```\n### Or encrypt, decrypt the private key (OpenSSL compatible)\n```\ntry SwKeyConvert.PrivateKey.encryptPEM(privateKeyPEM, passphrase: \"longpassword\", mode: .aes256CBC)\ntry SwKeyConvert.PrivateKey.decryptPEM(privEncrypted, passphrase: \"longpassword\")\n```\n### Get public key from private keys in DER format\n```\nlet publicKeyDER = try? CC.RSA.getPublicKeyFromPrivateKey(privateKeyDER!)\n```\n### Encrypt, decrypt data with RSA\n```\ntry CC.RSA.encrypt(data, derKey: publicKey, tag: tag, padding: .oaep, digest: .sha1)\ntry CC.RSA.decrypt(data, derKey: privateKey, tag: tag, padding: .oaep, digest: .sha1)\n```\n### Sign, verify data with RSA\n```\nlet sign = try? CC.RSA.sign(testMessage, derKey: privKey, padding: .pss, \n digest: .sha256, saltLen: 16)\nlet verified = try? CC.RSA.verify(testMessage, derKey: pubKey, padding: .pss,\n digest: .sha256, saltLen: 16, signedData: sign!)\n```\n### Elliptic curve functions\n```\nlet keys = try? CC.EC.generateKeyPair(384)\nlet signed = try? CC.EC.signHash(keys!.0, hash: hash)\nlet verified = try? CC.EC.verifyHash(keys!.1, hash: hash, signedData: signed!)\n\nlet shared = try? CC.EC.computeSharedSecret(keys!.0, publicKey: partnerPubKey)\n\nlet privComponents = try? CC.EC.getPrivateKeyComponents(keys!.0)\nlet pubComponents = try? CC.EC.getPublicKeyComponents(keys!.1)\n\nlet pubKey = try? CC.EC.createFromData(keySize, x, y)\nlet pubKey = try? CC.EC.getPublicKeyFromPrivateKey(keys!.0)\n\n```\n### Diffie-Hellman functions\n```\nlet dh = try CC.DH.DH(dhParam: .rfc3526Group5)\nlet myPubKey = try dh.generateKey()\nlet commonKey = try dh.computeKey(partnerPubKey!)\n```\n### Encrypt, decrypt data with symmetric ciphers\n```\ntry CC.crypt(.encrypt, blockMode: .cbc, algorithm: .aes, padding: .pkcs7Padding, data: data, key: aesKey, iv: iv)\ntry CC.crypt(.decrypt, blockMode: .cfb, algorithm: .aes, padding: .pkcs7Padding, data: data, key: aesKey, iv: iv)\n```\n### Encrypt, decrypt data with symmetric authenticating ciphers\n```\ntry CC.cryptAuth(.encrypt, blockMode: .gcm, algorithm: .aes, data: data, aData: aData, key: aesKey, iv: iv, tagLength: tagLength)\ntry CC.cryptAuth(.decrypt, blockMode: .ccm, algorithm: .aes, data: data, aData: aData, key: aesKey, iv: iv, tagLength: tagLength)\n```\n### Digest functions\n```\nCC.digest(data, alg: .md5)\nCC.digest(data, alg: .sha256)\nCC.digest(data, alg: .sha512)\n```\n### HMAC function\n```\nCC.HMAC(data, alg: .sha512, key: key)\n```\n### CMAC function\n```\nCC.CMAC.AESCMAC(input, key: key)\n```\n### CRC function\n```\nlet output = try? CC.CRC.crc(input, mode: .crc32)\n```\n### KeyDerivation\n```\nCC.KeyDerivation.PBKDF2(password, salt: salt, prf: .sha256, rounds: 4096)\n```\n### Symmetric Key Wrapping\n```\ntry CC.KeyWrap.SymmetricKeyWrap(CC.KeyWrap.rfc3394IV, kek: kek, rawKey: rawKey)\ntry CC.KeyWrap.SymmetricKeyUnwrap(CC.KeyWrap.rfc3394IV, kek: kek, wrappedKey: wrappedKey)\n```\n### Upsert, get, delete keys from KeyStore\n```\ntry SwKeyStore.upsertKey(privateKeyPEM, keyTag: \"priv\", options: [kSecAttrAccessible:kSecAttrAccessibleWhenUnlockedThisDeviceOnly])\ntry SwKeyStore.getKey(\"priv\")\ntry SwKeyStore.delKey(\"priv\")\n```\n-----\n\n\nCheck availability\n---------------------\n\nSwCrypt uses dlopen and dlsym to load the CommonCrypto's functions, because not all of them are available in public header files. You have to check the availability before using them.\n\n```\nlet digestAvailable : Bool = CC.digestAvailable()\nlet ramdomAvailable : Bool = CC.randomAvailable(()\nlet hmacAvailable : Bool = CC.hmacAvailable()\nlet cryptorAvailable : Bool = CC.cryptorAvailable\nlet keyDerivationAvailable : Bool = CC.KeyDerivation.available()\nlet keyWrapAvailable : Bool = CC.KeyWrap.available()\nlet rsaAvailable : Bool = CC.RSA.available()\nlet dhAvailable : Bool = CC.DH.available()\nlet ecAvailable : Bool = CC.EC.available()\nlet crcAvailable : Bool = CC.CRC.available()\nlet cmacAvailable : Bool = CC.CMAC.available()\nlet gcmAvailable : Bool = CC.GCM.available()\nlet ccmAvailable : Bool = CC.CCM.available()\n\nor all in one turn:\nlet ccAvailable : Bool = CC.available()\n```\n\nInstall\n-------\nJust copy [SwCrypt.swift](https://github.com/soyersoyer/SwCrypt/blob/master/SwCrypt/SwCrypt.swift) to your project or use the [Carthage](https://github.com/Carthage/Carthage) dependency manager.\n\n### CocoaPods\n[CocoaPods][] is a dependency manager for Cocoa projects. To install SwCrypt with CocoaPods:\n\n 1. Make sure CocoaPods is [installed][CocoaPods Installation].\n\n 2. Update your Podfile to include the following:\n\n    ``` ruby\n    pod 'SwCrypt'\n    ```\n\n 3. Run `pod install`.\n\n[CocoaPods]: https://cocoapods.org\n[CocoaPods Installation]: https://guides.cocoapods.org/using/getting-started.html#getting-started\n\n### Swift Package Manager\nSPM is built into new versions of Xcode. To install SwCrypt with SPM:\n\n1. Open your project in Xcode\n\n2. Click \"File\" -\u003e \"Swift Packages\" -\u003e \"Add Package Dependency...\"\n\n3. Paste the following URL: https://github.com/soyersoyer/SwCrypt\n\n4. Click \"Next\" -\u003e \"Next\" -\u003e \"Finish\"\n \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/TakeScoop/SwiftyRSA\u003e\n - \u003chttps://github.com/henrinormak/Heimdall\u003e\n - \u003chttps://github.com/btnguyen2k/swift-rsautils\u003e\n\nLicense\n-------\n\nThis project is copyrighted under the MIT license.\n","funding_links":[],"categories":["Security","Swift"],"sub_categories":["Encryption","Other free courses"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyersoyer%2FSwCrypt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoyersoyer%2FSwCrypt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyersoyer%2FSwCrypt/lists"}