当前位置: 首页>>代码示例>>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;未经允许,请勿转载。