sha-512 not working properly on IOS Simulator - ios

I am implementing mobile app sha-512 function that will run on IOS, this function is working fine on all browsers including Safari.
I am testing my application using IOS Simulator.
My application is calling sha-512 function three times from one page/click. The problem is that, at first call sha-512 function produces right result, but at second and third call it produces wrong result.
thanks in advance

This is my code
//Creating Hash Value
NSString *hashkey = [NSString stringWithFormat:#"data"];
// PHP uses ASCII encoding, not UTF
NSLog(#"hashkey : %#",hashkey);
const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination SHA512_Final
uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA512(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_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:#""];
Hope it helps you..

Related

Get unicode chars from webservice and display them in ios app

I need some help:
i get from WebService only a part from the uunicode value, and after this I append the prefix \u to finish the value. The .ttf is good, i tested with some hardcoded values.
NSString *cuvant = [[self.catData objectAtIndex:indexPath.row]objectAtIndex:9]; //Get data
//apend prefix (double \ to escape the \u command)
cuvant = [NSString stringWithFormat:#"\\u%#",cuvant];
// cell.catChar.text = [NSString stringWithUTF8String:"\ue674"]; --->this works very well
cell.catChar.text = [NSString stringWithUTF8String:[cuvant UTF8String]]; //---> this doesn't work
i searched the documentation, and other sites but i didn't found nothing usefull, all the hints are with hardcoded data... i need tot take the codes dinamically
Thanks!
All you need is just to feed this unicoded string as data first. So make a C-String then
NSData *dataFromUnicodedString = [NSData dataWithBytes:yourCUnicodedString length:strlen(yourCUnicodedString)];
and afterwards the resulting string will be
NSString *unicodedString = [[NSString alloc] initWithData:dataFromUnicodedString encoding:NSUTF8StringEncoding];

how do i create a sha256 hash of a file(jpeg,pdf and tiff) using objective-c

i used the following code that i took from here.
- (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;
}
NSData *imageHash=[self sha256:imageData];
imageHashtag = [imageHash base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
I am having some problems with this though- the hash being created includes / and = that are not supposed to be there.
Can someone help me figure out the mistake i am making here? and how can i solve it?
edit: I found the possible problem- i was converting the sha hash that was an base64 encoded data back into another base64string.
but when i used the following code to convert the hashdata into string, i only get nil.
so can someone help me with converting the sha hash into a string?
imageHashtag = [NSString stringWithUTF8String:[imageHash bytes]];
the imagehash does have 32 bits of data, but imagehashtag is nil.
we added the following code to solve the problem:
NSString *hash=[sha256 description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
return hash;
to solve our problem.

How to convert Base64 encoded NSString into Byte Array(Java) in ios?

I have an Image that is converted into NSData and which in-turn is converted into base64 encoded NSString. Now i have a service that accepts this base64 encoded string in only Byte Array(Java Supported). I tried different options but i am not able to convert the encoded string into Byte Array type. Can someone please help me on how to convert the encoded string into Byte Array(Java supported)?
Below is my answer for this. I came to know that the easiest way is to convert the image data into byte array directly without base64encoding the data.
NSError* error;
NSString* str=[[NSBundle mainBundle] pathForResource:#"black-circle" ofType:#"png"];
NSData* data= [NSData dataWithContentsOfFile:str options:NSDataReadingUncached error:&error];
NSInteger length=[data length];
const char* dataBytes=[data bytes];
Now you can traverse each byte using dataBytes pointer.
Hope this helps.
You can try out :
Use-[NSString UTF8String] which returns const char*.
or this link will help you :
Convert NSString into byte array iphone
We can get the NSdata from the NSStirng and then convert NSData into Byte Array. The easier way is to convert the captured image data into Byte Array using uint8_t. Below is the code snippet that does the trick.
NSData to Byte Array:-
NSData *data = UIImagePNGRepresentation(imageCaptured);
uint8_t byteArray[[data length]];
[data getBytes:&byteArray];
NSMutableString *byteArrayString = [[NSMutableString alloc]init];
for (int i = 0 ; i < sizeof(byteArray); i++) {
[byteArrayString appendString:[NSString stringWithFormat:#"%d,",byteArray[i]]];
}
// Removing last extra comma
[byteArrayString deleteCharactersInRange:NSMakeRange([byteArrayString length]-1, 1)];

How to encode the string in to "windows-1252" using ios?

The following string is working perfectly in android,please give me suggestion for encoding this in ios.
Android Example:String s = "hhh";
s.getBytes("Windows-1252");
A quick look at the docs for NSString would give you:
NSString *s = #"hhh";
NSData *data = [s dataUsingEncoding:NSWindowsCP1252StringEncoding];
uint_8 *bytes = [data bytes];
The equivalent code in iOS looks like this
NSString *str = #"hhh";
char buffer[100];
[str getCString:buffer maxLength:100 encoding:NSWindowsCP1252StringEncoding];

Difficulty dealing with encoding in SQLite

I'm trying to get information from a SQLite file, and I when I run a query, the information is returned with ASCII encoding. I'm using the code below to put the returned information into a string.
[NSString stringWithCString:(char *)sqlite3_column_text(compiledStatement, 2) encoding:NSASCIIStringEncoding];
When I try to use UTF8 encoding to put the returned information into a string, it doesn't work. The code below is used in the app currently on the store.
[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
The string is null. How can I convert the SQLite file so that it is encoded with UTF8? The current version of the app on the store uses the latter code above, so I need to figure out how to convert the file, instead of changing the code in the app.
You can use const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); with stringWithCharacters:length: (UTF16)
const unichar *text = sqlite3_column_text16(_sqlite_stmt, columnIndex);
if (text) {
NSUInteger length = sqlite3_column_bytes16(_sqlite_stmt, columnIndex)/sizeof(unichar);
article = [NSString stringWithCharacters:text length:length];
}
Try this :
char *title = (char *)sqlite3_column_text(compiledStatement, 2);
NSString *str = [NSString stringWithUTF8String:title];
Working fine for me !

Resources