[英]Convert MD5 messageDigest from Java to Digest::MD5 Ruby
I want to use Digest::MD5 on ruby like a java code. 我想像Java代码一样在Ruby上使用Digest :: MD5。 This is the java code:
这是java代码:
public static String MD5Encode(String input, String salt) {
try {
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
byte[] hash = null;
try {
messageDigest.update(salt.getBytes("UTF-8"));
messageDigest.update(input.getBytes("UTF-8"));
hash = messageDigest.digest();
} catch (UnsupportedEncodingException exception) {
logger.error("MD5Encoder:Encode:" + exception.toString());
}
if (hash != null) {
StringBuilder output = new StringBuilder(2 * hash.length);
for (byte b : hash) {
output.append(String.format("%02x", b & 0xff));
}
return output.toString();
}
} catch (NoSuchAlgorithmException exception) {
logger.error("MD5Encoder:Encode:" + exception.toString());
}
return null;
}
and this is the ruby code, but the result is not right: (the input variable in java is the password variable on ruby, and the salt variable is the same on both) 这是ruby代码,但是结果不正确:(java中的输入变量是ruby上的password变量,salt变量在两者上都相同)
salt = SecureRandom.hex
if (params[:gamestate_password] != "")
password = Digest::MD5.hexdigest(params[:gamestate_password] + salt)
user_query = "UPDATE user_v54 SET password= '#{password}', passwordSalt= '#{salt}' WHERE userId='#{params[:userId]}'"
end
您应该按照与Java中相同的顺序(在密码前面)将盐放入红宝石中:
password = Digest::MD5.hexdigest(salt + params[:gamestate_password])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.