snej / MYCrypto (http://mooseyard.com/projects/MYCrypto/)

A high-level cryptography API for Mac OS X and iPhone.

Clone this repository (size: 981.9 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/snej/mycrypto/

Examples of Using MYCrypto

Creating an RSA key-pair:

1
MYPrivateKey *keyPair = [[MYKeychain defaultKeychain] generateRSAKeyPairOfSize: 2048];

Creating a self-signed identity certificate:

1
2
3
4
5
6
7
8
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
                @"alice", @"Common Name",
                @"Alice", @"Given Name",
                @"Lidell", @"Surname",
                nil];
MYIdentity *ident = [keyPair createSelfSignedIdentityWithAttributes: attrs];

NSData *certData = ident.certificateData;

Signing and encrypting a message:

NSData *cleartext = [@"Attack at dawn" dataUsingEncoding: NSUTF8StringEncoding];
MYEncoder *encoder = [[MYEncoder alloc] init];
[encoder addSigner: ident];
[encoder addRecipient: bob];
[encoder addRecipient: carla];
[encoder addData: cleartext];
[encoder finish];
NSData *ciphertext = encoder.encodedData;

sendMessage(ciphertext);

Verifying and decoding a message:

NSData *ciphertext = receiveMessage();
NSError *error;
MYDecoder *decoder = [[MYDecoder alloc] initWithData: ciphertext error: &error];
if (!decoder)
    return NO;

if (!decoder.isSigned)
    return NO;
decoder.policy = [MYCertificate X509Policy];
NSMutableArray *signerCerts = [NSMutableArray array];
for (MYSigner *signer in decoder.signers) {
    if (signer.status != kCMSSignerValid) {
        return NO;
    [signerCerts addObject: signer.certificate];
}

NSData *plaintext = decoder.content;
processMessage(plaintext, signerCerts);