DMSOpenPGP 0.1.4

DMSOpenPGP 0.1.4

Maintained by jk234ert, MainasuK.



 
Depends on:
BouncyCastle-ObjC~> 0.1.0
OpenSSL-Universal~> 1.0.2.17
 

  • By
  • CMK

DMSOpenPGP

Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

DMSOpenPGP is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'DMSOpenPGP'

Usage

Setup

import BouncyCastle_ObjC

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // Add provider before use DMSOpenPGP (also add to extension if have)
    JavaSecuritySecurity.addProvider(with: BCJceProviderBouncyCastleProvider())

    return true
}

Keygen

do {
    let generateKeyData = GenerateKeyData(name: "Hello", email: "[email protected]", password: "Alice",
                                          masterKey: KeyData(), subkey: KeyData())
    let alice = try DMSPGPKeyRingFactory(generateKeyData: generateKeyData).keyRing
    let armoredPublicKey = alice.publicKeyRing.armored()
    let armoredSecretKey = alice.secretKeyRing.armored()
} catch {
    
}

Encrypt & Sign

// encrypt
guard let secretKeyRing = alice.secretKeyRing else {
    return
}

do {
    // with signature
    let encryptor = try DMSPGPEncryptor(publicKeyRings: [alice.publicKeyRing, bob.publicKeyRing, eve.publicKeyRing],
                                        secretKeyRing: secretKeyRing,
                                        password: "Alice")
    let encryptedWithSignatureMessage = try encryptor.encrypt(message: "Message")
    
    // without signature
    let encryptor2 = try DMSPGPEncryptor(publicKeyRings: [alice.publicKeyRing, bob.publicKeyRing, eve.publicKeyRing])
    let encryptedWithoutSignatureMessage = try encryptr2.encrypt(message: "Message")

} catch {
   
}
// Sign
guard let secretKeyRing = alice.secretKeyRing else {
    return
}

do {
    let encryptor = try DMSPGPEncryptor(secretKeyRing: secretKeyRing, password: "Alice")
    let cleartext = try encryptor.encrypt(message: "Message")
    // or
    let signer = try  DMSPGPSigner(secretKeyRing: keyRing.secretKeyRing, password: password)
    let cleartext2 = signer.sign(message: "Message")
} catch {
    
}

Decrypt & Verify

do {
    let decryptor = try DMSPGPDecryptor(armoredMessage: encryptedMessage)
    // decryptor.encryptingKeyIDs contains all decryptable secret keys' keyID   
    let decryptKey = decryptor.encryptingKeyIDs.compactMap { keyID in
        return alice.secretKeyRing?.getDecryptingSecretKey(keyID: keyID)
    }.first
    
    guard let secretKey = decryptKey else { 
        return
    }

    let message = try decryptor.decrypt(secretKey: secretKey, password: "Alice")
    let signatureVerifier = DMSPGPSignatureVerifier(message: message, onePassSignatureList: decryptor.onePassSignatureList, signatureList: decryptor.signatureList)
    let verifyResult = signatureVerifier.verifySignature(use: alice.publicKeyRing)
}

Please check DMSOpenPGP/Tests/DMSOpenPGPTests.swift in Example Pods unit tests to see more details.

Dependencies

License

DMSOpenPGP is available under the AGPL license. See the LICENSE file for more info.