Base64 vs NSPropertyListSerialization - ios

I need to encode my image into text.
And I found this class for that:
Base64 for iOS with ARC
When I try to encode my image I see that NSPropertyListSerialization creates absolutely the same string as base64 does. Is it the right way to create base64 String with NSPropertyListSerialization or am I missing something?
Base64:
[data base64EncodedString];
NSPropertyListSerialization:
[NSString stringWithUTF8String:[[NSPropertyListSerialization dataWithPropertyList:data format:NSPropertyListXMLFormat_v1_0 options:0 error:nil] bytes]]

No you're not missing anything. Base64 is simply a standard from encoding binary data in ASCII and pLists use Base64 encoding for encoding binary data like images (using NSPropertyListSerialization) so they should create identical Base64 strings for the same binary data.
If you're wondering about which to use in your application I'd recommend you use the base64 library. While Apple has pushed to make pLists a standard and pLists will probably always encode binary data as Base64 in future, in the extremely unlikely event they change something or drop support for pLists your code will break. Besides it's best to be clear in your code (for yourself and others) that you're encode your data to base 64.

Related

charachter encoding in PHP Extension

I'm currently writing a PHP extension in C++ with the Zend API. Basically I make PHP_METHOD{..} wrappers around my native C++ interface methods and using "zend_parse_parameters(..)" to fetch the corresponding input arguments.
This extension contains methods which can take strings as arguments, such as a filename.
I know from http://php.net/manual/en/language.types.string.php#language.types.string.details that strings have no encoding in PHP, but still can I expect from the PHP programmer that he will use a function like "utf8_decode(..)" such that the input strings can be read by the extension correctly?
Or does the PHP Programmer expect that the extension detects the encoding from the php-script and handles strings accordingly?
Every help is highly appreciated! Thanks!
You are correct. Strings are just binary blobs in PHP. As the author of an extension. Your options:
Have the user hand your extension UTF-8: By far the best option. The user has to make the decision. Assert that the string is UTF-8 encodable and fail early.
Encode yourself: You cannot know the meaning of the string. As PHP strings are just binary blobs and have no encoding information you do not know what the intended string content is. It might as well just come from a Windows file with weird encoding and was concatenated with a complete different encoding. Worse, it might be UTF-8 encodable, but actually not UTF-8, in which way you interpret it wrongly, without the user knowing. Hence, solution 1, have the user pass UTF-8.
Alternative: Force the user to pass an input encoding.
Here is an example of the alterantive 3:
$obj = MyExtensionClass('UTF-8'); // force encoding
$obj->someMethod($inputStr); // try to convert now
The standard library uses approach 1. See json_encode as an example:

How to send parameters with base64 image string using POST method in iOS?

I have parameters FirstName,LastName,Email & image ,Which need to send in the form of base64.I need appropriate code for it.
Base64 is used to convert binary data into text.
In your situation only Image needs to be encoded in that format, the rest should be text.
Convert UIImage to Base64 string in Objective-C and Swift

Send NSData to WCF

I have a WCF service, currently expecting byte[] as input parameter, but I want to send NSData from iOS. These types seem incompatible.
I've read a lot about this and most suggest that I convert the NSData to a base64 string.
Will this work for obscured data? I.e. my NSData consists of NSData that was passed through an AES256 Encryption algorithm. I don't think this data can be converted back to string successfully.
Thank you Tom
I think sending the raw bytes would most probably have worked, but in the end I opted to convert the NSData to a base64 NSString and send that instead.
Works great!
PS> Could someone please give me some rep? I'd like to participate more, but no one is voting on anything I post.
Here is my profile with my 2 or 3 other questions: https://stackoverflow.com/users/1014983/matthys-du-toit

NSDictionary writeToFile encoding only in UTF-8?

By putting a NSDictionary to a file I get an UTF-8 encoded XML file. I need to write data to a file in NSISOLatin1StringEncoding. Is NSDictionary UTF-8 only? How to achieve my goal?
Are you sure you need a file encoded as ISO Latin-1? The problem with all encodings other than some form of Unicode is that they can't represent all possible characters.
The encoding is surely the least of your problems. A dictionary's file representation is a property list file. It's unlikely that any code which requires Latin-1 encoding would understand that format. Indeed, the format is not guaranteed. It's not even guaranteed to be XML or textual. Property lists may be binary.
If you want to exchange data with a program that's going to use anything other than Cocoa's property list implementation, you should manually write the contents of the dictionary out in a format that's defined independently of Apple's property list format.
And, yes, if Cocoa does write the property list as XML, it's going to be UTF-8-encoded.

SHA256 implementation using Base64 for input and output

I've been asked to develop the company's backoffice for the iPad and, while developing the login screen, I've ran into an issue with the authentication process.
The passwords are concatenated with a salt, hashed using SHA-256 and stored in the database.
The backoffice is Flash-based and uses the as3crypto library to hash then password+salt and my problem is that the current implementation uses Base64 for both input and output.
This site demonstrates how this can be done: just select Hash and select Base64 for both input and output format and fire away. So far, all my attempts have yielded different results from the ones this site (and the backoffice code) give me.
While I think that in theory it should be relatively simply:
Base64 encode the pass+salt
Hash it using SHA-256
Base64 encode the result again
so far I haven't been able to do this and I'm getting quite the headache to be honest.
My code is becoming a living maze, i'll have to redo-it tomorrow I reckon.
Any ideas?
Cheers and thanks in advance
PS: Here's the Backoffice's Flash code for generating hashed passwords by the way:
var currentResult:ByteArray;
var hash:IHash = Crypto.getHash('sha256');
var data:ByteArray = Base64.decodeToByteArray(str + vatel);
currentResult = hash.hash(data);
return Base64.encodeByteArray(currentResult).toString();
The backoffice code does not do
Base64 encode the pass+salt
Hash it using SHA-256
Base64 encode the result again
(as you wrote above)
Instead, what it does is
Base64 decode the pass+salt string into a byte array
Hash the byte array using SHA-256
Base64 encode the byte array, returning a string
As per step 1 above, it's a unclear what kind of character encoding the input strings uses. You need to make sure that both systems use the same encoding for the input strings! UTF8, UTF16-LE or UTF16-BE makes a world of a difference in this case!
Start by finding out the correct character encoding to use on the iOS side.
Oh, and Matt Gallagher has written an easy to use wrapper class for hashes to use on iOS, HashValue.m, I've used it with good results.

Resources