Created
July 25, 2012 02:56
-
-
Save noahmiller/3174119 to your computer and use it in GitHub Desktop.
EncryptionTransformer class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface EncryptionTransformer : NSValueTransformer | |
{} | |
/** | |
* Returns the key used for encrypting / decrypting values during transformation. | |
*/ | |
- (NSString*)key; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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