ObjectivePGP Documentation

repository·main·Indexed 20 days ago

https://github.com/krzyzanowskim/objectivepgp

An implementation of the OpenPGP protocol for iOS and macOS. Provides functionality for encryption, decryption, signing, verification, and key management, including key generation via KeyGenerator, key storage using Keyring, and ASCII armor conversion via the Armor class.

Tokens
1.3K
Snippets
9
Records
10
Agent score
23%

What's inside ObjectivePGP

  1. Manage keys using Keyring

    main

    A Keyring acts as a storage container (in-memory or on-disk) for PGP keys. You can use ObjectivePGP.defaultKeyring for a shared instance or initialize a new Keyring().

    Common operations include:

    • Accessing all keys via .keys
    • Importing keys via .import(keys:) or .import(keyIdentifier:fromPath:)
    • Deleting keys via .delete(keys:)
    • Finding keys by identifier or User ID
    let keyring = Keyring()
    let allKeys = keyring.keys
    keyring.import(keys: [key])
    keyring.delete(keys: [key])
    
    keyring.import(keyIdentifier:"979E4B03DFFE30C6", fromPath:"/path/to/secring.gpg")
    if let key = keyring.findKey("979E4B03DFFE30C6") {
        // key found in keyring
    }
    
    keyring.findKeys("Name <email@example.com>").forEach(key) { 
        // process key
    }
  2. Install ObjectivePGP via Swift Package Manager

    main

    To add ObjectivePGP to your Swift project, add the following dependency to your Package.swift file. It is recommended to use .upToNextMinor(from: "0.99.4") to ensure compatibility.

    dependencies: [
        .package(url: "https://github.com/krzyzanowskim/ObjectivePGP.git", .upToNextMinor(from: "0.99.4"))
    ]
  3. Convert binary data to ASCII armor

    main

    Use the Armor class to convert binary PGP data (keys or messages) into an ASCII-armored format suitable for text-based transmission like email.

    Important: Use the correct PGPArmorType to ensure the correct headers are applied.

    // Example: Armoring an encrypted message
    let armoredMessage = Armor.armored(encryptedData, as: .message)
    
    // Example: Armoring a public key
    let armoredKey = Armor.armored(publicKeyData, as: .publicKey)
  4. Export keys and keyrings

    main

    You can export the entire contents of a Keyring to a file, or export specific keys as raw data.

    To export specific keys by type, use exportKeys(of:) with .public or .secret.

    // Write keyring to file
    try keyring.export().write(to: URL(fileURLWithPath: "keyring.gpg"))
    
    // Public keys (Data)
    let publicKeys = keyring.exportKeys(of: .public)
  5. Generate a new PGP key pair

    main

    Use KeyGenerator to create a new PGP key pair for a specific User ID. You can optionally provide a passphrase for the secret key.

    let key = KeyGenerator().generate(for: "marcin@example.com", passphrase: "password")
    let publicKey = try key.export(keyType: .public)
    let secretKey = try key.export(keyType: .secret)
  6. Encrypt and decrypt data

    main

    Perform encryption and decryption operations. When encrypting, you can optionally add a signature. When decrypting, you can optionally verify the signature of the sender.

    let encrypted = try ObjectivePGP.encrypt(fileContent, addSignature: true, using: [key1, key2])
    let decrypted = try ObjectivePGP.decrypt(encrypted, andVerifySignature: true, using: [key1])
  7. Read PGP keys from a file path

    main

    You can load one or more PGP keys (public or private) directly from a file on disk using readKeys(fromPath:) in Swift or readKeysFromPath:error: in Objective-C.

    let keys = try ObjectivePGP.readKeys(fromPath: "/path/to/key.asc")
  8. Sign and verify data

    main

    Use ObjectivePGP.sign to create a signature for data (with an option for detached signatures) and ObjectivePGP.verify to validate a signature against the original data using the appropriate keys.

    let signature = try ObjectivePGP.sign(encryptedBin, detached: true, using: [key1])
    try ObjectivePGP.verify(encryptedBin, withSignature: signature, using: [key1])