简体   繁体   English

在Java实现中寻找javascript md5方法

[英]Looking for javascript md5 method in java implementation

I have Javascript md5 on site auth . 我在auth站点上有Javascript md5。

I need to implement only this function: 我只需要实现以下功能:

function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}

I need help with following methods: 我需要以下方法的帮助:

Convert an array of little-endian words to a hex string: 将小尾数词数组转换为十六进制字符串:

function binl2hex(binarray)
{
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for(var i = 0; i < binarray.length * 4; i++)
  {
    str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
           hex_tab.charAt((binarray[i>>2] >> ((i%4)*8  )) & 0xF);
  }
  return str;
}

Convert a string to an array of little-endian words If chrsz is ASCII, characters >255 have their hi-byte silently ignored. 将字符串转换为小端单词数组。如果chrsz是ASCII,大于255的字符的高字节将被忽略。

function str2binl(str)
{
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}  

If you're just looking to implement MD5, that's something which is built in to java. 如果您只是想实现MD5,那是内置于Java中的东西。

https://stackoverflow.com/a/415971/576519 https://stackoverflow.com/a/415971/576519

I have developed a simple Java MD5 function that is compatible with a standard JavaScript function you can download the class from: http://developersfound.com/HexMD5.zip 我已经开发了与标准JavaScript函数兼容的简单Java MD5函数,可以从以下网站下载该类: http : //developersfound.com/HexMD5.zip

Here is the code: 这是代码:

/* This MD5 hash class is compatible with the JavaScript MD5 has code found at http://pajhome.org.uk/crypt/md5/md5.html */
package com.DevFound;

import java.security.MessageDigest;

public class HexMD5 {
    public static String getMD5Str(String inputVal)throws Exception
    {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(inputVal.getBytes());

        byte byteData[] = md.digest();

        //convert the byte to hex format method 1
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        //convert the byte to hex format method 2
        StringBuffer hexString = new StringBuffer();
        for (int i=0;i<byteData.length;i++) {
            String hex=Integer.toHexString(0xff & byteData[i]);
            if(hex.length()==1) hexString.append('0');
            hexString.append(hex);
        }
        return hexString.toString();
    }
}

I haven't tested this code with your particular JavaScript md5 function but I have listed the function that it is compatible with in the comment at the top of the code. 我尚未使用您的特定JavaScript md5函数测试此代码,但在代码顶部的注释中列出了与之兼容的函数。

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

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