简体   繁体   English

我如何在Java中找到php md5(sha1(“ test”))

[英]how can i find php md5(sha1(“test”)) in java

In my existing system, i have hashed the password with the following algorithm in php. 在我现有的系统中,我在php中使用以下算法对密码进行了哈希处理。

$userId = "testusername";
$password = "testpassword";
echo md5(sha1($userId).sha1($password));

what will be the equivalent method in Java for the above, because i was migrating php to java. 以上将是Java中的等效方法,因为我正在将php迁移到Java。

when i tried to search in google, they are talking about MessageDigest method. 当我尝试在Google中搜索时,他们正在谈论MessageDigest方法。 In PHP i have used the inbuild md5() and sha1() function 在PHP中,我使用了内置的md5()和sha1()函数

in java, i found the following, but still, its not equivalent. 在Java中,我发现了以下内容,但仍不相同。

   public static String sha1(String input) {
    StringBuilder sb = null;
    try{
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.reset();
        md.update(input.getBytes());
        byte[] bytes = md.digest();
        sb = new StringBuilder();
        for(int i=0; i< bytes.length ;i++)
        {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
    }catch(RuntimeException | NoSuchAlgorithmException e){
        throw new RuntimeException(e.getMessage());
    }
    return sb.toString();
}

public static String md5(String input) {

    StringBuilder sb = null;
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(input.getBytes());
        byte[] bytes = md.digest();
        sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        }
    } catch (RuntimeException | NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage());
    }
    return sb.toString();
}

} }

You can try bellow example : 您可以尝试下面的例子:

class Main {
    public static void main(String[] a) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String output, input = "ml";
        MessageDigest md = MessageDigest.getInstance("md5");
        byte[] digest = md.digest(input.getBytes("UTF-8"));
        BigInteger bigInt = new BigInteger(1, digest);
        output = bigInt.toString(16);
        System.out.println(""+output);
    }
}

In the same way you also can generate sha1 just pass "sha1" in MessageDigest.getInstance(); 同样,您也可以通过在MessageDigest.getInstance();传递"sha1"来生成sha1 MessageDigest.getInstance(); function. 功能。

sha1($userId)+sha1($password) completely wrong. sha1($userId)+sha1($password)完全错误。 To do string concatenation in PHP you need sha1($userId).sha1($password) 要在PHP中进行字符串连接,您需要sha1($userId).sha1($password)

The result you're seeing in PHP is actually md5(8) or c9f0f895fb98ab9159f51fd0297e236d . 您在PHP中看到的结果实际上是md5(8)c9f0f895fb98ab9159f51fd0297e236d This is because the SHA1 of $password begins with an 8. The rest of the hash is thrown away. 这是因为$password的SHA1以8开头。其余的哈希值被丢弃。

This can not be used as a secure hashing function because there are too many collisions. 由于冲突太多,因此不能将其用作安全的哈希函数。 For example, a password of 12345 has the same hash. 例如,密码12345具有相同的哈希。 You should require users to reset their passwords and use a standard password hashing mechanism instead. 您应该要求用户重置其密码,并使用标准的密码哈希机制。

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

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