📄 msgdigest.java
字号:
/*
$Author: $
$Date: $
$Revision: $
$NoKeywords: $
*/
package jp.co.ntl.codec;
import java.security.*;
import java.io.UnsupportedEncodingException;
public class MsgDigest
{
public static byte[] getDigest(String data) {
return getDigest(data, "MD5");
}
public static byte[] getDigest(String data, String algorithm) {
byte[] bytes;
try {
bytes = data.getBytes("ISO-8859-1");
} catch(UnsupportedEncodingException e) {
return null;
}
return getDigest(bytes, algorithm);
}
public static byte[] getDigest(byte[] bytes, String algorithm) {
byte[] md_bytes;
try {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.update(bytes);
md_bytes = md.digest();
} catch (NoSuchAlgorithmException e) {
md_bytes = null;
}
return md_bytes;
}
public static boolean compareDigest(byte[] bytes1, byte[] bytes2) {
boolean ret = compareDigest(bytes1, bytes2, "MD5");
return ret;
}
public static boolean compareDigest(String str, byte[] bytes2) {
byte[] bytes1;
try {
bytes1 = str.getBytes("ISO-8859-1");
} catch(UnsupportedEncodingException e) {
return false;
}
return compareDigest(bytes1, bytes2, "MD5");
}
public static boolean compareDigest(byte[] bytes1, byte[] bytes2, String algorithm) {
return MessageDigest.isEqual( bytes1, bytes2 );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -