Utilize the fetch
function with force-cache
to retrieve content from cache. After that, process the arrayBuffer from the response. It is worth noting that MD5 is considered outdated and no longer supported by browsers. For more information on Crypto, visit this link. You have the option to choose between SHA-1, SHA-256, SHA-384, and SHA-512 for digesting. An example of digesting using SHA-256 is provided below.
public void getImageSHA256(){
driver.get("https://www.blognone.com/");
WebElement img = driver.findElement(By.cssSelector("img"));
String imgUrl = img.getAttribute("src").trim();
String script = "function hex(buffer) { var hexCodes = []; var view = new DataView(buffer); for (var i = 0; i < view.byteLength; i += 4) { var value = view.getUint32(i); var stringValue = value.toString(16); var padding = '00000000'; var paddedValue = (padding + stringValue).slice(-padding.length); hexCodes.push(paddedValue); } return hexCodes.join(\"\");}" +
"var callback = arguments[arguments.length - 1];" +
"fetch(arguments[0],{cache:'force-cache'}).then((response)=> {" +
"return response.arrayBuffer(); }).then((buffer)=>{" +
" return crypto.subtle.digest('SHA-256', buffer); }).then((hashArray)=>{" +
" callback(hex(hashArray));"+
"});";
driver.manage().timeouts().setScriptTimeout(15, TimeUnit.SECONDS);
Object response = ((JavascriptExecutor) driver).executeAsyncScript(script, imgUrl);
System.out.println(response);
}
The screenshot displayed below illustrates the comparison between the SHA-256 generated by my code and the one produced by an online tool.
https://i.sstatic.net/lGBsI.png