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;}
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));
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));}}
public final class MD5 {public enum SaltOption {BEFORE, AFTER, BOTH, NONE;}private static final String ALG = "MD5";//For conversion to 2-char hexprivate 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]));}}
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 exceptionJOptionPane.showMessageDialog(null,e1.getClass().getName() + ": " + e1.getMessage());}return hashtext;}
现在只要你需要就调用这个函数,如下所示。
String text = textFieldName.getText();String pass = md5hashing(text);
import java.security.MessageDigest
val digest = MessageDigest.getInstance("MD5")
//Quick MD5 of textval text = "MD5 this text!"val md5hash1 = digest.digest(text.getBytes).map("%02x".format(_)).mkString
//MD5 of text with updatesdigest.update("MD5 ".getBytes())digest.update("this ".getBytes())digest.update("text!".getBytes())val md5hash2 = digest.digest().map(0xFF & _).map("%02x".format(_)).mkString
//Outputprintln(md5hash1 + " should be the same as " + md5hash2)
<?php$goodtext = "Not found";// If there is no parameter, this code is all skippedif ( 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// textfor($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 $ch2for($j=0; $j<strlen($txt); $j++ ) {$ch2 = $txt[$j]; // Our second characterfor($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 0if ( $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";}?>