@Override public void run() { final String hex = StringUtils.hex(bytes); // avoid optimization by HotSpot if (hex.length() != bytes.length * 2) { throw new RuntimeException(); } }
/** * Generates a digest (hex string) for the given string */ public static String digest(String string, String digestAlgo, String encoding) { try { MessageDigest digester = MessageDigest.getInstance(digestAlgo); digester.update(string.getBytes(encoding)); return hex(digester.digest()); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * Generates a digest (hex string) for the given string */ public static String digest(String string, String digestAlgo, String encoding) { try { MessageDigest digester = MessageDigest.getInstance(digestAlgo); digester.update(string.getBytes(encoding)); return hex(digester.digest()); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** @return MD5 digest (32 characters). */ public static String getMd5(InputStream in) throws IOException { byte[] digest = getDigest(in, "MD5"); return StringUtils.hex(digest); }
/** @return SHA-1 digest (40 characters). */ public static String getSha1(InputStream in) throws IOException { byte[] digest = getDigest(in, "SHA-1"); return StringUtils.hex(digest); }
/** @return MD5 digest (32 characters). */ public static String getMd5(InputStream in) throws IOException { byte[] digest = getDigest(in, "MD5"); return StringUtils.hex(digest); }
/** @return SHA-1 digest (40 characters). */ public static String getSha1(InputStream in) throws IOException { byte[] digest = getDigest(in, "SHA-1"); return StringUtils.hex(digest); }
@Test public void testHexBig() { for (int i = 0; i < 256 * 256; i++) { byte[] bytes = {(byte) (i >> 8), (byte) i}; String hexExpected = DatatypeConverter.printHexBinary(bytes); String hex = StringUtils.hex(bytes); assertEquals(hexExpected, hex); byte[] bytes2 = StringUtils.parseHex(hex); assertEquals(bytes[0], bytes2[0]); assertEquals(bytes[1], bytes2[1]); } } }
/** * Generates a digest (hex string) for the given string */ public static String digest(String string, String digestAlgo, String encoding) { try { MessageDigest digester = MessageDigest.getInstance(digestAlgo); digester.update(string.getBytes(encoding)); return hex(digester.digest()); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * Generates a digest (hex string) for the given string */ public static String digest(String string, String digestAlgo, String encoding) { try { MessageDigest digester = MessageDigest.getInstance(digestAlgo); digester.update(string.getBytes(encoding)); return hex(digester.digest()); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** @return SHA-1 digest (40 characters). */ public static String getSha1(InputStream in) throws IOException { byte[] digest = getDigest(in, "SHA-1"); return StringUtils.hex(digest); }
/** @return SHA-1 digest (40 characters). */ public static String getSha1(InputStream in) throws IOException { byte[] digest = getDigest(in, "SHA-1"); return StringUtils.hex(digest); }
/** @return MD5 digest (32 characters). */ public static String getMd5(InputStream in) throws IOException { byte[] digest = getDigest(in, "MD5"); return StringUtils.hex(digest); }
/** @return MD5 digest (32 characters). */ public static String getMd5(InputStream in) throws IOException { byte[] digest = getDigest(in, "MD5"); return StringUtils.hex(digest); }