import java.security.*; .. byte[] bytesOfMessage = yourString.getBytes("UTF-8"); MessageDigest md = MessageDigest.getInstance("MD5"); byte[] thedigest = md.digest(bytesOfMessage);
private byte[] md5(String value) { MessageDigest md5; try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e.getMessage(), e); } md5.reset(); byte[] bytes = value.getBytes(StandardCharsets.UTF_8); md5.update(bytes); return md5.digest(); }
String plaintext = "your text here"; MessageDigest m = MessageDigest.getInstance("MD5"); m.reset(); m.update(plaintext.getBytes()); byte[] digest = m.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; }
/** * Calculates the MD5 digest for the given input stream and returns it. */ public static byte[] calculateMD5Digest(InputStream is) throws NoSuchAlgorithmException, IOException { int bytesRead = 0; byte[] buffer = new byte[2048]; MessageDigest md5 = MessageDigest.getInstance("MD5"); while ((bytesRead = is.read(buffer)) != -1) { md5.update(buffer, 0, bytesRead); } return md5.digest(); }
private static byte[] digestFile(InputStream input, MessageDigest messageDigest) throws IOException { int bufferLength = 8 * 1024; byte[] buffer = new byte[bufferLength]; int read = input.read(buffer, 0, bufferLength); while (read > -1) { messageDigest.update(buffer, 0, read); read = input.read(buffer, 0, bufferLength); } return messageDigest.digest(); }
Security.addProvider(new BouncyCastleProvider()); String data = "hello world"; MessageDigest mda = MessageDigest.getInstance("SHA-512", "BC"); byte [] digesta = mda.digest(data.getBytes()); MessageDigest mdb = MessageDigest.getInstance("SHA-512", "BC"); byte [] digestb = mdb.digest(data.getBytes()); System.out.println(MessageDigest.isEqual(digesta, digestb)); System.out.println(Hex.encodeHex(digesta));
public static byte[] md5Bytes(String text) { MessageDigest msgDigest = null; try { msgDigest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("System doesn't support MD5 algorithm."); } msgDigest.update(text.getBytes()); byte[] bytes = msgDigest.digest(); return bytes; }
private static byte[] computeMd5Bytes(InputStream is, long remaining) throws IOException { byte readBuff[] = new byte[4096]; MessageDigest digest = MD5_DIGEST.get(); digest.reset(); int read; while (remaining > 0 && (read = is.read(readBuff)) != -1) { int updateLen = (int) Math.min(remaining, read); digest.update(readBuff, 0, updateLen); remaining -= updateLen; } return digest.digest(); }
protected String generateKey(Map<String, String> values) { MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); byte[] bytes = digest.digest(values.toString().getBytes("UTF-8")); return String.format("%032x", new BigInteger(1, bytes)); } catch (NoSuchAlgorithmException nsae) { throw new IllegalStateException("MD5 algorithm not available. Fatal (should be in the JDK).", nsae); } catch (UnsupportedEncodingException uee) { throw new IllegalStateException("UTF-8 encoding not available. Fatal (should be in the JDK).", uee); } } }
/** * Returns the fingerprint of the public key. * * @return the fingerprint of the public key. */ public String getFingerprint() { RSAPublicKey key = InstanceIdentityProvider.RSA.getPublicKey(); if (key == null) { return null; } // TODO replace with org.jenkinsci.remoting.util.KeyUtils once JENKINS-36871 changes are merged try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.reset(); byte[] bytes = digest.digest(key.getEncoded()); StringBuilder result = new StringBuilder(Math.max(0, bytes.length * 3 - 1)); for (int i = 0; i < bytes.length; i++) { if (i > 0) { result.append(':'); } int b = bytes[i] & 0xFF; result.append(Character.forDigit((b>>4)&0x0f, 16)).append(Character.forDigit(b&0xf, 16)); } return result.toString(); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("JLS mandates MD5 support"); } } }
/** * Get the md5 for inputStream. * This method cost less memory. It read bufLen bytes from the FileInputStream once. * * @param is * @param bufLen bytes number read from the stream once. * The less bufLen is the more times getMD5() method takes. Also the less bufLen is the less memory cost. */ public static String getMD5(final InputStream is, final int bufLen) { if (is == null || bufLen <= 0) { return null; } try { MessageDigest md = MessageDigest.getInstance("MD5"); StringBuilder md5Str = new StringBuilder(32); byte[] buf = new byte[bufLen]; int readCount = 0; while ((readCount = is.read(buf)) != -1) { md.update(buf, 0, readCount); } byte[] hashValue = md.digest(); for (int i = 0; i < hashValue.length; i++) { md5Str.append(Integer.toString((hashValue[i] & 0xff) + 0x100, 16).substring(1)); } return md5Str.toString(); } catch (Exception e) { return null; } }
private static String calcClassSign(String name) { try { Class<?> clazz = Class.forName(name); ByteArrayOutputStream result = new ByteArrayOutputStream(); ObjectOutputStream outputStream = new ObjectOutputStream(result); outputStream.writeObject(clazz); outputStream.close(); MessageDigest crypt = MessageDigest.getInstance("SHA-1"); crypt.reset(); crypt.update(result.toByteArray()); return new BigInteger(1, crypt.digest()).toString(16); } catch (Exception e) { throw new IllegalStateException("Can't calculate sign of " + name, e); } }
private static byte[] digest(String algorithm, InputStream inputStream) throws IOException { MessageDigest messageDigest = getDigest(algorithm); if (inputStream instanceof UpdateMessageDigestInputStream){ ((UpdateMessageDigestInputStream) inputStream).updateMessageDigest(messageDigest); return messageDigest.digest(); } else { final byte[] buffer = new byte[StreamUtils.BUFFER_SIZE]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { messageDigest.update(buffer, 0, bytesRead); } return messageDigest.digest(); } }
private byte[] md5(String value) { MessageDigest md5; try { md5 = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e.getMessage(), e); } md5.reset(); byte[] bytes = value.getBytes(StandardCharsets.UTF_8); md5.update(bytes); return md5.digest(); }
/** * get md5 in byte array * * @param buffer data to be calculated * @return md5 result in byte array format */ public static byte[] getRawDigest(byte[] buffer) { try { MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(buffer); return mdTemp.digest(); } catch (Exception e) { return null; } }
private static byte[] compute(File f, String algorithm) throws IOException { InputStream is = Files.newInputStream(f.toPath()); try { MessageDigest md = getMessageDigest(algorithm); md.reset(); byte[] buf = new byte[BUFFER_SIZE]; int len = 0; while ((len = is.read(buf)) != -1) { md.update(buf, 0, len); } return md.digest(); } finally { is.close(); } }
MessageDigest md = MessageDigest.getInstance("MD5"); try (InputStream is = Files.newInputStream(Paths.get("file.txt")); DigestInputStream dis = new DigestInputStream(is, md)) { /* Read decorated stream (dis) to EOF as normal... */ } byte[] digest = md.digest();
/** * 对字符串进行散列, 支持md5与sha1算法. */ private static byte[] digest(@NotNull byte[] input, MessageDigest digest, byte[] salt, int iterations) { // 带盐 if (salt != null) { digest.update(salt); } // 第一次散列 byte[] result = digest.digest(input); // 如果迭代次数>1,进一步迭代散列 for (int i = 1; i < iterations; i++) { digest.reset(); result = digest.digest(result); } return result; }