Question

Im using the following code as an example. Help me out if I'm wrong please.

- (void)storeToKeychain {

kPassword = [self computeSHA256DigestForString:[NSString stringWithFormat:@"%@%i%@", [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], pinHash, SALT_HASH];


NSData *data = [@"Data" dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSData *encryptedData = [RNEncryptor encryptData:data
                                withSettings:kRNCryptorAES256Settings
                                      password:kPassword
                                         error:&error];
}

- (NSString*)computeSHA256DigestForString:(NSString*)input {

const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA256_DIGEST_LENGTH];

// This is an iOS5-specific method.
// It takes in the data, how much data, and then output format, which in this case is an int array.
CC_SHA256(data.bytes, data.length, digest);

// Setup our Objective-C output.
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];

// Parse through the CC_SHA256 results (stored inside of digest[]).
for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
    [output appendFormat:@"%02x", digest[i]];
}

return output;
}

My app has a setup and login page. Whenever the user creates an account, I want the password to be stored in the keychain encrypted, I should probably do the username to for added security. So I want to use RNCryptor to encrypt the user's username and password in the keychain when the user creates an account.

Im not sure what password to use for kPassword in the RNEcryptor method. I was originally thinking of using an SHA256 hash made up of the UUID, username and a random 40 character salt hash to encrypt the password into the keychain. I later found RNCryptor and I was wondering if could instead use the SHA256 hash as kPassword or is there any other alternative?

No correct solution

OTHER TIPS

Try this

Add AESCrypt-ObjC-master from https://github.com/Gurpartap/AESCrypt-ObjC

now add these to the top

#import "AESCrypt.h"

then add these

NSString *message = @"yourStringtobeencrypted";
NSString *password = @"yourpassword";
NSString *encryptedData = [AESCrypt encrypt:message password:password];

And there you go.. for decryption

NSString *message1 = [AESCrypt decrypt:encryptedData password:password];

:-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top