Earth
Earth

Reputation: 11

implementing password hashing in iOS applications

I am trying to implement password hashing in my iPad application.I found secure salted password hashing link which is having sample codes of java, .net ,etc but not ios. Can anybody help me to found a solution in the same way how they are doing...

Upvotes: 1

Views: 230

Answers (2)

Sandy Chapman
Sandy Chapman

Reputation: 11341

Note that MD5 is insecure and shouldn't be used for hashing sensitive information such as passwords. You should instead use SHA1 or SHA256 like this:

+ (NSData *)sha256:(NSData *)data {
    unsigned char hash[CC_SHA256_DIGEST_LENGTH];
    if(CC_SHA256([data bytes], [data length], hash) ) {
        NSData *sha256 = [NSData dataWithBytes:hash length: CC_SHA256_DIGEST_LENGTH];        
        return sha256;
    }
    return nil;
}

Upvotes: 1

Ashish P.
Ashish P.

Reputation: 858

Use common crypto library for making md5 of your password.

#import <CommonCrypto/CommonDigest.h>

and then use.

- (NSString *) md5:(NSString *) input
{
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];

    return  output; 
}

Upvotes: 1

Related Questions