在Java中是否有任何方法可以生成一个字符串的MD5哈希?
当前回答
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)
其他回答
我已经使用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";
}
?>
你可以参考这个资料来源
下面是我如何使用它:
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));
来自Apache Commons项目的Hex。
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));
}
}
Codingkit上有一篇关于这个的文章。查看:http://codingkit.com/a/JAVA/2013/1020/2216.html
您可能还想看看apache公用编解码器项目的DigestUtils类,它提供了非常方便的方法来创建MD5或SHA摘要。
我只是下载了common -codec.jar,得到了完美的php,比如md5。这是手册。
只需将其导入到您的项目并使用即可
String Url = "your_url";
System.out.println( DigestUtils.md5Hex( Url ) );
结果出来了。
推荐文章
- Eclipse调试器总是阻塞在ThreadPoolExecutor上,没有任何明显的异常,为什么?
- Java生成两个给定值之间的随机数
- 如何有效地从数组列表或字符串数组中删除所有空元素?
- 比较JUnit断言中的数组,简洁的内置方式?
- codestyle;把javadoc放在注释之前还是之后?
- 如何在Spring中定义List bean ?
- 将Set<T>转换为List<T>的最简洁的方法
- 在JavaScript中,什么相当于Java的Thread.sleep() ?
- 使用Java重命名文件
- URL从Java中的类路径加载资源
- .toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
- Hibernate中不同的保存方法之间有什么区别?
- Java 8流和数组操作
- Java Regex捕获组
- Openssl不被视为内部或外部命令