本文整理汇总了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));
}
}
}
示例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);
}
示例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");
}
}
示例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();
}
示例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;
}
示例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);
}
示例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;
}
}
示例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);
}
}
示例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;
}
示例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;
}
示例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;
}
}
示例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 "";
}
示例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;
}
示例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;
}
}
示例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;
}