Welcome to the documentation for CryptoSwift – a fast, safe and convenient Swift framework for encryption and cryptographic operations.
Table of Contents
Getting Started
CryptoSwift provides a wide range of cryptographic functions to ensure your data remains secure. To use CryptoSwift in your project, follow the steps below:
Installation
Follow these steps to install CryptoSwift:
- Open your Terminal.
- Navigate to your project directory.
- Run the following command to include CryptoSwift via CocoaPods:
pod 'CryptoSwift'
If you prefer using Swift Package Manager, add the following to your Package.swift:
.package(url: "https://github.com/krzyzanowskim/CryptoSwift.git", from: "1.4.0")
Usage Examples
Here are some examples demonstrating the usage of CryptoSwift:
Symmetric Encryption
To encrypt and decrypt data symmetrically, CryptoSwift offers several algorithms, including:
- AES (Advanced Encryption Standard)
- Blowfish
- Twofish
Here’s an example of using AES:
import CryptoSwift
let key: [UInt8] = Array("SecretCryptKey123".utf8)
let encrypted = try! AES(key: key, blockMode: .ECB).encrypt(Array("Hello, World!".utf8))
let decrypted = try! AES(key: key, blockMode: .ECB).decrypt(encrypted)
Asymmetric Encryption
To perform asymmetric encryption using public and private keys, CryptoSwift provides support for the RSA algorithm:
import CryptoSwift
let keys = try! RSA.generateKeys()
let publicKey = keys.publicKey
let privateKey = keys.privateKey
let encrypted = try! RSA.encrypt(data: Array("Hello, World!".utf8), publicKey: publicKey)
let decrypted = try! RSA.decrypt(encrypted: encrypted, privateKey: privateKey)
Hash Functions
CryptoSwift allows you to compute cryptographic hash functions, including:
- MD5
- SHA-1
- SHA-256
Here’s an example using MD5:
import CryptoSwift
let hash = try! "Hello, World!".md5()
HMAC
CryptoSwift supports HMAC (Hash-based Message Authentication Code), which uses a hash function and a secret key to securely verify the integrity and authenticity of a message:
import CryptoSwift
let key: [UInt8] = Array("SecretKey123".utf8)
let hmac = try! HMAC(key: key, variant: .sha256).authenticate(Array("Hello, World!".utf8))
FAQ
Here are some frequently asked questions:
Question 1: How do I import CryptoSwift into my Swift project?
Answer:
To import CryptoSwift, you can use either CocoaPods or Swift Package Manager. Refer to the Installation section for detailed instructions.
Question 2: Can I use CryptoSwift in my Objective-C project?
Answer:
No, CryptoSwift is a Swift framework and is primarily designed for use in Swift projects. It is not compatible with Objective-C.
Contributing
If you would like to contribute to CryptoSwift, please follow the guidelines outlined in the CONTRIBUTING.md file in the repository.
License
CryptoSwift is released under the MIT License. For more details, see the LICENSE file.