简体   繁体   English

如何在Java中生成MD5 hash?

[英]How can I generate an MD5 hash in Java?

Is there any method to generate MD5 hash of a string in Java?有什么方法可以生成Java中字符串的MD5 hash吗?

The MessageDigest class can provide you with an instance of the MD5 digest. MessageDigest类可以为您提供 MD5 摘要的实例。

When working with strings and the crypto classes be sure to always specify the encoding you want the byte representation in. If you just use string.getBytes() it will use the platform default.使用字符串和加密类时,请确保始终指定您想要的字节表示的编码。如果您只使用string.getBytes()它将使用平台默认值。 (Not all platforms use the same defaults) (并非所有平台都使用相同的默认值)

import java.security.*;

..

byte[] bytesOfMessage = yourString.getBytes("UTF-8");

MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);

If you have a lot of data take a look at the .update(byte[]) method which can be called repeatedly.如果您有大量数据,请查看可以重复调用的.update(byte[])方法。 Then call .digest() to obtain the resulting hash.然后调用.digest()以获取结果哈希。

You need java.security.MessageDigest .你需要java.security.MessageDigest

Call MessageDigest.getInstance("MD5") to get a MD5 instance of MessageDigest you can use.调用MessageDigest.getInstance("MD5")以获取您可以使用的MessageDigest的 MD5 实例。

The compute the hash by doing one of:通过执行以下操作之一来计算哈希:

  • Feed the entire input as a byte[] and calculate the hash in one operation with md.digest(bytes) .将整个输入作为byte[]输入,并使用md.digest(bytes)在一次操作中计算哈希。
  • Feed the MessageDigest one byte[] chunk at a time by calling md.update(bytes) .通过调用md.update(bytes)一次为MessageDigest一个byte[]块。 When you're done adding input bytes, calculate the hash with md.digest() .添加完输入字节后,使用md.digest()计算散列。

The byte[] returned by md.digest() is the MD5 hash. md.digest()返回的byte[]是 MD5 哈希值。

If you actually want the answer back as a string as opposed to a byte array, you could always do something like this:如果您确实希望将答案作为字符串而不是字节数组返回,您可以始终执行以下操作:

String plaintext = "your text here";
MessageDigest m = MessageDigest.getInstance("MD5");
m.reset();
m.update(plaintext.getBytes());
byte[] digest = m.digest();
BigInteger bigInt = new BigInteger(1,digest);
String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
while(hashtext.length() < 32 ){
  hashtext = "0"+hashtext;
}

You might also want to look at the DigestUtils class of the apache commons codec project, which provides very convenient methods to create MD5 or SHA digests.您可能还想查看 apache commons codec项目的DigestUtils类,它提供了非常方便的方法来创建 MD5 或 SHA 摘要。

Found this:发现这个:

public String MD5(String md5) {
   try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(md5.getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; ++i) {
          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
       }
        return sb.toString();
    } catch (java.security.NoSuchAlgorithmException e) {
    }
    return null;
}

on the site below, I take no credit for it, but its a solution that works!在下面的网站上,我不相信它,但它是一个有效的解决方案! For me lots of other code didnt work properly, I ended up missing 0s in the hash.对我来说,许多其他代码无法正常工作,我最终在哈希中丢失了 0。 This one seems to be the same as PHP has.这个好像和 PHP 一样。 source: http://m2tec.be/blog/2010/02/03/java-md5-hex-0093来源: http : //m2tec.be/blog/2010/02/03/java-md5-hex-0093

Here is how I use it:这是我如何使用它:

final MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(string.getBytes(Charset.forName("UTF8")));
final byte[] resultByte = messageDigest.digest();
final String result = new String(Hex.encodeHex(resultByte));

where Hex is: org.apache.commons.codec.binary.Hex from the Apache Commons project .其中 Hex 是:来自Apache Commons 项目的org.apache.commons.codec.binary.Hex

I just downloaded commons-codec.jar and got perfect php like md5.我刚刚下载了commons-codec.jar并获得了像 md5 这样的完美 php。 Here is manual .这里是手册

Just import it to your project and use只需将其导入您的项目并使用

String Url = "your_url";

System.out.println( DigestUtils.md5Hex( Url ) );

and there you have it.你有它。

I've found this to be the most clear and concise way to do it:我发现这是最清晰简洁的方法:

MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(StandardCharsets.UTF_8.encode(string));
return String.format("%032x", new BigInteger(1, md5.digest()));

Another implementation:另一个实现:

import javax.xml.bind.DatatypeConverter;

String hash = DatatypeConverter.printHexBinary( 
           MessageDigest.getInstance("MD5").digest("SOMESTRING".getBytes("UTF-8")));

Found this solution which is much cleaner in terms of getting a String representation back from an MD5 hash.发现这个解决方案在从 MD5 散列中获取字符串表示方面更清晰。

import java.security.*;
import java.math.*;

public class MD5 {
    public static void main(String args[]) throws Exception{
        String s="This is a test";
        MessageDigest m=MessageDigest.getInstance("MD5");
        m.update(s.getBytes(),0,s.length());
        System.out.println("MD5: "+new BigInteger(1,m.digest()).toString(16));
    }
}

The code was extracted from here .代码是从这里提取的。

No need to make it too complicated.没必要让它太复杂。
DigestUtils works fine and makes you comfortable while working with md5 hashes. DigestUtils工作正常,让您在使用md5哈希时感到舒适。

DigestUtils.md5Hex(_hash);

or或者

DigestUtils.md5(_hash);

Either you can use any other encryption methods such as sha or md .您可以使用任何其他加密方法,例如shamd

Another option is to use the Guava Hashing methods :另一种选择是使用 番石榴哈希方法

Hasher hasher = Hashing.md5().newHasher();
hasher.putString("my string");
byte[] md5 = hasher.hash().asBytes();

Handy if you are already using Guava (which if you're not, you probably should be).如果你已经在使用 Guava(如果你没有使用,你可能应该使用),这很方便。

I have a Class (Hash) to convert plain text in hash in formats: md5 or sha1, simillar that php functions ( md5 , sha1 ):我有一个类(散列)来将散列中的纯文本转换为以下格式:md5 或 sha1,类似于 php 函数( md5sha1 ):

public class Hash {
    /**
     * 
     * @param txt, text in plain format
     * @param hashType MD5 OR SHA1
     * @return hash in hashType 
     */
    public static String getHash(String txt, String hashType) {
        try {
                    java.security.MessageDigest md = java.security.MessageDigest.getInstance(hashType);
                    byte[] array = md.digest(txt.getBytes());
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < array.length; ++i) {
                        sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
                 }
                    return sb.toString();
            } catch (java.security.NoSuchAlgorithmException e) {
                //error action
            }
            return null;
    }

    public static String md5(String txt) {
        return Hash.getHash(txt, "MD5");
    }

    public static String sha1(String txt) {
        return Hash.getHash(txt, "SHA1");
    }
}

Testing with JUnit and PHP使用 JUnit 和 PHP 进行测试

PHP Script: PHP脚本:

<?php

echo 'MD5 :' . md5('Hello World') . "\n";
echo 'SHA1:' . sha1('Hello World') . "\n";

Output PHP script:输出 PHP 脚本:

MD5 :b10a8db164e0754105b7a99be72e3fe5
SHA1:0a4d55a8d778e5022fab701977c5d840bbc486d0

Using example and Testing with JUnit:使用示例和使用 JUnit 进行测试:

    public class HashTest {

    @Test
    public void test() {
        String txt = "Hello World";
        assertEquals("b10a8db164e0754105b7a99be72e3fe5", Hash.md5(txt));
        assertEquals("0a4d55a8d778e5022fab701977c5d840bbc486d0", Hash.sha1(txt));
    }

}

Code in GitHub GitHub 中的代码

https://github.com/fitorec/java-hashes https://github.com/fitorec/java-hashes

My not very revealing answer:我的回答不是很透彻:

private String md5(String s) {
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.update(s.getBytes(), 0, s.length());
        BigInteger i = new BigInteger(1,m.digest());
        return String.format("%1$032x", i);         
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return null;
}

There is a DigestUtils class in Spring also: Spring 中还有一个DigestUtils类:

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/util/DigestUtils.html http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/util/DigestUtils.html

This class contains the method md5DigestAsHex() that does the job.这个类包含完成这项工作的方法md5DigestAsHex()

You can try following.您可以尝试以下。 See details and download codes here: http://jkssweetlife.com/java-hashgenerator-md5-sha-1/在此处查看详细信息和下载代码: http : //jkssweetlife.com/java-hashgenerator-md5-sha-1/

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Example {

public static void main(String[] args) throws Exception {

    final String inputString = "Hello MD5";

    System.out.println("MD5 hex for '" + inputString + "' :");
    System.out.println(getMD5Hex(inputString));
}

public static String getMD5Hex(final String inputString) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(inputString.getBytes());

    byte[] digest = md.digest();

    return convertByteToHex(digest);
}

private static String convertByteToHex(byte[] byteData) {

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < byteData.length; i++) {
        sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
    }

    return sb.toString();
}
}

Bombe's answer is correct, however note that unless you absolutely must use MD5 (eg forced on you for interoperability), a better choice is SHA1 as MD5 has weaknesses for long term use. Bombe 的回答是正确的,但是请注意,除非您绝对必须使用 MD5(例如强制使用以实现互操作性),否则更好的选择是 SHA1,因为 MD5 有长期使用的弱点。

I should add that SHA1 also has theoretical vulnerabilities, but not as severe.我应该补充一点,SHA1 也有理论上的漏洞,但没有那么严重。 The current state of the art in hashing is that there are a number of candidate replacement hash functions but none have yet emerged as the standard best practice to replace SHA1.散列技术的当前状态是有许多候选替换散列函数,但还没有一个作为替换 SHA1 的标准最佳实践出现。 So, depending on your needs you would be well advised to make your hash algorithm configurable so it can be replaced in future.因此,根据您的需要,建议您使哈希算法可配置,以便将来可以替换它。

另一个实现: Java 中的快速 MD5 实现

String hash = MD5.asHex(MD5.getHash(new File(filename)));

I do not know if this is relevant for anyone reading this, but I just had the problem that I wanted to我不知道这是否与任何阅读本文的人相关,但我只是遇到了我想要的问题

  • download a file from a given URL and从给定的 URL 下载文件
  • compare its MD5 to a known value.将其 MD5 与已知值进行比较。

I wanted to do it with JRE classes only (no Apache Commons or similar).我只想用 JRE 类来做(没有 Apache Commons 或类似的)。 A quick web search did not show me sample code snippets doing both at the same time, only each task separately.快速的网络搜索没有向我显示同时执行这两个任务的示例代码片段,只是分别执行每个任务。 Because this requires to read the same file twice, I figured it might be worth the while to write some code which unifies both tasks, calculating the checksum on the fly while downloading the file.因为这需要读取同一个文件两次,所以我认为编写一些代码来统一这两个任务,在下载文件时即时计算校验和可能是值得的。 This is my result (sorry if it is not perfect Java, but I guess you get the idea anyway):这是我的结果(对不起,如果它不是完美的 Java,但我想你还是明白了):

import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.security.DigestOutputStream;        // new
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

void downloadFile(String fromURL, String toFile, BigInteger md5)
    throws IOException, NoSuchAlgorithmException
{
    ReadableByteChannel in = Channels.newChannel(new URL(fromURL).openStream());
    MessageDigest md5Digest = MessageDigest.getInstance("MD5");
    WritableByteChannel out = Channels.newChannel(
        //new FileOutputStream(toFile));  // old
        new DigestOutputStream(new FileOutputStream(toFile), md5Digest));  // new
    ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024);  // 1 MB

    while (in.read(buffer) != -1) {
        buffer.flip();
        //md5Digest.update(buffer.asReadOnlyBuffer());  // old
        out.write(buffer);
        buffer.clear();
    }

    BigInteger md5Actual = new BigInteger(1, md5Digest.digest()); 
    if (! md5Actual.equals(md5))
        throw new RuntimeException(
            "MD5 mismatch for file " + toFile +
            ": expected " + md5.toString(16) +
            ", got " + md5Actual.toString(16)
        );
}

Unlike PHP where you can do an MD5 hashing of your text by just calling md5 function ie md5($text) , in Java it was made little bit complicated.与 PHP 不同,您可以通过调用 md5 函数即md5($text)对文本进行 MD5 散列,在 Java 中它变得有点复杂。 I usually implemented it by calling a function which returns the md5 hash text.我通常通过调用一个返回 md5 哈希文本的函数来实现它。 Here is how I implemented it, First create a function named md5hashing inside your main class as given below.这是我实现它的方法,首先在主类中创建一个名为md5hashing的函数,如下所示。

public static String md5hashing(String text)
    {   String hashtext = null;
        try 
        {
            String plaintext = text;
            MessageDigest m = MessageDigest.getInstance("MD5");
            m.reset();
            m.update(plaintext.getBytes());
            byte[] digest = m.digest();
            BigInteger bigInt = new BigInteger(1,digest);
            hashtext = bigInt.toString(16);
            // Now we need to zero pad it if you actually want the full 32 chars.
            while(hashtext.length() < 32 ){
              hashtext = "0"+hashtext;   
            }
        } catch (Exception e1) 
        {
            // TODO: handle exception
            JOptionPane.showMessageDialog(null,e1.getClass().getName() + ": " + e1.getMessage());   
        }
        return hashtext;     
    }

Now call the function whenever you needed as given below.现在在需要时调用该函数,如下所示。

String text = textFieldName.getText();
String pass = md5hashing(text);

Here you can see that hashtext is appended with a zero to make it match with md5 hashing in PHP.在这里您可以看到 hashtext 附加了一个零以使其与 PHP 中的 md5 散列匹配。

For what it's worth, I stumbled upon this because I want to synthesize GUIDs from a natural key for a program that will install COM components;对于它的价值,我偶然发现了这一点,因为我想从一个将安装 COM 组件的程序的自然键中合成 GUID; I want to syhthesize so as not to manage GUID lifecycle.我想综合以便不管理 GUID 生命周期。 I'll use MD5 and then use the UUID class to get a string out of it.我将使用 MD5,然后使用 UUID 类从中获取字符串。 (http://stackoverflow.com/questions/2190890/how-can-i-generate-guid-for-a-string-values/12867439 raises this issue). (http://stackoverflow.com/questions/2190890/how-can-i-generate-guid-for-a-string-values/12867439 提出了这个问题)。

In any case, java.util.UUID can get you a nice String from the MD5 bytes.在任何情况下, java.util.UUID 都可以从 MD5 字节中获得一个不错的字符串。

return UUID.nameUUIDFromBytes(md5Bytes).toString();

是否有任何方法可以在Java中生成字符串的MD5哈希?

import java.security.*;
import javax.xml.bind.*;

byte[] bytesOfMessage = yourString.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytesOfDigest = md.digest(bytesOfMessage);
String digest = DatatypeConverter.printHexBinary(bytesOfDigest).toLowerCase();

this one gives the exact md5 as you get from mysql's md5 function or php's md5 functions etc. This is the one I use (you can change according to your needs)这个给出了你从 mysql 的 md5 函数或 php 的 md5 函数等中得到的准确的 md5。这是我使用的一个(你可以根据你的需要改变)

public static String md5( String input ) {
    try {
        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
        byte[] array = md.digest(input.getBytes( "UTF-8" ));
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            sb.append( String.format( "%02x", array[i]));
        }
        return sb.toString();
    } catch ( NoSuchAlgorithmException | UnsupportedEncodingException e) {
        return null;            
    }

}
import java.security.MessageDigest

val digest = MessageDigest.getInstance("MD5")

//Quick MD5 of text
val text = "MD5 this text!"
val md5hash1 = digest.digest(text.getBytes).map("%02x".format(_)).mkString

//MD5 of text with updates
digest.update("MD5 ".getBytes())
digest.update("this ".getBytes())
digest.update("text!".getBytes())
val md5hash2 = digest.digest().map(0xFF & _).map("%02x".format(_)).mkString

//Output
println(md5hash1 + " should be the same as " + md5hash2)

MD5 is perfectly fine if you don't need the best security, and if you're doing something like checking file integrity then security is not a consideration.如果您不需要最好的安全性,那么 MD5 完全没问题,如果您正在做诸如检查文件完整性之类的事情,那么安全性不是考虑因素。 In such as case you might want to consider something simpler and faster, such as Adler32, which is also supported by the Java libraries.在这种情况下,您可能需要考虑更简单、更快速的方法,例如 Java 库也支持的 Adler32。

try this:尝试这个:

public static String getHashMD5(String string) {
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        BigInteger bi = new BigInteger(1, md.digest(string.getBytes()));
        return bi.toString(16);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(MD5Utils.class
                .getName()).log(Level.SEVERE, null, ex);

        return "";
    }
}

You can generate MD5 hash for a given text by making use of the methods in the MessageDigest class in the java.security package.您可以通过使用java.security包中MessageDigest类中的方法为给定文本生成 MD5 哈希 Below is the complete code snippet,下面是完整的代码片段,

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.xml.bind.DatatypeConverter;

public class MD5HashGenerator 
{

   public static void main(String args[]) throws NoSuchAlgorithmException
   {
       String stringToHash = "MyJavaCode"; 
       MessageDigest messageDigest = MessageDigest.getInstance("MD5");
       messageDigest.update(stringToHash.getBytes());
       byte[] digiest = messageDigest.digest();
       String hashedOutput = DatatypeConverter.printHexBinary(digiest);
       System.out.println(hashedOutput);
   }
}

The output from the MD5 function is a 128 bit hash represented by 32 hexadecimal numbers. MD5 函数的输出是由 32 个十六进制数表示的 128 位散列。

In case, if you are using a database like MySQL, you can do this in a more simpler way as well.如果您使用的是像 MySQL 这样的数据库,您也可以通过更简单的方式来完成此操作。 The query Select MD5(“text here”) will return the MD5 hash of the text in the bracket.查询Select MD5(“text here”)将返回括号中文本的 MD5 哈希值。

这就是我来这里的目的 - 一个方便的 scala 函数,它返回 MD5 哈希字符串:

def md5(text: String) : String = java.security.MessageDigest.getInstance("MD5").digest(text.getBytes()).map(0xFF & _).map { "%02x".format(_) }.foldLeft(""){_ + _}
 import java.math.BigInteger;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;

/**
* MD5 encryption
*
* @author Hongten
*
*/
public class MD5 {

 public static void main(String[] args) {
     System.out.println(MD5.getMD5("123456"));
 }

 /**
  * Use md5 encoded code value
  *
  * @param sInput
  * clearly
  * @ return md5 encrypted password
  */
 public static String getMD5(String sInput) {

     String algorithm = "";
     if (sInput == null) {
         return "null";
     }
     try {
         algorithm = System.getProperty("MD5.algorithm", "MD5");
     } catch (SecurityException se) {
     }
     MessageDigest md = null;
     try {
         md = MessageDigest.getInstance(algorithm);
     } catch (NoSuchAlgorithmException e) {
         e.printStackTrace();
     }
     byte buffer[] = sInput.getBytes();

     for (int count = 0; count < sInput.length(); count++) {
         md.update(buffer, 0, count);
     }
     byte bDigest[] = md.digest();
     BigInteger bi = new BigInteger(bDigest);
     return (bi.toString(16));
 }
}

There is an article on Codingkit about that.有一篇关于 Codingkit 的文章。 Check out: http://codingkit.com/a/JAVA/2013/1020/2216.html查看: http : //codingkit.com/a/JAVA/2013/1020/2216.html

You could try using Caesar .您可以尝试使用Caesar

First option:第一个选项:

byte[] hash =
    new Hash(
        new ImmutableMessageDigest(
            MessageDigest.getInstance("MD5")
        ),
        new PlainText("String to hash...")
    ).asArray();

Second option:第二种选择:

byte[] hash =
    new ImmutableMessageDigest(
        MessageDigest.getInstance("MD5")
    ).update(
        new PlainText("String to hash...")
    ).digest();
private String hashuj(String dane) throws ServletException{
    try {
        MessageDigest m = MessageDigest.getInstance("MD5");
        byte[] bufor = dane.getBytes();
        m.update(bufor,0,bufor.length);
        BigInteger hash = new BigInteger(1,m.dige`enter code here`st());
        return String.format("%1$032X", hash);

    } catch (NoSuchAlgorithmException nsae) {
        throw new ServletException("Algorytm szyfrowania nie jest obsługiwany!");
    }
}

Simple function using java.security.MessageDigest library使用java.security.MessageDigest库的简单 function

public String stringMD5Hash(String text) throws NoSuchAlgorithmException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    byte[] bytes = messageDigest.digest(text.getBytes());
    StringJoiner stringJoiner = new StringJoiner("");
    for (byte b : bytes) {
        stringJoiner.add(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
    }
    return stringJoiner.toString();
}

and for files use this function对于文件,请使用此 function

public static String FileMD5Hash(File file) throws NoSuchAlgorithmException, IOException {
    MessageDigest messageDigest = MessageDigest.getInstance("MD5");
    byte[] bytes = messageDigest.digest(getFileBytes(file));
    StringJoiner stringJoiner = new StringJoiner("");
    for (byte b : bytes) {
        stringJoiner.add(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
    }
    return stringJoiner.toString();
}

public static byte[] getFileBytes(File file) throws IOException{
    InputStream inputStream = new FileInputStream(file);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] bytes = new byte[1024];
    int bytesRead;
    while ((bytesRead = inputStream.read(bytes)) != -1) {
        byteArrayOutputStream.write(bytes, 0, bytesRead);
    }
    return byteArrayOutputStream.toByteArray();
}

I did this... Seems to work ok - I'm sure somebody will point out mistakes though...我这样做了......似乎工作正常 - 我相信有人会指出错误......

public final class MD5 {
public enum SaltOption {
    BEFORE, AFTER, BOTH, NONE;
}
private static final String ALG = "MD5";
//For conversion to 2-char hex
private static final char[] digits = {
    '0' , '1' , '2' , '3' , '4' , '5' ,
    '6' , '7' , '8' , '9' , 'a' , 'b' ,
    'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};

private SaltOption opt;

/**
 * Added the SaltOption constructor since everybody
 * has their own standards when it comes to salting
 * hashes.
 * 
 * This gives the developer the option...
 * 
 * @param option The salt option to use, BEFORE, AFTER, BOTH or NONE.
 */
public MD5(final SaltOption option) {
    //TODO: Add Char Encoding options too... I was too lazy!
    this.opt = option;
}

/**
 * 
 * Returns the salted MD5 checksum of the text passed in as an argument.
 * 
 * If the salt is an empty byte array - no salt is applied.
 * 
 * @param txt The text to run through the MD5 algorithm.
 * @param salt The salt value in bytes.
 * @return The salted MD5 checksum as a <code>byte[]</code>
 * @throws NoSuchAlgorithmException
 */
private byte[] createChecksum(final String txt, final byte[] salt) throws NoSuchAlgorithmException {
    final MessageDigest complete = MessageDigest.getInstance(ALG);
    if(opt.equals(SaltOption.BEFORE) || opt.equals(SaltOption.BOTH)) {
        complete.update(salt);
    }
    complete.update(txt.getBytes());
    if(opt.equals(SaltOption.AFTER) || opt.equals(SaltOption.BOTH)) {
        complete.update(salt);
    }
    return complete.digest();
}

/**
 * 
 * Returns the salted MD5 checksum of the file passed in as an argument.
 * 
 * If the salt is an empty byte array - no salt is applied.
 * 
 * @param fle The file to run through the MD5 algorithm.
 * @param salt The salt value in bytes.
 * @return The salted MD5 checksum as a <code>byte[]</code>
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
private byte[] createChecksum(final File fle, final byte[] salt)
        throws IOException, NoSuchAlgorithmException {
    final byte[] buffer = new byte[1024];
    final MessageDigest complete = MessageDigest.getInstance(ALG);
            if(opt.equals(SaltOption.BEFORE) || opt.equals(SaltOption.BOTH)) {
            complete.update(salt);
        }
    int numRead;
    InputStream fis = null;
    try {
        fis = new FileInputStream(fle);
        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);
    } finally {
    if (fis != null) {
            fis.close();
        }
    }
            if(opt.equals(SaltOption.AFTER) || opt.equals(SaltOption.BOTH)) {
            complete.update(salt);
        }
    return complete.digest();
}

/**
 * 
 * Efficiently converts a byte array to its 2 char per byte hex equivalent.
 * 
 * This was adapted from JDK code in the Integer class, I just didn't like
 * having to use substrings once I got the result...
 *
 * @param b The byte array to convert
 * @return The converted String, 2 chars per byte...
 */
private String convertToHex(final byte[] b) {
    int x;
    int charPos;
    int radix;
    int mask;
    final char[] buf = new char[32];
    final char[] tmp = new char[3];
    final StringBuilder md5 = new StringBuilder();
    for (int i = 0; i < b.length; i++) {
        x = (b[i] & 0xFF) | 0x100;
        charPos = 32;
        radix = 1 << 4;
        mask = radix - 1;
        do {
            buf[--charPos] = digits[x & mask];
            x >>>= 4;
        } while (x != 0);
        System.arraycopy(buf, charPos, tmp, 0, (32 - charPos));
        md5.append(Arrays.copyOfRange(tmp, 1, 3));
    }
    return md5.toString();
}

/**
 * 
 * Returns the salted MD5 checksum of the file passed in as an argument.
 * 
 * @param fle The file you want want to run through the MD5 algorithm.
 * @param salt The salt value in bytes
 * @return The salted MD5 checksum as a 2 char per byte HEX <code>String</code>
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public String getMD5Checksum(final File fle, final byte[] salt)
        throws NoSuchAlgorithmException, IOException {
    return convertToHex(createChecksum(fle, salt));
}

/**
 * 
 * Returns the MD5 checksum of the file passed in as an argument.
 * 
 * @param fle The file you want want to run through the MD5 algorithm.
 * @return The MD5 checksum as a 2 char per byte HEX <code>String</code>
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public String getMD5Checksum(final File fle)
        throws NoSuchAlgorithmException, IOException {
    return convertToHex(createChecksum(fle, new byte[0]));
}

/**
 * 
 * Returns the salted MD5 checksum of the text passed in as an argument.
 * 
 * @param txt The text you want want to run through the MD5 algorithm.
 * @param salt The salt value in bytes.
 * @return The salted MD5 checksum as a 2 char per byte HEX <code>String</code>
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public String getMD5Checksum(final String txt, final byte[] salt)
        throws NoSuchAlgorithmException {
    return convertToHex(createChecksum(txt, salt));
}

/**
 * 
 * Returns the MD5 checksum of the text passed in as an argument.
 * 
 * @param txt The text you want want to run through the MD5 algorithm.
 * @return The MD5 checksum as a 2 char per byte HEX <code>String</code>
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public String getMD5Checksum(final String txt)
        throws NoSuchAlgorithmException {

    return convertToHex(createChecksum(txt, new byte[0]));
}
}

I have made this using php as follows我已经使用 php 做了这个,如下所示

<?php
$goodtext = "Not found";
// If there is no parameter, this code is all skipped
if ( isset($_GET['md5']) ) {
    $time_pre = microtime(true);
    $md5 = $_GET['md5'];
    // This is our alphabet
    $txt = "0123456789";
    $show = 15;
    // Outer loop go go through the alphabet for the
    // first position in our "possible" pre-hash
    // text
    for($i=0; $i<strlen($txt); $i++ ) {
        $ch1 = $txt[$i];   // The first of two characters
        // Our inner loop Note the use of new variables
        // $j and $ch2 
        for($j=0; $j<strlen($txt); $j++ ) {
            $ch2 = $txt[$j];  // Our second character
            for($k=0; $k<strlen($txt); $k++ ) {
                $ch3 = $txt[$k];
                for($l=0; $l<strlen($txt); $l++){
                    $ch4 = $txt[$l];
                    // Concatenate the two characters together to 
                    // form the "possible" pre-hash text
                    $try = $ch1.$ch2.$ch3.$ch4;
                    // Run the hash and then check to see if we match
                    $check = hash('md5', $try);
                    if ( $check == $md5 ) {
                        $goodtext = $try;
                        break;   // Exit the inner loop
                    }
                    // Debug output until $show hits 0
                    if ( $show > 0 ) {
                        print "$check $try\n";
                        $show = $show - 1;
                    }
                    if($goodtext == $try){
                        break;
                    }
                }
                if($goodtext == $try){
                    break;
                }
            }
            if($goodtext == $try) {
                break;  
            }
        }
        if($goodtext == $try){
            break;
        }
    }
    // Compute ellapsed time
    $time_post = microtime(true);
    print "Ellapsed time: ";
    print $time_post-$time_pre;
    print "\n";
}
?>

you may refer this - source你可以参考这个 -来源

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

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