简体   繁体   中英

Data encoding in Objective-C and Ruby

I'm working on an iOS app using Parse, and I need to simulate their encryption flow using Ruby for the website and browser extensions I'm building for it.

For generating the salt for AES , they have the following code in the iOS app:

NSString *str            =    user.email;
NSData *strData          =    [str dataUsingEncoding: NSUTF8StringEncoding];
NSData *encryptedData    =    [strData AESEncryptWithPassphrase:str]; // using the same key

What's puzzling me is dataUsingEncoding: NSUTF8StringEncoding -- it's obviously encoding the string in UTF-8, but when I see it in NSLog it seems quite different. So if str was "randomemail@gmail.com", doing NSLog on strData outputs:

<72616e64 6f6d656d 61696c40 676d6169 6c2e636f 6d>

Now, how do I get that same output in Ruby? I can't get the right salt because of this ("randomemail@gmail.com".encode("UTF-8") simply returns the email as expected). How can I simulate dataUsingEncoding in Ruby to get the same exact salt, is there something I'm missing?

Thanks

Try this:

"randomemail@gmail.com".encode("UTF-8").bytes.to_a.map{ |x| x.to_s(16)}

and you will "see" what you want.

The hexadecimal representation of "randomemail@gmail.com" with UTF-8 encoding is

<72616e64 6f6d656d 61696c40 676d6169 6c2e636f 6d>

But ruby shows you the string representation, which is "randomemail@gmail.com". They are showing the same stuff in different ways.

Check here for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM