0

I am implementing a ruby hashing function in javascript. I have encountered a line of code that I'm uncertain what exactly it is doing. Admittedly, it is due to mu lack of ruby experience, but I thought I could draw on your knowledge to help. The bit of code is:

someString.each_char { |c| bytes += c + "\x00" }

My question is what is this doing? To me it seems it is encoding a string. I would like to say it is UTF8. But I am uncertain. BTW, the string is an command line argument being passed into the program. Any help is appreciated.

ultratoad
  • 116
  • 2
  • 7
  • It’s adding a NUL character after each character. Maybe trying to convert ASCII to little-endian UTF-16. Where does the hashing function come from? – Ry- Mar 16 '18 at 21:58
  • It appears that `bytes` is initialized before this expression is executed. No? – Cary Swoveland Mar 16 '18 at 21:58
  • I got this part of the code from this answer. We are doing something similar. https://stackoverflow.com/questions/530426/reimplement-asp-net-membership-and-user-password-hashing-in-ruby – ultratoad Mar 16 '18 at 21:59
  • Cary, thats right. it is initialized like this `bytes = ""` – ultratoad Mar 16 '18 at 22:00
  • Ah, the Ruby code isn’t correct, then; you should be basing your implementation off the .NET one. `Encoding.Unicode` is little-endian UTF-16. Is this client-side JavaScript or Node.js? – Ry- Mar 16 '18 at 22:04
  • Javascript. (React) – ultratoad Mar 16 '18 at 22:08

0 Answers0