I had created a SHA256 encoding of the string using the following function,
const char *s=[#"123456" cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
CC_SHA256(keyData.bytes, keyData.length, digest);
NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
NSString *hash=[out description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
NSLog(#"Hash : %#", hash);
It gives me the output : 8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92.
But I need the following output : jZae727K08KaOmKSgOaGzww/XVqGr/PKEgIMkjrcbJI=. It's base64.
How Can I convert the "hex" hash I generated to "base64"?
I had use this website to generate base64 hash : http://www.online-convert.com/result/7bd4c809756b3c16cf9d1939b1e57584
You should not be converting the NSString *hash that you generated from the description to base-64. It is a hex string, not the actual data bytes.
You should go straight from NSData *out to base-64 string, using any of the available base-64 encoders. For example, you can download an implementation from this post, and use it as follows:
const char *s=[#"123456" cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData=[NSData dataWithBytes:s length:strlen(s)];
uint8_t digest[CC_SHA256_DIGEST_LENGTH]={0};
CC_SHA256(keyData.bytes, keyData.length, digest);
NSData *out=[NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
// The method below is added in the NSData+Base64 category from the download
NSString *base64 =[out base64EncodedString];
I use this, (mentionned in How do I do base64 encoding on iphone-sdk?)
http://www.imthi.com/blog/programming/iphone-sdk-base64-encode-decode.php
Works well, and easily usable with ARC.
How do you convert an unsigned char array to an NSData in objective c?
This is what I am trying to do, but it doesn't work.
Buffer is my unsigned char array.
NSData *data = [NSData dataWithBytes:message length:length];
You can just use this NSData class method
+ (id)dataWithBytes:(const void *)bytes length:(NSUInteger)length
Something like
NSUInteger size = // some size
unsigned char array[size];
NSData* data = [NSData dataWithBytes:(const void *)array length:sizeof(unsigned char)*size];
You can then get the array back like this (if you know that it is the right data type)
NSUInteger size = [data length] / sizeof(unsigned char);
unsigned char* array = (unsigned char*) [data bytes];
I have a problem related to AES Encryption. The problem is I need to encrypt the string using AES encryption technique with Intialization Vector, Salt, RFC2898 iteration and Generate a key using sha1 algorithm.
I used this code
+(NSString *)stringToSha1:(NSString *)str{
const char *s = [str cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA1(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
NSLog(#"Hash is %# for string %#", hash, str);
return hash;
}
For sha1 key generation but it produces totally different as this technique do in .net and Android.
Android and .net already have classes and library to do this and i left alone so how I can do it in iPhone.
This should be what you need
+ (NSData *)sha1HashFromString:(NSString *)stringToHash {
NSData *stringData = [stringToHash dataUsingEncoding:NSASCIIStringEncoding];
uint8_t digest[CC_SHA1_DIGEST_LENGTH] = {0};
CC_SHA1([stringData bytes], [stringData length], digest);
NSData *hashedData = [NSData dataWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
return [hashedData autorelease];
}
How to create a SHA256 of a String in iphone/objective c...
Sha256 in Objective-C for iPhone
I have read this..but i am not able to understand this..
I want to create output similar to php funcation as follows:-
$hash = hash_hmac("sha256", implode(';', $hash_parameters), $api_key);
where hash parameters is the array of arguments...
Can you write this as a method which will take the input string...?
And what will be the output of method NSData or NSString..??
I have to create a request with this..??
So in the request object..
[theRequest setHTTPBody:requestBody];
what should be the type of requestBody??
I'm not sure I fully understand your questions but if you're looking to create a hashed string you CAN pass in your parameters as arguments to a hash function.
-(void)generateHashedString {
NSString *key = #"Some random string";
//enter your objects you want to encode in the data object
NSString *data = [NSString stringWithFormat:#"%#%#%#", #"sha256", hash_parameters, api_key];
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
length:sizeof(cHMAC)];
NSString *hash = [HMAC base64Encoding];
}
This will give you an NSString of hash that you can use to make your requests. NSLog(#"%#",hash); To see what you generated!
Make sure you #import <CommonCrypto/CommonHMAC.h> too
I didn't compare the following code to the PHP function output but it works for me:
+(NSString *)signWithKey:(NSString *)key usingData:(NSString *)data
{
const char *cKey = [key cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [data cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
return [[HMAC.description stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"<>"]] stringByReplacingOccurrencesOfString:#" " withString:#""];
}
Let me know if it was helpful...
I spend a hole day, trying to convert the generated hash (bytes) into readable data. I used the base64 encoded like the answer above and it didnĀ“t work at all for me (b.t.w. you need and an external .h to be able to use the base64 encoding, which i had).
So what i did was this (which works perfectly without an external .h):
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:cHMAC length:CC_SHA256_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
// hash is now a string with just the 40char hash value in it
NSLog(#"%#",hash);
For a reference this HMac hashing will work on PHP.
- (NSString *)getToken:(NSString *)queryString
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSDate *dateTodayUTC = [formatter dateFromString:dateString];
NSString *nowTimestamp = [NSString stringWithFormat:#"%.f", [dateTodayUTC timeIntervalSince1970]];
NSString *hashCombinations = [[NSString alloc] initWithFormat:#"%#%#%.f", queryString, public_api_key, [dateTodayUTC timeIntervalSince1970]];
const char *privateKey = [private_api_key cStringUsingEncoding:NSUTF8StringEncoding];
const char *requestData = [hashCombinations cStringUsingEncoding:NSUTF8StringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
//HmacSHA256
CCHmac(kCCHmacAlgSHA256, // algorithm
privateKey, strlen(privateKey), // privateKey
requestData, strlen(requestData), // requestData
cHMAC); // length
NSString *hash;
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
for(int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++)
[output appendFormat:#"%02x", cHMAC[i]];
hash = output;
NSString *base64HashString = [self base64String:hash];
self.tokenLabel.text = hash;
NSLog(#"generated hash = %#", hash);
NSLog(#"base64 hash = %#", base64HashString);
NSLog(#"timestamp = %# nsdate utc = %#", nowTimestamp, dateString);
NSLog(#"combinations %#", hashCombinations);
return [base64HashString urlencode];
}
You can use this base64 method.
- (NSString *)base64String:(NSString *)str
{
NSData *theData = [str dataUsingEncoding: NSASCIIStringEncoding];
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}
I think it's more compact solution:
#import <CommonCrypto/CommonCrypto.h>
...
-(NSData*)Sha256WithKey:(NSData*)key andData:(NSData*)data{
NSMutableData* result = [NSMutableData
dataWithCapacity:CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, [key bytes], [key length],
[data bytes], [data length], result.mutableBytes);
return result;
}
....
I want to generate a key using SHA256 with N number of iterations.
They input should be my "password" + "random number"
I have seen the Crypto sample provided by apple but it seems it doesn't provide my requirement(or might be possible I didnt get it properly).
I have gone through below link as well but is doesnt have method to generate a key using SHA256
http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html
Waiting for some hint.
Regards
Try this, it worked for me
1) To get a hash for plane text input
-(NSString*)sha256HashFor:(NSString*)input
{
const char* str = [input UTF8String];
unsigned char result[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(str, strlen(str), result);
NSMutableString *ret = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH*2];
for(int i = 0; i<CC_SHA256_DIGEST_LENGTH; i++)
{
[ret appendFormat:#"%02x",result[i]];
}
return ret;
}
2) To get hash for NSData as input
Note:- I have used NSData category, so the code is as follow
- (NSString *)SHA256_HASH {
if (!self) return nil;
unsigned char hash[CC_SHA256_DIGEST_LENGTH];
if ( CC_SHA256([(NSData*)self bytes], [(NSData*)self length], hash) ) {
NSData *sha2 = [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [sha2 description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
// hash is now a string with just the 40char hash value in it
//NSLog(#"hash = %#",hash);
// Format SHA256 fingerprint like
// 00:00:00:00:00:00:00:00:00
int keyLength=[hash length];
NSString *formattedKey = #"";
for (int i=0; i<keyLength; i+=2) {
NSString *substr=[hash substringWithRange:NSMakeRange(i, 2)];
if (i!=keyLength-2)
substr=[substr stringByAppendingString:#":"];
formattedKey = [formattedKey stringByAppendingString:substr];
}
return formattedKey;
}
return nil;
}