ObjectivePGP is an implementation of OpenPGP protocol for iOS and macOS. OpenPGP is the most widely used email encryption standard. It is defined by the OpenPGP Working Group of the Internet Engineering Task Force (IETF).
Here is the blog post story.
You want to help, great! Go ahead and fork our repo, make your changes and send us a pull request.
You are welcome to contribute. See CONTRIBUTING.md
Please create Pull Request.
The ObjectivePGP stays under a dual license:
Free for non-commercial use, covered by the variant of BSD license. That means you have to mention Marcin Krzyżanowski as the original author of this code and reproduce the LICENSE text inside your app.
Commercial-use license to use in commercial products. Please bear in mind that some free products remain commercial products. Please contact me via email for details.
Not sure what to choose? check FAQ
dependencies: [
.package(url: "https://github.com/krzyzanowskim/ObjectivePGP.git", .upToNextMinor(from: "0.99.4"))
]
pod 'ObjectivePGP'
ObjectivePGP comes with the Frameworks for the latest release, you can copy and embed in your project:
Objective-C
#import<ObjectivePGP/ObjectivePGP.h>Swift
import ObjectivePGPNSArray<PGPKey *> *keys = [ObjectivePGP readKeysFromPath:@"/path/to/key.asc"error:nil];letkeys=tryObjectivePGP.readKeys(fromPath:"/path/to/key.asc")Keyring is a storage (in memory or on disk) that keep all sorts of PGP keys.
PGPKeyring *keyring = ObjectivePGP.defaultKeyring;
PGPKeyring *keyring = [[PGPKeyring alloc] init];
NSArray<PGPKey *> *allKeys = keyring.keys;
[keyring importKeys:@[key]];
[keyring deleteKeys:@[key]];
[keyring importKey:@"979E4B03DFFE30C6"fromPath:@"/path/to/secring.gpg"];
PGPKey *key = [keyring findKeyWithIdentifier:@"979E4B03DFFE30C6"];
NSArray<PGPKey *> keys = [pgp findKeysForUserID:@"Name <email@example.com>"];letkeyring=ObjectivePGP.defaultKeyring
letkeyring=Keyring()letallKeys= keyring.keys
keyring.import(keys:[key])
keyring.delete(keys:[key])
keyring.import(keyIdentifier:"979E4B03DFFE30C6", fromPath:"/path/to/secring.gpg")iflet key = keyring.findKey("979E4B03DFFE30C6"){
// key found in keyring
}
keyring.findKeys("Name <email@example.com>").forEach(key){
// process key
}// Write keyring to file
[[keyring export:error] writeToURL:[NSURLfileURLWithString:@"keyring.gpg"]];
// Public keys dataNSData *publicKeys = [keyring exportKeysOfType:PGPKeyTypePublic error:nil];// Write keyring to file
try keyring.export().write(to:URL(fileURLWithPath:"keyring.gpg"))
// Public keys (Data)
letpublicKeys= keyring.exportKeys(of:.public)Sign a data with a key:
NSData *signature = [ObjectivePGP sign:fileContent detached:YESusingKeys:@[key] passphraseForKey:nilerror:nil];
[ObjectivePGP verify:fileContent withSignature:signature usingKeys:@[key] passphraseForKey:nilerror:nil];letsignature=tryObjectivePGP.sign(encryptedBin, detached:true, using:[key1])tryObjectivePGP.verify(encryptedBin, withSignature: signature, using:[key1])NSData *encrypted = [ObjectivePGP encrypt:fileContent addSignature:YESusingKeys:@[key] passphraseForKey:nilerror:nil];
[ObjectivePGP decrypt:encrypted andVerifySignature:YESusingKeys:@[key] passphraseForKey:nilerror:nil];letencrypted=tryObjectivePGP.encrypt(fileContent),addSignature:true,using:[key1,key2])letdecrypted=tryObjectivePGP.decrypt(encrypted, andVerifySignature:true, using:[key1])PGPKeyGenerator *generator = [[PGPKeyGenerator alloc] init];
PGPKey *key = [generator generateFor:@"Marcin <marcin@example.com>"passphrase:nil];
NSData *publicKeyData = [key export:PGPKeyTypePublic error:nil];
NSData *secretKeyData = [key export:PGPKeyTypeSecret error:nil];letkey=KeyGenerator().generate(for:"marcin@example.com", passphrase:"password")letpublicKey=try key.export(keyType:.public)letsecretKey=try key.export(keyType:.secret)ASCII armor is a binary-to-textual encoding converter. ASCII armor involves encasing encrypted messaging in ASCII so that they can be sent in a standard messaging format such as email.
Example:
-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: For more info see https://www.objectivepgp.org
[...]
-----END PGP PUBLIC KEY BLOCK-----
Class PGPArmor can be used to convert binary format to ASCII format
NSString *armoredKey = [PGPArmor armoredData:encrypted as:PGPArmorPublicKey];letarmoredKey=Armor.armored(Data(), as:.publicKey)When convert manually, it is important to use right PGPArmorType value that define the header. It may be a tricky part so here's the cheatsheet:
| Type data | PGPArmorType | Example |
|---|---|---|
| Encrypted | PGPArmorMessage | Armor.armored(ObjectivePGP.encrypt(...), as: .message) |
| Decrypted | PGPArmorMessage | Armor.armored(ObjectivePGP.decrypt(...), as: .message) |
| Public key | PGPArmorTypePublic | Armor.armored(key.export(), as: .publicKey) |
| Secret key | PGPArmorTypeSecret | Armor.armored(key.export(), as: .secretKey) |
For any result of encryption the type is PGPArmorMessage
See CHANGELOG
Known limitations:
- Cleartext signature.
To date the ObjectivePGP code base has undergone a complete security audit from Cure53.
This product uses software developed by the OpenSSL Project for use in the OpenSSL Toolkit. (http://www.openssl.org/)
