本文整理汇总了Java中java.security.MessageDigest.digest方法的典型用法代码示例。如果您正苦于以下问题:Java MessageDigest.digest方法的具体用法?Java MessageDigest.digest怎么用?Java MessageDigest.digest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.security.MessageDigest
的用法示例。
在下文中一共展示了MessageDigest.digest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMD5
import java.security.MessageDigest; //导入方法依赖的package包/类
/**
* 生成md5
*
* @param message
* @return
*/
public static String getMD5(String message) {
String md5str = "";
try {
// 1 创建一个提供信息摘要算法的对象,初始化为md5算法对象
MessageDigest md = MessageDigest.getInstance("MD5");
// 2 将消息变成byte数组
byte[] input = message.getBytes();
// 3 计算后获得字节数组,这就是那128位了
byte[] buff = md.digest(input);
// 4 把数组每一字节(一个字节占八位)换成16进制连成md5字符串
md5str = bytesToHex(buff);
} catch (Exception e) {
e.printStackTrace();
}
return md5str;
}
示例2: main
import java.security.MessageDigest; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
X509CertImpl cert = loadCert(CERT_FILENAME);
/* Compute the hash in the same way as CertId constructor */
MessageDigest hash = MessageDigest.getInstance("SHA1");
hash.update(cert.getSubjectX500Principal().getEncoded());
byte[] expectedHash = hash.digest();
CertId certId = new CertId(cert, null);
byte[] receivedHash = certId.getIssuerNameHash();
if (! Arrays.equals(expectedHash, receivedHash)) {
throw new
Exception("Bad hash value for issuer name in CertId object");
}
}
示例3: strToMd5By32
import java.security.MessageDigest; //导入方法依赖的package包/类
public static String strToMd5By32(String str){
String reStr = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes());
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes){
int bt = b&0xff;
if (bt < 16){
stringBuffer.append(0);
}
stringBuffer.append(Integer.toHexString(bt));
}
reStr = stringBuffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return reStr;
}
示例4: getSha1
import java.security.MessageDigest; //导入方法依赖的package包/类
public static String getSha1(String str) {
if (str == null || str.length() == 0) {
return null;
}
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("GBK"));
byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
示例5: getPlayerHash
import java.security.MessageDigest; //导入方法依赖的package包/类
public static String getPlayerHash(UUID uuid)
{
if (hashCache.containsKey(uuid))
return hashCache.get(uuid);
String playerHash;
try
{
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(uuid.toString().getBytes(Charset.forName("UTF-8")));
playerHash = (new HexBinaryAdapter()).marshal(hash);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
return null;
}
hashCache.put(uuid, playerHash);
return playerHash;
}
示例6: calculateHASH
import java.security.MessageDigest; //导入方法依赖的package包/类
public static byte[] calculateHASH(String digestOID, byte[] data) throws Exception{
String digestName = "";
try{
if(Security.getProvider("BC") == null)
Security.addProvider(new BouncyCastleProvider());
if(digestOID.equals(CMSSignedDataGenerator.DIGEST_MD5))
digestName = "MD5";
if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA1))
digestName = "SHA-1";
if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA256))
digestName = "SHA-256";
if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA384))
digestName = "SHA-384";
if(digestOID.equals(CMSSignedDataGenerator.DIGEST_SHA512))
digestName = "SHA-512";
if(digestName.equals(""))
throw new Exception("Unsupported digestOID");
MessageDigest md = MessageDigest.getInstance(digestName, "BC");
md.update(data);
byte[] hash = md.digest();
return hash;
}catch(Exception e){
throw new Exception("Error on the generation for the Hash "+digestName+":\n"+e.getMessage());
}
}
示例7: MD5
import java.security.MessageDigest; //导入方法依赖的package包/类
/**
* 将明文参数进行MD5加密
*
* @param str
* 明文
* @return str 密文
*/
public static String MD5(String str) {
if (StringUtils.isEmpty(str) || StringUtils.isBlank(str)) {
return null;
}
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = str.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
byte[] md = mdInst.digest();
int j = md.length;
char strArr[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
strArr[k++] = hexDigits[byte0 >>> 4 & 0xf];
strArr[k++] = hexDigits[byte0 & 0xf];
}
return new String(strArr);
} catch (Exception e) {
return null;
}
}
示例8: toDigest
import java.security.MessageDigest; //导入方法依赖的package包/类
@NonNull
private static byte[] toDigest(
@NonNull final ClassPath cp,
@NonNull final MessageDigest md) {
final StringBuilder sb = new StringBuilder();
for (ClassPath.Flag flag : cp.getFlags()) {
sb.append(flag.name()).
append(File.pathSeparatorChar);
}
for (ClassPath.Entry e : cp.entries()) {
sb.append(e.getURL().toExternalForm()).
append(File.pathSeparatorChar);
}
try {
return md.digest(sb.toString().getBytes());
} finally {
md.reset();
}
}
示例9: getMD5Value
import java.security.MessageDigest; //导入方法依赖的package包/类
public static String getMD5Value(byte[] inputBytes) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(inputBytes);
byte[] digest = messageDigest.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;
}
return hashtext;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
示例10: updateDigest
import java.security.MessageDigest; //导入方法依赖的package包/类
@Override
public byte[] updateDigest(byte[] data, MessageDigest md) {
byte[] mainPart = new byte[data.length - LASTNBYTES];
for (int i = 0; i < mainPart.length; i++) {
mainPart[i] = data[i];
}
for (int j = 0; j < LASTNBYTES; j++) {
REMAIN[j] = data[data.length - LASTNBYTES + j];
}
md.update(ByteBuffer.wrap(mainPart));
return md.digest(REMAIN);
}
示例11: SHA_1
import java.security.MessageDigest; //导入方法依赖的package包/类
static byte[] SHA_1(byte[] input) {
try {
MessageDigest sha_1 = MessageDigest.getInstance("SHA-1");
sha_1.update(input);
return sha_1.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
示例12: generateHash
import java.security.MessageDigest; //导入方法依赖的package包/类
private byte[] generateHash(byte[] data) throws IOException
{
byte[] digest = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
md.update(data);
digest = md.digest();
} catch (Exception e) {
throw new IOException("generateHash failed: " + e, e);
}
return digest;
}
示例13: computeHash
import java.security.MessageDigest; //导入方法依赖的package包/类
@Override
public byte[] computeHash(byte[] bytes) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("SHA-256 not found", e);
}
md.update(bytes);
byte[] digest = md.digest();
return digest;
}
示例14: MD5
import java.security.MessageDigest; //导入方法依赖的package包/类
/** 计算给定 byte [] 串的 MD5 */
public static byte[] MD5(byte[] input) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
if (md != null) {
md.update(input);
return md.digest();
} else {
return null;
}
}
示例15: md5Hash
import java.security.MessageDigest; //导入方法依赖的package包/类
/**
* Create a MD5 hash of a string (from http://snippets.dzone.com/posts/show/3686)
* @param clear the source text
* @return
* @throws NoSuchAlgorithmException
*/
public String md5Hash(String clear) throws NoSuchAlgorithmException {
MessageDigest m = MessageDigest.getInstance("MD5");
byte[] data = clear.getBytes();
m.update(data,0,data.length);
BigInteger i = new BigInteger(1,m.digest());
return String.format("%1$032X", i);
}