I have this method on ruby which converts a string into a encrypted SHA code and I would like to know if it is somehow possible to achieve that using javascript?
The fact is that I don’t know exactly what are the configurations for this SHA (I believe it is 512 but I’m not sure, I tried few javascript online tools but I couldn’t achieve the same result)
require 'digest'
require 'iconv'
word = 'testing'
puts Digest::SHA2.new(512).hexdigest(Iconv.conv('UTF-16LE','ISO-8859-15', word))
#=> 6e42b2c2a6351036b78384212774135d99d849da3066264983e495b5f74dc922e3d361b8ea9c8527169757233ed0bd4e56b2c42aab0a21bbcca67219dc53b472
Perhaps by knowing what are the exact configurations used in the method above I could find it easier in javascript?
Thanks in advance
Just in case anybody needs that method to convert the string to hex UTF-16, here it is:
function toHex(str) {
var group = [], i;
for (i = 0; i < str.length; i += 1) {
group.push(str.charCodeAt(i).toString(16));
}
return group.join('00') + '00';
}
The reason you’re getting different results is because the Ruby code converts
'testing'into UTF-16, and the jsSHA example is using ASCII. If you expand “testing” into 16-bit Unicode, you get"740065007300740069006e006700"in hexadecimal, which gives the same answer as Ruby. Of course, you would also need to set it to SHA-512 like you mentioned.