I am using XMPP and XMPP will give me a photo data like the following:
a3f549fa9705e7ead2905de0b6a804227ecdd404
So I assume the above data is the photo data and I assume that it's Hex data. (maybe I am wrong)
So I use the following to create the UIImage, but it doesn't work
anyone know how to do it?
What I am trying to do is to change command from Hex String into NSData.
NSString* command = #"a3f549fa9705e7ead2905de0b6a804227ecdd404";
command = [command stringByReplacingOccurrencesOfString:#" " withString:#""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [command length]/2; i++) {
byte_chars[0] = [command characterAtIndex:i*2];
byte_chars[1] = [command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
UIImage *image = [UIImage imageWithData: commandToSend];
The string is 40 characters long and surely looks hex-encoded. This makes 160 bits of information. And this 160 reminds me of SHA-1, in accordance with this document:
http://xmpp.org/extensions/xep-0153.html
So what you have here is the checksum of an image, not the image itself. You need to read the document to find out how to get the whole image.
Related
I have an hex string coming from server, I have to convert that hex string to NSString. I used some of the methods found in this post How to convert an NSString to hex values
I almost got the same text except that Ú appear randomly in some place.
I've change Ú into bold text to be easy to find in the following example.
Can you guys help me what's wrong with the algorithm.
My algorithm
- (NSString *) stringFromHex:(NSString *)str
{
NSMutableData *stringData = [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [str length] / 2; i++) {
byte_chars[0] = [str characterAtIndex:i*2];
byte_chars[1] = [str characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[stringData appendBytes:&whole_byte length:1];
}
return [[NSString alloc] initWithData:stringData encoding:NSASCIIStringEncoding];
}
Hex string from server
7539686132564968635943666c6348532f564a656261736b526a47585a75734a625a61316c5753475a645575625a5253386f473574347678694c4930695648704c52473170547a4f6b356552da554675394e58672b6943743857524637557748755961634435306936437a7a535a726d6e53664e6e523862433232594a4f593638655464314c6747324763544f56357a736f544c49734e734ada672f77472f6b6b4b48446c644659426d51334d3dda
Normal string from server which is RSA encoded string
u9ha2VIhcYCflcHS/VJebaskRjGXZusJbZa1lWSGZdUubZRS8oG5t4vxiLI0iVHpLRG1pTzOk5eR
UFu9NXg+iCt8WRF7UwHuYacD50i6CzzSZrmnSfNnR8bC22YJOY68eTd1LgG2GcTOV5zsoTLIsNsJ
g/wG/kkKHDldFYBmQ3M=
Normal string converted using my above algorithm
u9ha2VIhcYCflcHS/VJebaskRjGXZusJbZa1lWSGZdUubZRS8oG5t4vxiLI0iVHpLRG1pTzOk5eRÚUFu9NXg+iCt8WRF7UwHuYacD50i6CzzSZrmnSfNnR8bC22YJOY68eTd1LgG2GcTOV5zsoTLIsNsJÚg/wG/kkKHDldFYBmQ3M=Ú
The server seems to convert DA to a space, probably because DA lies outside the ASCII range, which only runs up to 7A due to it using only 7 bits.
The algorithm converts DA to Ú, which seems to be ok when using Latin-1 encoding. Using a different encoding from the list here, such as NSUTF8StringEncoding, might help.
For Encryption using following function:
https://gist.github.com/Veelian/7541502
Whenever I read file in binary and apply encryption on it,
I get some encrypted text of that file but whenever I execute
same above operation again then i will get different encrypted text
File is same contents of file also same, still getting different encrypted text
if execute same operation again.
Please find the below code
NSString *filePath = #"/Users/xyx/Desktop/NOTICE.txt";
const char *cfilePath = [filePath cStringUsingEncoding:NSASCIIStringEncoding];
FILE *fileStream= fopen (cfilePath,"rb");
NSDictionary *properties = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSNumber *fileSize = [properties objectForKey: NSFileSize];
long l_fileSize = [fileSize longValue];
temp = (char*) malloc (sizeof(char)*l_fileSize);
fgetpos(fileStream,&pos);
int bytesToRead = (int)l_fileSize;
size_t byt=fread(temp,1,bytesToRead,fileStream);
buffer = [NSData dataWithBytes:(const void *)temp length:sizeof(char)*byt];
NSLog(#"Buffer : %#",buffer);
BytesRead = buffer.length;
NSData *encryptedBytes=[buffer AES256EncryptWithKey:[Security key]]; //taking key from other class
size_t length = [encryptedBytes length] + 1;
unsigned char aBuffer[length];
[encryptedBytes getBytes:aBuffer length:length];
NSString *encBytes = [NSString stringWithFormat:#"%s", aBuffer];
NSLog(#"ENcrypted Bytes : %#",encBytes);
I have 1 Hex file, i want to read this file and parse it to NSString.
I used this code to read hex file but it only prinf hex code in console:
-(void)readHexfile
{
NSData *data = [NSData dataWithContentsOfFile:#"path file"];
NSLog(#"Patch File: %#",data);
}
Do you have any suggestions? Thanks in advance
Use stringWithContentsOfFile:encoding:error: instead of dataWithContentsOfFile to read it as NSString.
There is no such a thing like a "hex file". Hex, or hexadecimal, is a numerical system that is quite suitable to display binary data in octets (8-bit bytes) in some way suitable for humans.
What you currently do is displaying the description of the NSData object onth the console in hex.
Some quick and dirty hack could be just to use the description of the NSData.
NSString *hexString = [data description];
This will create some overhead that you could strip of using string manipulation methods.
There are smater ways that may require more work.
On the contrary, if you are not interested in a hex representation then use stringWithContentsOfFile to read the file directly into an NSString object. You can then apply various encodings depending on how your file is actually encoded.
You'd read that using a NSScanner (convert your data to a string first using [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] (assuming your text is pure ASCII or UTF-8) or read it directly using +[NSString stringWithContentsOfFile:encoding:error:]). See also the String Programming Guide on how to use scanners.
Edit: So it seems you want to read a file with null-terminated strings. A naive and inefficient way to do that would be:
NSData *data = [NSData dataWithContentsOfFile:#"file.path"];
NSMutableArray *strings = [NSMutableArray array];
const char *rawData = [data bytes];
NSUInteger dataLength = [data length];
NSMutableData *currentString = [NSMutableData data];
for (NSUInteger i = 0; i < dataLength; i++) {
if (rawData[i] == 0) {
if ([currentString length] > 0) {
[strings addObject:[[[NSString alloc] initWithData:currentString encoding:NSUTF8StringEncoding] autorelease]];
}
[currentString release];
currentString = [NSMutableData data];
} else {
[currentString appendBytes:&rawData[i] length:1];
}
}
// Handle the last string if it wasn't null-terminated.
if ([currentString length] > 0) {
[strings addObject:[[[NSString alloc] initWithData:currentString encoding:NSUTF8StringEncoding] autorelease]];
}
// "strings" now is a list of strings.
I am using xcode and this is my sha512 method:
-(NSString*) sha512:(NSString*)input
{
const char *cstr = [input cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:input.length];
uint8_t digest[CC_SHA512_DIGEST_LENGTH];
CC_SHA512(data.bytes, data.length, digest);
NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2];
for (int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) {
[output appendFormat:#"%02x", digest[i]];
}
return output;
}
When I try to pass the input "test", it returns:
"ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"
which matches other sha512 hash tools (including my Java program and "http://hash.online-convert.com/sha512-generator").
However, when I input non-ascii char like "é", it returns something different than all my other sha512 tools.
For input "é", my method returns:
"60313f8521d3016916d876f7ad11cf42a30dfd4ff9bc557f1e2f90e0d37c56b76ab5e42c8a16db20c18086b0d769c08542429c262cc21ee4fba02bfc689a4797"
when other tools (again including my Java program and "http://hash.online-convert.com/sha512-generator") return "9e2ad28633f24451bd4f3c1cb20586a21a44c3aeedbdc01b9cc8fa72917ea7bd689c82b8bf1fef89b911cf8cc46fa2c1ccc10087b2094fd4d3350ecd88526a2c".
Did I miss anything? Any ideas about this?
Thanks!
Create your NSData object like this:
NSData *data = [input dataUsingEncoding:NSUTF8StringEncoding];
Just double checked and it works correctly like that.
I am fetching result by fire a transaction but by the transaction one result is coming as blob attributes, that is image, I want to change that blob attribute to image
I wrote code for that "icon" is the key for fetch the image from transaction,
so please help me check this,
image is printing nil,
why?
NSString *inputString = [[[self formModel] attributeAsString:#"icon"] description];
NSLog(#"icon is %#",[[self formModel] attributeAsString:#"icon"]);
NSLog(#"inputstring is %#",inputString);
//NSImage *image = [NSUnarchiver unarchiveObjectWithData:[[self formModel] attributeAsString:#"icon"]];
//NSLog(#"image is %#",image);
NSArray *words = [inputString componentsSeparatedByString:#" "];
NSLog(#"words is %#",words);
NSArray *sizes = [words valueForKey:#"length"];
int sizeOfBytes = 0;
for (NSNumber *size in sizes) {
sizeOfBytes += [size intValue]/2;
}
int bytes[sizeOfBytes];
int counts = 0;
for (NSString *word in words) {
// convert each word from string to int
NSMutableString *ostr = [NSMutableString stringWithCapacity:[word length]];
while ([word length] > 0) {
[ostr appendFormat:#"%#", [word substringFromIndex:[word length] - 2]];
word = [word substringToIndex:[word length] - 2];
}
NSScanner *scaner = [NSScanner scannerWithString:ostr];
unsigned int val;
[scaner scanHexInt:&val];
bytes[counts] = val;
counts++;
}
// get NSData form c array
NSData *data = [NSData dataWithBytes:bytes length:sizeOfBytes];
NSLog(#"My NSDATA %#",data);
NSImage *Image = [[NSImage alloc] initWithData:data];
Never use the output of description to do processing. There is no guarantee of its format. What format is your original "blob" in and how was it generated? Your code suggests it might be an NSData or it might be an NSKeyArchiver. Both of these easily convert to NSData. You never need to do this by hand by converting to a string.