/** * Computes the MD5 of the given file. */ public static byte[] computeMD5Hash(File file) throws FileNotFoundException, IOException { return computeMD5Hash(new FileInputStream(file)); }
@Override public Optional<RequestBody> modifyHttpContent(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { if (!BLACKLIST_METHODS.contains(context.request().getClass()) && context.requestBody().isPresent() && !context.httpRequest().firstMatchingHeader(CONTENT_MD5).isPresent()) { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IoUtils.copy(context.requestBody().get().contentStreamProvider().newStream(), baos); executionAttributes.putAttribute(CONTENT_MD5_ATTRIBUTE, Md5Utils.md5AsBase64(baos.toByteArray())); return context.requestBody(); } catch (IOException e) { throw new UncheckedIOException(e); } } return context.requestBody(); }
/** * Computes the MD5 of the given file. */ public static byte[] computeMD5Hash(File file) throws FileNotFoundException, IOException { return computeMD5Hash(new FileInputStream(file)); }
/** * Returns the MD5 in base64 for the given file. */ public static String md5AsBase64(File file) throws FileNotFoundException, IOException { return BinaryUtils.toBase64(computeMD5Hash(file)); } }
/** * Returns the MD5 in base64 for the data from the given input stream. * Note this method closes the given input stream upon completion. */ public static String md5AsBase64(InputStream is) throws IOException { return BinaryUtils.toBase64(computeMD5Hash(is)); }
/** * Returns the MD5 in base64 for the given byte array. */ public static String md5AsBase64(byte[] input) { return BinaryUtils.toBase64(computeMD5Hash(input)); }
/** * Returns the MD5 in base64 for the data from the given input stream. * Note this method closes the given input stream upon completion. */ public static String md5AsBase64(InputStream is) throws IOException { return BinaryUtils.toBase64(computeMD5Hash(is)); }
/** * Returns the MD5 in base64 for the given byte array. */ public static String md5AsBase64(byte[] input) { return BinaryUtils.toBase64(computeMD5Hash(input)); }
/** * Returns the MD5 in base64 for the given file. */ public static String md5AsBase64(File file) throws FileNotFoundException, IOException { return BinaryUtils.toBase64(computeMD5Hash(file)); } }
/** * Returns the hex-encoded MD5 hash String of the given message body. */ private static String calculateMessageBodyMd5(String messageBody) { log.debug(() -> "Message body: " + messageBody); byte[] expectedMd5; try { expectedMd5 = Md5Utils.computeMD5Hash(messageBody.getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { throw SdkClientException.builder() .message("Unable to calculate the MD5 hash of the message body. " + e.getMessage()) .cause(e) .build(); } String expectedMd5Hex = BinaryUtils.toHex(expectedMd5); log.debug(() -> "Expected MD5 of message body: " + expectedMd5Hex); return expectedMd5Hex; }
/** * Returns the hex-encoded MD5 hash String of the given message body. */ private static String calculateMessageBodyMd5(String messageBody) { log.debug(() -> "Message body: " + messageBody); byte[] expectedMd5; try { expectedMd5 = Md5Utils.computeMD5Hash(messageBody.getBytes(StandardCharsets.UTF_8)); } catch (Exception e) { throw SdkClientException.builder() .message("Unable to calculate the MD5 hash of the message body. " + e.getMessage()) .cause(e) .build(); } String expectedMd5Hex = BinaryUtils.toHex(expectedMd5); log.debug(() -> "Expected MD5 of message body: " + expectedMd5Hex); return expectedMd5Hex; }