SwiftyRSA

repository·master·Indexed 23 days ago

https://github.com/takescoop/swiftyrsa

A Swift library providing high-level wrappers for RSA encryption, decryption, signing, and verification. It is optimized for iOS compatibility through X.509 header management and keychain integration. Supports Swift 5.0+ and Xcode 10.2+, with availability for Objective-C projects.

Tokens
1.2K
Snippets
4
Records
9
Agent score
30%

What's inside SwiftyRSA

  1. Generate RSA keys using ssh-keygen

    master

    To generate PEM public and private keys externally, use the following ssh-keygen commands:

    $ ssh-keygen -t rsa -m PEM -f ~/mykey -N ''
    $ cat ~/mykey > ~/private.pem
    $ ssh-keygen -f ~/mykey.pub -e -m pem > ~/public.pem
  2. Install SwiftyRSA

    master

    SwiftyRSA requires Swift 5.0+ and Xcode 10.2+. You can install it using CocoaPods or Carthage.

    Swift 5.0+

    CocoaPods: Add the following to your Podfile:

    pod 'SwiftyRSA'

    Carthage: Add the following to your Cartfile:

    github "TakeScoop/SwiftyRSA"

    Objective-C

    To use SwiftyRSA in an Objective-C project, use the following Podfile entry:

    pod 'SwiftyRSA/ObjC'
    pod 'SwiftyRSA'
    # or for Objective-C
    pod 'SwiftyRSA/ObjC'
  3. Sign and verify digital signatures

    master

    SwiftyRSA allows you to sign data with a private key and verify it with a public key. Signing automatically calculates a SHA digest of the provided ClearMessage.

    Sign with a private key

    Use clear.signed(with:digestType:). Supported digest types include .sha1.

    Verify with a public key

    Use clear.verify(with:signature:digestType:) to check if a Signature is valid for the given message.

  4. Decrypt a message with a private key

    master

    To decrypt data, initialize an EncryptedMessage (e.g., from a Base64 string) and call .decrypted(with:padding:) using a PrivateKey instance. The result is a ClearMessage object which can be converted back to data, a Base64 string, or a UTF-8 string.

    let encrypted = try EncryptedMessage(base64Encoded: base64String)
    let clear = try encrypted.decrypted(with: privateKey, padding: .PKCS1)
    
    // Accessing the decrypted content:
    let data = clear.data
    let base64String = clear.base64Encoded
    let string = try clear.string(using: .utf8)
  5. Encrypt a message with a public key

    master

    To encrypt data, create a ClearMessage from a string or data, then call .encrypted(with:padding:) using a PublicKey instance. You can specify the padding, such as .PKCS1.

    Supported padding: .PKCS1.

    let str = "Clear Text"
    let clear = try ClearMessage(string: str, using: .utf8)
    let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)
    
    let data = encrypted.data
    let base64String = encrypted.base64Encoded
  6. Export key content

    master

    Once you have a key instance, you can export its content using the following methods:

    • key.pemString(): Returns the PEM representation.
    • key.base64String(): Returns the Base64 representation.
    • key.data(): Returns the raw data.
    • key.reference: Returns the underlying SecKey reference.
    • key.originalData: Returns the original data used to create the key.
  7. Handle X.509 certificate headers

    master

    SwiftyRSA supports X.509 certificates for public keys. You can manually add or strip the X.509 header using utility methods.

    Warning: Creating a PublicKey instance or using export methods will automatically strip the header to ensure compatibility with the iOS keychain.

    Add X.509 header

    let publicKeyData = try publicKey.data()
    let publicKeyWithHeader = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData)

    Strip X.509 header

    let headerLessData = try SwiftyRSA.stripKeyHeader(keyData: publicKeyWithHeader)
  8. Generate an RSA key pair

    master

    You can generate a new RSA key pair directly within the library using SwiftyRSA.generateRSAKeyPair(sizeInBits:).

    let keyPair = SwiftyRSA.generateRSAKeyPair(sizeInBits: 2048)
    let privateKey = keyPair.privateKey
    let publicKey = keyPair.publicKey
  9. Initialize Public and Private keys from various formats

    master

    SwiftyRSA provides multiple ways to instantiate PublicKey and PrivateKey objects depending on your key source:

    • DER file: PublicKey(derNamed: "name") / PrivateKey(derNamed: "name")
    • PEM file: PublicKey(pemNamed: "name") / PrivateKey(pemNamed: "name")
    • PEM string: PublicKey(pemEncoded: str) / PrivateKey(pemEncoded: str)
    • Base64 string: PublicKey(base64Encoded: str) / PrivateKey(base64Encoded: str)
    • Raw Data: PublicKey(data: data) / PrivateKey(data: data)
    • SecKey reference: PublicKey(reference: secKey) / PrivateKey(reference: secKey)