1 #import <Foundation/Foundation.h>
    2 #import "SSCrypto.h"
    3 
    4 int main (int argc, const char * argv[])
    5 {
    6     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    7 
    8     SSCrypto *crypto;
    9     int n;
   10     
   11     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   12     
   13     // TEST 1: Get SHA1 digest for string
   14     
   15     // This is the same as running the following command in the terminal:
   16     // echo -n "foo" | openssl dgst -sha1
   17     
   18     NSString *name = @"foo";
   19     
   20     crypto = [[SSCrypto alloc] init];
   21     [crypto setClearTextWithString:name];
   22     
   23     NSLog(@"Name: %@", [crypto clearTextAsString]);
   24     NSLog(@"SHA1 Digest of Name using digest method: %@", [[crypto digest:@"SHA1"] hexval]);
   25     
   26     NSData *sha1Name = [SSCrypto getSHA1ForData:[name dataUsingEncoding:NSUTF8StringEncoding]];
   27     NSLog(@"SHA1 Digest using getSHA1ForData method: %@", [sha1Name hexval]);
   28     
   29     NSLog(@" ");
   30     NSLog(@" ");
   31     NSLog(@" ");
   32     
   33     [crypto release];
   34 
   35     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   36     
   37     // Test 2: Symmetric encryption and decryption using various ciphers
   38     
   39     NSData *seedData1 = [SSCrypto getKeyDataWithLength:32];
   40     crypto = [[SSCrypto alloc] initWithSymmetricKey:seedData1];
   41     
   42     NSArray *ciphers = [NSArray arrayWithObjects:@"aes256", @"aes128", @"blowfish", @"aes192",
   43         @"RC4", @"blowfish", @"RC5", @"des3", @"des", nil];
   44     
   45     NSString *password = @"pumpkin";
   46     [crypto setClearTextWithString:password];
   47     
   48     for(n = 0; n < [ciphers count]; n++)
   49     {
   50         NSData *cipherText = [crypto encrypt:[ciphers objectAtIndex:n]];
   51         NSData *clearText = [crypto decrypt:[ciphers objectAtIndex:n]];
   52         
   53         NSLog(@"Original password: %@", password);
   54         NSLog(@"Cipher text: '%@' using %@", [cipherText encodeBase64WithNewlines:NO], [ciphers objectAtIndex:n]);
   55         NSLog(@"Clear text: '%s' using %@", [clearText bytes], [ciphers objectAtIndex:n]);
   56         
   57         NSLog(@" ");
   58     }
   59     
   60     NSLog(@" ");
   61     NSLog(@" ");
   62     
   63     [crypto release];
   64     
   65     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   66     
   67     // Test 3: Generating digests from strings
   68     
   69     // This is the same as running the following command in the terminal:
   70     // echo -n "I like cheese" | openssl dgst -md5
   71     //
   72     // Where -md5 is the digest to use.
   73     // See man dgst for a list of all available digests.
   74     
   75     NSData *seedData2 = [SSCrypto getKeyDataWithLength:32];
   76     crypto = [[SSCrypto alloc] initWithSymmetricKey:seedData2];
   77     
   78     NSArray *digests = [NSArray arrayWithObjects:@"MD2", @"MD4", @"MD5", @"SHA1", @"RIPEMD160", nil];
   79     
   80     NSString *secret = @"I like cheese";
   81     [crypto setClearTextWithString:secret];
   82     
   83     for(n = 0; n < [digests count]; n++)
   84     {
   85         NSData *digest = [crypto digest:[digests objectAtIndex:n]];
   86         NSLog(@"'%@' %@ digest hexdump: %@", [crypto clearTextAsString], [digests objectAtIndex:n], [digest hexval]);
   87     }
   88     
   89     NSLog(@" ");
   90     NSLog(@" ");
   91     NSLog(@" ");
   92     
   93     [crypto release];
   94     
   95     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   96     
   97     // Generate public and private key for next 2 tests...
   98 
   99     // You can generate your own private key by running the following command in the terminal:
  100     // openssl genrsa -out private.pem 2048
  101     //
  102     // Where 2048 is the size of the private key.
  103     // You may used a bigger number.
  104     // It is probably a good recommendation to use at least 1024...
  105 
  106     // Then to extract the public key from the private key, use the following command:
  107     // openssl rsa -in private.pem -out public.pem -outform PEM -pubout
  108     
  109     // If you are unfamiliar with the basics of Public-key cryptography, a great tutorial can be found on wikipedia:
  110     // http://en.wikipedia.org/wiki/Public-key_cryptography
  111     
  112     // generate a private key
  113     NSData *privateKeyData = [SSCrypto generateRSAPrivateKeyWithLength:2048];
  114     NSLog(@"privateKeyData: \n%s", [privateKeyData bytes]);
  115     // generate a public key from the private key data
  116     NSData *publicKeyData = [SSCrypto generateRSAPublicKeyFromPrivateKey:privateKeyData];
  117     NSLog(@"publicKeyData: \n%s", [publicKeyData bytes]);
  118 
  119     // At this point you would write the private and public keys to files
  120     // for later use like so:
  121     //
  122     // [privateKeyData writeToFile:@"/some/file/path/private.pem" atomically:YES];
  123     // [publicKeyData writeToFile:@"/some/file/path/public.pem" atomically:YES];
  124 
  125     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  126     
  127     // Test 4: Sign (encrypt), and then verify (decrypt) a string
  128     
  129     // Signing is the same as running the following command in the terminal:
  130     // echo -n "The duck quacks at daybreak" | openssl rsautl -sign -inkey Privatekey.pem | openssl enc -base64
  131     
  132     // Verifying is the same as running the following command in the terminal:
  133     // echo -n "Q102..." | openssl enc -base64 -d | openssl rasutl -verify -inkey PUBKEY.pem -pubin
  134     
  135     crypto = [[SSCrypto alloc] initWithPublicKey:publicKeyData privateKey:privateKeyData];
  136     
  137     NSString *secretPhrase = @"The duck quacks at daybreak";
  138     [crypto setClearTextWithString:secretPhrase];
  139     
  140     NSData *signedTextData = [crypto sign];
  141     NSData *verifiedTextData = [crypto verify];
  142     
  143     NSLog(@"Secret Phrase: %@", secretPhrase);
  144     NSLog(@"Signed (Encrypted using private key): %@", [signedTextData encodeBase64]);
  145     NSLog(@"Verified (Decrypted using public key): %s", [verifiedTextData bytes]);
  146     
  147     // Note: we could also have output the verifiedTextData (clearText) by doing the following:
  148     // NSLog(@"Now Verified: %@", [crypto clearTextAsString]);
  149     
  150     NSLog(@" ");
  151     NSLog(@" ");
  152     NSLog(@" ");
  153     
  154     [crypto release];
  155     
  156     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157     
  158     // Test 5: Encrypt, and then decrypt a string
  159     
  160     // Encrypting is the same as running the following command in the terminal:
  161     // echo -n "Billy likes Mandy" | openssl rsautl -encrypt -inkey PUBKEY.pem -pubin | openssl enc -base64
  162     // 
  163     // Note: you'll get a different encryption everytime, so don't expect them to be the same...
  164     
  165     // Decrypting is the same as running the following command in the terminal:
  166     // echo -n "SLSbd6..."| openssl enc -base64 -d | openssl rsautl -decrypt -inkey Privatekey.pem
  167     
  168     crypto = [[SSCrypto alloc] initWithPublicKey:publicKeyData privateKey:privateKeyData];
  169     
  170     NSString *topSecret = @"Billy likes Mandy";
  171     [crypto setClearTextWithString:topSecret];
  172     
  173     NSData *encryptedTextData = [crypto encrypt];
  174     NSData *decryptedTextData = [crypto decrypt];
  175 
  176     NSLog(@"Top Secret: %@", topSecret);
  177     NSLog(@"Encrypted: %@", [encryptedTextData encodeBase64]);
  178     NSLog(@"Decrypted: %s", [decryptedTextData bytes]);
  179     
  180     // Note: we could also have output the decryptedTextData (clearText) by doing the following:
  181     // NSLog(@"Now Decrypted: %@", [crypto clearTextAsString]);
  182     
  183     NSLog(@" ");
  184     NSLog(@" ");
  185     NSLog(@" ");
  186 
  187     [crypto release];
  188     
  189     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  190     
  191     [pool release];
  192     return 0;
  193 }
  194