Skip to content

Instantly share code, notes, and snippets.

@noahmiller
Created July 25, 2012 02:56
Show Gist options
  • Save noahmiller/3174119 to your computer and use it in GitHub Desktop.
Save noahmiller/3174119 to your computer and use it in GitHub Desktop.
EncryptionTransformer class
@interface EncryptionTransformer : NSValueTransformer
{}
/**
* Returns the key used for encrypting / decrypting values during transformation.
*/
- (NSString*)key;
@end
@implementation EncryptionTransformer
+ (Class)transformedValueClass
{
return [NSData class];
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (NSString*)key
{
// Your version of this class might get this key from the app delegate or elsewhere.
return @"secret key";
}
- (id)transformedValue:(NSData*)data
{
// If there's no key (e.g. during a data migration), don't try to transform the data
if (nil == [self key])
{
return data;
}
if (nil == data)
{
return nil;
}
return [data dataAES256EncryptedWithKey:[self key]];
}
- (id)reverseTransformedValue:(NSData*)data
{
// If there's no key (e.g. during a data migration), don't try to transform the data
if (nil == [self key])
{
return data;
}
if (nil == data)
{
return nil;
}
return [data dataAES256DecryptedWithKey:[self key]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment