import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class URLHashing {
private static final Charset UTF_8 = StandardCharsets.UTF_8;
private static final String OUTPUT_FORMAT = "%-20s:%s";
public static void main(String[] args)
{
String pText = "Hello MD5";
System.out.println(String.format(OUTPUT_FORMAT, "Input (string)", pText));
System.out.println(String.format(OUTPUT_FORMAT, "Input (length)", pText.length()));
byte[] md5InBytes = getMD5Hash(pText);
System.out.println(String.format(OUTPUT_FORMAT, "MD5 (hex) ", convertToHex(md5InBytes)));
// fixed length, 16 bytes, 128 bits.
System.out.println(String.format(OUTPUT_FORMAT, "MD5 (hex length)", convertToHex(md5InBytes).length()));
System.out.println(String.format("MD5 (base64) " + convertToBase64(md5InBytes)));
System.out.println(String.format("MD5 (base 64 length)" + convertToBase64(md5InBytes).length()));
}
public static byte[] getMD5Hash(String path)
{
MessageDigest md ;
try{
md = MessageDigest.getInstance("MD5");
}
catch(final NoSuchAlgorithmException ex)
{
throw new IllegalArgumentException(ex);
}
byte[] bytes = md.digest(path.getBytes(UTF_8));
return bytes;
}
public static String convertToHex(byte[] bytes)
{
// convert to hex
StringBuilder sb = new StringBuilder();
for(byte b: bytes)
{
//%x is a format specifier that format and output the hex value.
//If you are providing int or long value, it will convert it to hex value.
//%02x means if your provided value is less than two digits then 0 will be prepended.
//You provided value 16843009 and it has been converted to 1010101 which a hex value.
sb.append(String.format("%02x", b));
}
return sb.toString();
}
public static String convertToBase64(byte[] bytes)
{
String base64 = Base64.getEncoder().encodeToString(bytes);
return base64;
}
}
To speed up queries on non-key attributes, you can create a global secondary index in DynamoDB. A global secondary index contains a selection of attributes from the base table, but they are organized by a primary key that is different from that of the table. The index key does not need to have any of the key attributes from the table. It doesn't even need to have the same key schema as a table.
Being cacheable is one of the architectural constraints of REST.
GET requests should be cachable by default – until a special condition arises. Usually, browsers treat all GET requests as cacheable.
POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response.
Responses to PUT and DELETE requests are not cacheable at all.