SwiftyRSA is a Swift library enabling RSA public/private key encryption, decryption, signing, and verification for secure data handling in iOS applications.
RSA public/private key encryption in Swift
SwiftyRSA is primarily used by iOS developers who need to implement RSA encryption and digital signature functionality within their apps to secure sensitive data such as passwords or personal information. It is especially useful for encrypting data before transmission or verifying data authenticity using public/private key pairs.
SwiftyRSA requires Swift 5.0+ and Xcode 10.2 or later. It supports multiple key formats and padding schemes but primarily uses PKCS#1 padding. Users should handle key management securely and ensure keys are stored and loaded safely. The library also supports Objective-C integration via a dedicated pod.
Use Swift 5.0 and Xcode 10.2 or higher
Install via CocoaPods with `pod 'SwiftyRSA'`
Install Objective-C support via `pod 'SwiftyRSA/ObjC'`
Install via Carthage with `github "TakeScoop/SwiftyRSA"`
let publicKey = try PublicKey(pemNamed: "public")
Load a public key from a PEM file named 'public'
let clear = try ClearMessage(string: "Clear String", using: .utf8) let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)
Encrypt a clear text string using a public RSA key with PKCS#1 padding
let privateKey = try PrivateKey(pemNamed: "private") let encrypted = try EncryptedMessage(base64Encoded: "AAA===") let clear = try encrypted.decrypted(with: privateKey, padding: .PKCS1)
Decrypt an encrypted Base64 string using a private RSA key with PKCS#1 padding
let signature = clear.signed(with: privateKey, digestType: .sha1)
Generate a digital signature for a clear message using a private key and SHA-1 digest
let isSuccessful = try signature.verify(with: publicKey, digestType: .sha1)
Verify a digital signature using a public key and SHA-1 digest
let publicKey = try PublicKey(base64Encoded: base64String)
Create a public key from a Base64 encoded string
let privateKey = try PrivateKey(data: data)
Create a private key from raw data