{"id":13466030,"url":"https://github.com/Kitura/BlueCryptor","last_synced_at":"2025-03-25T21:31:05.211Z","repository":{"id":44901031,"uuid":"56717465","full_name":"Kitura/BlueCryptor","owner":"Kitura","description":"Swift cross-platform crypto library using CommonCrypto/libcrypto","archived":false,"fork":false,"pushed_at":"2024-11-20T04:43:14.000Z","size":283,"stargazers_count":191,"open_issues_count":12,"forks_count":47,"subscribers_count":34,"default_branch":"master","last_synced_at":"2025-03-11T22:35:05.935Z","etag":null,"topics":["commoncrypto","cryptography","libcrypto","linux","macos","openssl","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Kitura.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","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":"2016-04-20T20:05:17.000Z","updated_at":"2025-02-27T05:10:39.000Z","dependencies_parsed_at":"2024-05-13T14:58:42.511Z","dependency_job_id":"8239a766-4529-4ca1-8489-aaee15456987","html_url":"https://github.com/Kitura/BlueCryptor","commit_stats":{"total_commits":330,"total_committers":17,"mean_commits":19.41176470588235,"dds":0.5545454545454546,"last_synced_commit":"cec97c24b111351e70e448972a7d3fe68a756d6d"},"previous_names":["ibm-swift/bluecryptor"],"tags_count":140,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FBlueCryptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FBlueCryptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FBlueCryptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Kitura%2FBlueCryptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Kitura","download_url":"https://codeload.github.com/Kitura/BlueCryptor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245547160,"owners_count":20633312,"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":["commoncrypto","cryptography","libcrypto","linux","macos","openssl","swift"],"created_at":"2024-07-31T15:00:38.262Z","updated_at":"2025-03-25T21:31:05.204Z","avatar_url":"https://github.com/Kitura.png","language":"Swift","funding_links":[],"categories":["Libs","Security [🔝](#readme)","Swift"],"sub_categories":["Security"],"readme":"[![APIDoc](https://img.shields.io/badge/docs-kitura.io-1FBCE4.svg)](https://www.kitura.io/packages.html#all)\n[![SwiftPackageIndex](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FKitura%2FBlueCryptor%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/Kitura/BlueCryptor)\n[![TravisCI](https://travis-ci.org/Kitura/BlueCryptor.svg?branch=master)](https://travis-ci.org/Kitura/BlueCryptor)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FKitura%2FBlueCryptor%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/Kitura/BlueCryptor)\n[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)\n\n# BlueCryptor\nSwift cross-platform crypto library derived from [IDZSwiftCommonCrypto](https://github.com/iosdevzone/IDZSwiftCommonCrypto).\n\n**IMPORTANT NOTE:** This release is **NOT** entirely source code compatible with previous releases.  There are instances where *exceptions* are thrown now instead of the framework calling *fatalError()*.  This means that there are more *recoverable* errors in the library than before.  The only time that *fatalError()* is called is to indicate either a *programming error* or a *non-recoverable system error*.\n\n**Note:** On macOS and iOS, _BlueCryptor_ uses the Apple provided *CommonCrypto* library. On Linux, it uses *libcrypto from the OpenSSL project*.\n\n## Prerequisites\n\n### Swift\n\n* Swift Open Source `swift-5.1-RELEASE` toolchain (**Minimum REQUIRED for latest release**)\n* Swift Open Source `swift-5.4-RELEASE` toolchain (**Recommended**)\n* Swift toolchain included in *Xcode Version 11.0 or higher*.\n\n### macOS\n\n* macOS 10.14.6 (*Mojave*) or higher.\n* Xcode Version 11.0 or higher using one of the above toolchains.\n* Xcode Version 12.5 or higher using the included toolchain (*Recommended*).\n* CommonCrypto is provided by macOS.\n\n### iOS\n\n* iOS 10.0 or higher\n* Xcode Version 11.0 or higher using one of the above toolchains.\n* Xcode Version 12.5 or higher using the included toolchain (*Recommended*).\n* CommonCrypto is provided by iOS.\n\n### Linux\n\n* Ubuntu 16.04 (or 16.10 but only tested on 16.04) and 18.04.\n* One of the Swift Open Source toolchain listed above.\n* OpenSSL is provided by the distribution.  **Note:** 1.0.x, 1.1.x and later releases of OpenSSL are supported.\n* The appropriate **libssl-dev** package is required to be installed when building.\n\n## Build\n\nTo build **Cryptor** from the command line:\n\n```\n% cd \u003cpath-to-clone\u003e\n% swift build\n```\n\n## Testing\n\nTo run the supplied unit tests for **Cryptor** from the command line:\n\n```\n% cd \u003cpath-to-clone\u003e\n% swift build\n% swift test\n```\n\n## Getting started\n\n### Including in your project\n\n#### Swift Package Manager\n\nTo include BlueCryptor into a Swift Package Manager package, add it to the `dependencies` attribute defined in your `Package.swift` file. You can select the version using the `majorVersion` and `minor` parameters. For example:\n```\n\tdependencies: [\n\t\t.Package(url: \"https://github.com/Kitura/BlueCryptor.git\", majorVersion: \u003cmajorVersion\u003e, minor: \u003cminor\u003e)\n\t]\n```\n\n#### Carthage\nTo include BlueCryptor in a project using Carthage, add a line to your `Cartfile` with the GitHub organization and project names and version. For example:\n```\n\tgithub \"Kitura/BlueCryptor\" ~\u003e \u003cmajorVersion\u003e.\u003cminor\u003e\n```\n\n#### CocoaPods\nTo include BlueCryptor in a project using CocoaPods, you just add `BlueCryptor` to your `Podfile`, for example:\n```\n    platform :ios, '10.0'\n\n    target 'MyApp' do\n        use_frameworks!\n        pod 'BlueCryptor'\n    end\n```\n\n### Before starting\n\nThe first thing you need to do is import the Cryptor framework.  This is done by the following:\n```swift\nimport Cryptor\n```\n\n## API\n\n### Cryptor\n\nThe following code demonstrates encryption and decryption using `AES` single block CBC mode using optional chaining.\n```swift\nlet key = CryptoUtils.byteArray(fromHex: \"2b7e151628aed2a6abf7158809cf4f3c\")\nlet iv = CryptoUtils.byteArray(fromHex: \"00000000000000000000000000000000\")\nlet plainText = CryptoUtils.byteArray(fromHex: \"6bc1bee22e409f96e93d7e117393172a\")\n\nvar textToCipher = plainText\nif plainText.count % Cryptor.Algorithm.aes.blockSize != 0 {\n\ttextToCipher = CryptoUtils.zeroPad(byteArray: plainText, blockSize: Cryptor.Algorithm.aes.blockSize)\n}\ndo {\n\tlet cipherText = try Cryptor(operation: .encrypt, algorithm: .aes, options: .none, key: key, iv: iv).update(byteArray: textToCipher)?.final()\n\t\t\n\tprint(CryptoUtils.hexString(from: cipherText!))\n\t\t\n\tlet decryptedText = try Cryptor(operation: .decrypt, algorithm: .aes, options: .none, key: key, iv: iv).update(byteArray: cipherText!)?.final()\n\n\tprint(CryptoUtils.hexString(from: decryptedText!))\n} catch let error {\n\tguard let err = error as? CryptorError else {\n\t\t// Handle non-Cryptor error...\n\t\treturn\n\t}\n\t// Handle Cryptor error... (See Status.swift for types of errors thrown)\n}\n```\n\n### Digest\n\nThe following example illustrates generating an `MD5` digest from both a `String` and an instance of `NSData`.\n```swift\nlet qbfBytes : [UInt8] = [0x54,0x68,0x65,0x20,0x71,0x75,0x69,0x63,0x6b,0x20,0x62,0x72,0x6f,0x77,0x6e,0x20,0x66,0x6f,0x78,0x20,0x6a,0x75,0x6d,0x70,0x73,0x20,0x6f,0x76,0x65,0x72,0x20,0x74,0x68,0x65,0x20,0x6c,0x61,0x7a,0x79,0x20,0x64,0x6f,0x67,0x2e]\nlet qbfString = \"The quick brown fox jumps over the lazy dog.\"\n\n// String...\nlet md5 = Digest(using: .md5)\nmd5.update(string: qfbString)\nlet digest = md5.final()\n\n// NSData using optional chaining...\nlet qbfData = CryptoUtils.data(from: qbfBytes)\nlet digest = Digest(using: .md5).update(data: qbfData)?.final()\n```\n\n### HMAC\n\nThe following demonstrates generating an `SHA256` HMAC using byte arrays for keys and data.\n```swift\nlet myKeyData = \"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b\"\nlet myData = \"4869205468657265\"\nlet key = CryptoUtils.byteArray(fromHex: myKeyData)\nlet data : [UInt8] = CryptoUtils.byteArray(fromHex: myData)\n\nlet hmac = HMAC(using: HMAC.Algorithm.sha256, key: key).update(byteArray: data)?.final()\n```\n\n### Key Derivation\n\nThe following illustrates generating a key using a password, salt, number of rounds and a specified derived key length using the SHA1 algorithm. Then it shows how to generate a `String` from resultant key.\n```swift\nlet password = \"password\"\nlet salt = salt\nlet rounds: UInt = 2\nlet derivedKeyLen = 20\ndo {\n\tlet key = PBKDF.deriveKey(fromPassword: password, salt: salt, prf: .sha1, rounds: rounds, derivedKeyLength: derivedKeyLen)\n\tlet keyString = CryptoUtils.hexString(from: key)\n} catch let error {\n\tguard let err = error as? CryptorError else {\n\t\t// Handle non-Cryptor error...\n\t\treturn\n\t}\n\t// Handle Cryptor error... (See Status.swift for types of errors thrown)\n}\n```\n\n### Random Byte Generation\n\nThe following demonstrates generating random bytes of a given length.\n```swift\nlet numberOfBytes = 256*256\ndo {\n\tlet randomBytes = try Random.generate(byteCount: numberOfBytes)\n} catch {\n  \tprint(\"Error generating random bytes\")\n}\n```\n\n### Utilities\n\n**Cryptor** also provides a set of data manipulation utility functions for conversion of data from various formats:\n- To byteArray (`[UInt8]`)\n\t- From hex string\n\t- From UTF8 string\n- To `Data`\n\t- From hex string\n\t- From byte array (`[UInt8]`)\n- To `NSData`\n\t- From hex string\n\t- From byte array (`[UInt8]`)\n- To `NSString`\n\t- From byte array (`[UInt8]`)\n- To hexList (`String`)\n\t- From byte array (`[UInt8]`)\n\nAlso provided are an API to pad a byte array (`[UInt8]`) such that it is an integral number of `block size in bytes` long.\n- ```func zeroPad(byteArray: [UInt8], blockSize: Int) -\u003e [UInt8]```\n- ```func zeroPad(string: String, blockSize: Int) -\u003e [UInt8]```\n\n## Restrictions\n\nThe following algorithm is not available on Linux since it is not supported by *OpenSSL*.\n- Digest: MD2\n\nIn all cases, use of unsupported APIs or algorithms will result in a Swift `fatalError()`, terminating the program and should be treated as a programming error.\n\n## Community\n\nWe love to talk server-side Swift and Kitura. Join our [Slack](http://swift-at-ibm-slack.mybluemix.net/) to meet the team!\n\n## License\n\nThis library is licensed under Apache 2.0. Full license text is available in [LICENSE](https://github.com/Kitura/BlueCryptor/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKitura%2FBlueCryptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FKitura%2FBlueCryptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FKitura%2FBlueCryptor/lists"}