### Install via Swift Package Manager Source: https://objectivepgp.com/ Add the library as a dependency in your Package.swift file. ```swift dependencies: [ .package(url: "https://github.com/krzyzanowskim/ObjectivePGP.git", .upToNextMinor(from: "0.99.4")) ] ``` -------------------------------- ### ASCII Armor Public Key Block Example Source: https://objectivepgp.com/ This is an example of a PGP public key block formatted with ASCII armor. It is used for sending keys in plain text formats like email. ```plaintext -----BEGIN PGP PUBLIC KEY BLOCK----- Comment: For more info see https://www.objectivepgp.org [...] -----END PGP PUBLIC KEY BLOCK----- ``` -------------------------------- ### Install via CocoaPods Source: https://objectivepgp.com/ Add the dependency to your Podfile. ```ruby pod 'ObjectivePGP' ``` -------------------------------- ### Import ObjectivePGP Source: https://objectivepgp.com/ Include the library in your source files. ```objective-c #import ``` ```swift import ObjectivePGP ``` -------------------------------- ### Read PGP Keys Source: https://objectivepgp.com/ Load public or private keys from a file path. ```objective-c NSArray *keys = [ObjectivePGP readKeysFromPath:@"/path/to/key.asc" error:nil]; ``` ```swift let keys = try ObjectivePGP.readKeys(fromPath: "/path/to/key.asc") ``` -------------------------------- ### Sign and Verify Data Source: https://objectivepgp.com/ Sign data using a key and verify signatures. ```objective-c NSData *signature = [ObjectivePGP sign:fileContent detached:YES usingKeys:@[key] passphraseForKey:nil error:nil]; [ObjectivePGP verify:fileContent withSignature:signature usingKeys:@[key] passphraseForKey:nil error:nil]; ``` ```swift let signature = try ObjectivePGP.sign(encryptedBin, detached:true, using: [key1]) try ObjectivePGP.verify(encryptedBin, withSignature: signature, using: [key1]) ``` -------------------------------- ### Armoring Public Key Data in Swift Source: https://objectivepgp.com/ Use the `.publicKey` armor type when armoring exported public keys. This ensures the correct header and format for public key data. ```swift Armor.armored(key.export(), as: .publicKey) ``` -------------------------------- ### Manage Keyring Source: https://objectivepgp.com/ Perform operations on a PGP keyring, such as importing, deleting, or finding keys. ```objective-c PGPKeyring *keyring = ObjectivePGP.defaultKeyring; PGPKeyring *keyring = [[PGPKeyring alloc] init]; NSArray *allKeys = keyring.keys; [keyring importKeys:@[key]]; [keyring deleteKeys:@[key]]; [keyring importKey:@"979E4B03DFFE30C6" fromPath:@"/path/to/secring.gpg"]; PGPKey *key = [keyring findKeyWithIdentifier:@"979E4B03DFFE30C6"]; NSArray keys = [pgp findKeysForUserID:@"Name "]; ``` ```swift let keyring = ObjectivePGP.defaultKeyring 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 ").forEach(key) { // process key } ``` -------------------------------- ### Armoring Secret Key Data in Swift Source: https://objectivepgp.com/ When armoring exported secret keys, use the `.secretKey` armor type. This is crucial for maintaining the correct format and security of secret key material. ```swift Armor.armored(key.export(), as: .secretKey) ``` -------------------------------- ### Generate Key Pair Source: https://objectivepgp.com/ Create a new PGP key pair and export the public and secret components. ```objective-c PGPKeyGenerator *generator = [[PGPKeyGenerator alloc] init]; PGPKey *key = [generator generateFor:@"Marcin " passphrase:nil]; NSData *publicKeyData = [key export:PGPKeyTypePublic error:nil]; NSData *secretKeyData = [key export:PGPKeyTypeSecret error:nil]; ``` ```swift let key = KeyGenerator().generate(for: "marcin@example.com", passphrase: "password") let publicKey = try key.export(keyType: .public) let secretKey = try key.export(keyType: .secret) ``` -------------------------------- ### Export Keys Source: https://objectivepgp.com/ Export keyring data or specific key types to files or memory. ```objective-c // Write keyring to file [[keyring export:error] writeToURL:[NSURL fileURLWithString:@"keyring.gpg"]]; // Public keys data NSData *publicKeys = [keyring exportKeysOfType:PGPKeyTypePublic error:nil]; ``` ```swift // Write keyring to file try keyring.export().write(to: URL(fileURLWithPath: "keyring.gpg")) // Public keys (Data) let publicKeys = keyring.exportKeys(of: .public) ``` -------------------------------- ### Armoring Encrypted Data in Objective-C Source: https://objectivepgp.com/ Use the `PGPArmor` class to convert binary encrypted data into an ASCII-armored string format, suitable for transmission via standard text-based protocols. ```objective-c NSString *armoredKey = [PGPArmor armoredData:encrypted as:PGPArmorPublicKey]; ``` -------------------------------- ### Encrypt and Decrypt Data Source: https://objectivepgp.com/ Encrypt data with keys and decrypt it while verifying signatures. ```objective-c NSData *encrypted = [ObjectivePGP encrypt:fileContent addSignature:YES usingKeys:@[key] passphraseForKey:nil error:nil]; [ObjectivePGP decrypt:encrypted andVerifySignature:YES usingKeys:@[key] passphraseForKey:nil error:nil]; ``` ```swift let encrypted = try ObjectivePGP.encrypt(fileContent), addSignature: true, using: [key1, key2]) let decrypted = try ObjectivePGP.decrypt(encrypted, andVerifySignature: true, using: [key1]) ``` -------------------------------- ### Armoring Decrypted Data as a Message in Swift Source: https://objectivepgp.com/ To ASCII-armor decrypted data, specify `.message` as the armor type. This is useful for preparing decrypted content for storage or transmission. ```swift Armor.armored(ObjectivePGP.decrypt(...), as: .message) ``` -------------------------------- ### Armoring Encrypted Data in Swift Source: https://objectivepgp.com/ The `Armor` class in Swift can be used to ASCII-armor binary data, such as encrypted messages or keys, for safe transmission. ```swift let armoredKey = Armor.armored(Data(), as: .publicKey) ``` -------------------------------- ### Armoring Encrypted Data as a Message in Swift Source: https://objectivepgp.com/ When armoring the result of an encryption operation, use `PGPArmorMessage` or `.message` as the type to ensure correct header formatting. ```swift Armor.armored(ObjectivePGP.encrypt(...), as: .message) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.