Skip to content

Instantly share code, notes, and snippets.

@st3fan
Created February 10, 2010 04:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st3fan/300018 to your computer and use it in GitHub Desktop.
Save st3fan/300018 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <mach/mach_time.h>
#include <stdint.h>
#include <CommonCrypto/CommonCryptor.h>
struct Timer
{
public:
void start()
{
elapsed_ = 0;
start_ = mach_absolute_time();
}
void stop()
{
end_ = mach_absolute_time();
}
double elapsed()
{
double elapsed = -1;
uint64_t difference = end_ - start_;
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) == 0) {
elapsed = (double) difference * (double) info.numer / (double)info.denom;
}
return elapsed;
}
private:
uint64_t start_;
uint64_t end_;
double elapsed_;
};
const size_t kDataLength = (4 * 1024 * 1024);
int test()
{
char* data = new char[kDataLength];
if (data != NULL)
{
char key[kCCKeySizeAES128] = {
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef,
0xde, 0xad, 0xbe, 0xef
};
size_t dataOutMoved;
Timer encryptionTimer;
encryptionTimer.start();
CCCryptorStatus status = CCCrypt(
kCCEncrypt,
kCCAlgorithmAES128,
0,
key,
kCCKeySizeAES128,
NULL,
data,
kDataLength,
data,
kDataLength,
&dataOutMoved
);
encryptionTimer.stop();
if (status != kCCSuccess) {
std::cerr << "Encryption failed with error " << status << std::endl;
return 1;
}
NSLog(@"Encryption took %f seconds (%.0f MB/second)",
encryptionTimer.elapsed() / 1000000000.0, (4.0 / encryptionTimer.elapsed() * 1000000000.0));
// Decrypt
Timer decryptionTimer;
decryptionTimer.start();
status = CCCrypt(
kCCDecrypt,
kCCAlgorithmAES128,
0,
key,
kCCKeySizeAES128,
NULL,
data,
kDataLength,
data,
kDataLength,
&dataOutMoved
);
decryptionTimer.stop();
if (status != kCCSuccess) {
std::cerr << "Encryption failed with error " << status << std::endl;
return 1;
}
NSLog(@"Decryption took %f seconds (%.0f MB/second)",
decryptionTimer.elapsed() / 1000000000, (4.0 / decryptionTimer.elapsed() * 1000000000));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment