Reputation: 126
Here is code fragment of c#.
byte[] bytes = Encoding.Unicode.GetBytes('test');
byte[] numArray = new byte[0];
numArray = (byte[])null;
using (MD5 md5 = MD5.Create())
numArray = md5.ComputeHash(bytes);
OutPut:
bytes = [116, 0, 101, 0, 115, 0, 116, 0]
numArray = [200, 5, 158, 46, 199, 65, 159, 89, 14, 121, 215, 241, 183, 116, 191, 230]
Above same thing trying to achieve using Ruby or Ruby on Rails but facing some issues -
ruby code
bytes = "test".bytes.to_a.join(",") + ","
bytes = bytes.gsub(",", "/0/").split("/")
numArray = Digest::MD5.digest(bytes)
OutPut:
bytes = ["116", "0", "101", "0", "115", "0", "116", "0"]
When I am trying to access Digest::MD5.digest it is accepting only string value if I convert it into string I could not get same the result as C# code provided.
Upvotes: 3
Views: 977
Reputation: 27227
This works in Ruby 1.9.3 and higher:
require 'digest/md5'
Digest::MD5.digest( "test".encode( 'UTF-16LE' ) ).bytes.to_a
=> [200, 5, 158, 46, 199, 65, 159, 89, 14, 121, 215, 241, 183, 116, 191, 230]
Some explanation:
gsub
will get you in trouble, use Ruby's String encoding methods.Digest::MD5.digest
will read input strings as if force-encoded to 'ASCII-8BIT' (i.e. it just reads the bytes in the string, as they are stored, ignoring the character semantics), and also returns data in that encoding. Upvotes: 4