當前位置: 首頁>>代碼示例>>Java>>正文


Java MessageDigest類代碼示例

本文整理匯總了Java中java.security.MessageDigest的典型用法代碼示例。如果您正苦於以下問題:Java MessageDigest類的具體用法?Java MessageDigest怎麽用?Java MessageDigest使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MessageDigest類屬於java.security包,在下文中一共展示了MessageDigest類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testCodecConsistency

import java.security.MessageDigest; //導入依賴的package包/類
@Test
public void testCodecConsistency()
    throws NoSuchAlgorithmException, UnsupportedEncodingException
{
    byte[] decoded = null;

    for (int h=0; h < 1000; h++) {
        byte[] digest = MessageDigest.getInstance("SHA-1").digest(
            UUID.randomUUID().toString().getBytes("UTF-8")
        );
        String b16Encoded = Base16.encodeAsString(digest);
        {
            decoded = Base16.decode(b16Encoded);
            Assert.assertTrue(Arrays.equals(decoded, digest));
            decoded = Base16Lower.decode(b16Encoded);
            Assert.assertTrue(Arrays.equals(decoded, digest));
        }
        {   // test decoding case insensitivity
            decoded = Base16.decode(b16Encoded.toLowerCase());
            Assert.assertTrue(Arrays.equals(decoded, digest));
            decoded = Base16Lower.decode(b16Encoded.toLowerCase());
            Assert.assertTrue(Arrays.equals(decoded, digest));
        }
    }
}
 
開發者ID:IBM,項目名稱:ibm-cos-sdk-java,代碼行數:26,代碼來源:Base16CodecTest.java

示例2: HMACMD5

import java.security.MessageDigest; //導入依賴的package包/類
HMACMD5(byte[] key) throws NTLMEngineException {
    try {
        md5 = MessageDigest.getInstance("MD5");
    } catch (Exception ex) {
        // Umm, the algorithm doesn't exist - throw an
        // NTLMEngineException!
        throw new NTLMEngineException(
                "Error getting md5 message digest implementation: " + ex.getMessage(), ex);
    }

    // Initialize the pad buffers with the key
    ipad = new byte[64];
    opad = new byte[64];

    int keyLength = key.length;
    if (keyLength > 64) {
        // Use MD5 of the key instead, as described in RFC 2104
        md5.update(key);
        key = md5.digest();
        keyLength = key.length;
    }
    int i = 0;
    while (i < keyLength) {
        ipad[i] = (byte) (key[i] ^ (byte) 0x36);
        opad[i] = (byte) (key[i] ^ (byte) 0x5c);
        i++;
    }
    while (i < 64) {
        ipad[i] = (byte) 0x36;
        opad[i] = (byte) 0x5c;
        i++;
    }

    // Very important: update the digest with the ipad buffer
    md5.reset();
    md5.update(ipad);

}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:39,代碼來源:NTLMEngineImpl.java

示例3: base64AndMD5

import java.security.MessageDigest; //導入依賴的package包/類
/**
 * 先進行MD5摘要再進行Base64編碼獲取摘要字符串
 *
 * @return
 */
public static String base64AndMD5(byte[] bytes) {
    if (bytes == null) {
        throw new IllegalArgumentException("bytes can not be null");
    }
    try {
        final MessageDigest md = MessageDigest.getInstance("MD5");
        md.reset();
        md.update(bytes);
        final Base64 base64 = new Base64();
        final byte[] enbytes = base64.encode(md.digest());
        return new String(enbytes);
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("unknown algorithm MD5");
    }
}
 
開發者ID:linkingli,項目名稱:FaceDistinguish,代碼行數:21,代碼來源:MessageDigestUtil.java

示例4: createChecksum

import java.security.MessageDigest; //導入依賴的package包/類
public static byte[] createChecksum(String filename) throws Exception {
    InputStream fis = new FileInputStream(filename);

    byte[] buffer = new byte[1024];
    MessageDigest complete = MessageDigest.getInstance("MD5");
    int numRead;

    do {
        numRead = fis.read(buffer);
        if (numRead > 0) {
            complete.update(buffer, 0, numRead);
        }
    } while (numRead != -1);

    fis.close();
    return complete.digest();
}
 
開發者ID:cybershrapnel,項目名稱:nanocheeze,代碼行數:18,代碼來源:Torrent3.java

示例5: SHA256

import java.security.MessageDigest; //導入依賴的package包/類
private String SHA256(String data)
{
    String signature = null;
    try
    {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        md.update(data.getBytes("UTF-8"));
        byte[] bytes = md.digest(data.getBytes("UTF-8"));
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++)
            sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
        signature = sb.toString();
    }
    catch (NoSuchAlgorithmException | UnsupportedEncodingException e)
    {
    }
    return signature;
}
 
開發者ID:CrypDist,項目名稱:CrypDist,代碼行數:19,代碼來源:MerkleTree.java

示例6: verify

import java.security.MessageDigest; //導入依賴的package包/類
public static boolean verify(byte[] signature, byte[] message, byte[] publicKey, boolean enforceCanonical) {

        if (enforceCanonical && !Curve25519.isCanonicalSignature(signature)) {
            Logger.logDebugMessage("Rejecting non-canonical signature");
            return false;
        }

        if (enforceCanonical && !Curve25519.isCanonicalPublicKey(publicKey)) {
            Logger.logDebugMessage("Rejecting non-canonical public key");
            return false;
        }

        byte[] Y = new byte[32];
        byte[] v = new byte[32];
        System.arraycopy(signature, 0, v, 0, 32);
        byte[] h = new byte[32];
        System.arraycopy(signature, 32, h, 0, 32);
        Curve25519.verify(Y, v, h, publicKey);

        MessageDigest digest = Crypto.sha256();
        byte[] m = digest.digest(message);
        digest.update(m);
        byte[] h2 = digest.digest(Y);

        return Arrays.equals(h, h2);
    }
 
開發者ID:muhatzg,項目名稱:burstcoin,代碼行數:27,代碼來源:Crypto.java

示例7: MD5

import java.security.MessageDigest; //導入依賴的package包/類
public static String MD5(String s) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    try {
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        byte[] md = mdInst.digest(s.getBytes());
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (byte b : md) {
            str[k++] = hexDigits[b >>> 4 & 0xf];
            str[k++] = hexDigits[b & 0xf];
        }
        return new String(str).toLowerCase();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
 
開發者ID:pelucky,項目名稱:DouyuDanmu,代碼行數:19,代碼來源:Danmu.java

示例8: hashWithDigest

import java.security.MessageDigest; //導入依賴的package包/類
private static String hashWithDigest(final String in, final String digest) {
    try {
        MessageDigest Digester = MessageDigest.getInstance(digest);
        Digester.update(in.getBytes("UTF-8"), 0, in.length());
        byte[] sha1Hash = Digester.digest();
        return toSimpleHexString(sha1Hash);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Hashing the password failed", ex);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Encoding the string failed", e);
    }

}
 
開發者ID:ergothvs,項目名稱:Lucid2.0,代碼行數:14,代碼來源:LoginCrypto.java

示例9: getDigest

import java.security.MessageDigest; //導入依賴的package包/類
/** get digest from cache */

    private MessageDigest getDigest(String algorithm)
            throws SignatureException {
        if (createdDigests == null)
            createdDigests = new HashMap<>();

        MessageDigest digest = createdDigests.get(algorithm);

        if (digest == null) {
            try {
                digest = MessageDigest.getInstance(algorithm);
                createdDigests.put(algorithm, digest);
            } catch (NoSuchAlgorithmException nsae) {
                // ignore
            }
        }
        return digest;
    }
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:SignatureFileVerifier.java

示例10: MD5Encode

import java.security.MessageDigest; //導入依賴的package包/類
public static String MD5Encode(String origin) throws Exception{
 String resultString = null;
 resultString = new String(origin);
 MessageDigest md = MessageDigest.getInstance("MD5");
 resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
 return resultString;
}
 
開發者ID:viakiba,項目名稱:karanotes,代碼行數:8,代碼來源:EncryptUtil.java

示例11: checksum

import java.security.MessageDigest; //導入依賴的package包/類
public static String checksum(Long domainId, List<ITransaction> list) {
  xLogger.fine("Entered checksum: {0}", (list == null ? "NULL" : list.size()));
  Iterator<ITransaction> it = list.iterator();
  try {
    MessageDigest md = MessageDigest.getInstance("MD5");
    while (it.hasNext()) {
      ITransaction trans = it.next();
      if (trans.getDomainId() == null) {
        trans.setDomainId(domainId);
      }
      byte[] fingerprint = trans.fingerprint();
      if (fingerprint != null) {
        md.update(fingerprint);
      }
    }
    return new String(Hex.encodeHex(md.digest()));
  } catch (Exception e) {
    xLogger.warn("{0} when getting checksum: {1}", e.getClass().getName(), e.getMessage());
    return null;
  }
}
 
開發者ID:logistimo,項目名稱:logistimo-web-service,代碼行數:22,代碼來源:TransactionUtil.java

示例12: md5

import java.security.MessageDigest; //導入依賴的package包/類
public static String md5(byte[] source) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(source);
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuilder hexString = new StringBuilder();
        for (byte aMessageDigest : messageDigest) {
            hexString.append(Integer.toHexString(0xFF & aMessageDigest));
        }
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}
 
開發者ID:drakeet,項目名稱:rebase-android,代碼行數:20,代碼來源:Md5.java

示例13: getWebSocketAccept

import java.security.MessageDigest; //導入依賴的package包/類
private String getWebSocketAccept(String key) throws ServletException {

		MessageDigest sha1Helper = sha1Helpers.poll();
		if (sha1Helper == null) {
			try {
				sha1Helper = MessageDigest.getInstance("SHA1");
			} catch (NoSuchAlgorithmException e) {
				throw new ServletException(e);
			}
		}

		sha1Helper.reset();
		sha1Helper.update(key.getBytes(B2CConverter.ISO_8859_1));
		String result = Base64.encode(sha1Helper.digest(WS_ACCEPT));

		sha1Helpers.add(sha1Helper);

		return result;
	}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:20,代碼來源:WebSocketServlet.java

示例14: getMessageDigest

import java.security.MessageDigest; //導入依賴的package包/類
/**
 * 十六進製
 *
 * @param buffer
 * @return
 */
public static String getMessageDigest(byte[] buffer) {
    char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    try {
        MessageDigest mdTemp = MessageDigest.getInstance(Algorithm.MD5.getType());
        mdTemp.update(buffer);
        byte[] md = mdTemp.digest();
        int j = md.length;
        char[] str = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (Exception e) {
        return null;
    }
}
 
開發者ID:lzmlsfe,項目名稱:19porn,代碼行數:26,代碼來源:Codec.java

示例15: md5

import java.security.MessageDigest; //導入依賴的package包/類
public static String md5(byte[] plainText) {
	String result = "";
	try {
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(plainText);
		byte b[] = md.digest();
		int i;
		StringBuffer buf = new StringBuffer("");
		for (int offset = 0; offset < b.length; offset++) {
			i = b[offset];
			if (i < 0)
				i += 256;
			if (i < 16)
				buf.append("0");
			buf.append(Integer.toHexString(i));
		}
		result = buf.toString();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	return result;
}
 
開發者ID:langxianwei,項目名稱:iot-plat,代碼行數:23,代碼來源:Tools.java


注:本文中的java.security.MessageDigest類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。