简体   繁体   English

Objective-C和Ruby中的数据编码

[英]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. 我正在使用Parse开发iOS应用程序,我需要使用Ruby为其构建的网站和浏览器扩展程序模拟其加密流程。

For generating the salt for AES , they have the following code in the iOS app: 为了为AES生成盐,它们在iOS应用中具有以下代码:

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. 令我感到困惑的是dataUsingEncoding: NSUTF8StringEncoding显然是在UTF-8中对字符串进行编码,但是当我在NSLog中看到它时,它似乎完全不同。 So if str was "randomemail@gmail.com", doing NSLog on strData outputs: 因此,如果str是“ randomemail@gmail.com”,则对strData输出执行NSLog

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

Now, how do I get that same output in Ruby? 现在,如何在Ruby中获得相同的输出? I can't get the right salt because of this ("randomemail@gmail.com".encode("UTF-8") simply returns the email as expected). 因此,我收不到合适的盐(“ randomemail@gmail.com” .encode(“ UTF-8”)只会按预期返回电子邮件)。 How can I simulate dataUsingEncoding in Ruby to get the same exact salt, is there something I'm missing? 如何在Ruby中模拟dataUsingEncoding以获得完全相同的盐,我是否缺少某些东西?

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 使用UTF-8编码的“ randomemail@gmail.com”的十六进制表示为

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

But ruby shows you the string representation, which is "randomemail@gmail.com". 但是ruby向您显示字符串表示形式,即“ randomemail@gmail.com”。 They are showing the same stuff in different ways. 他们以不同的方式展示相同的东西。

Check here for more information. 在此处查看更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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