Skip to content

Instantly share code, notes, and snippets.

@paralax
Last active July 10, 2023 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paralax/5cffcd5fb126b47e8aa6ae7c4527c751 to your computer and use it in GitHub Desktop.
Save paralax/5cffcd5fb126b47e8aa6ae7c4527c751 to your computer and use it in GitHub Desktop.
Implementation of Hhhash in Java (HTTP Header Hashing)
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.security.MessageDigest;
/*
Implementation of Hhhash in Java
HTTP Header Hashing
Used to group hosts together ... Take the keys in the order presented by the server
and concat them with ":" and SHA-256 hash that
https://www.foo.be/2023/07/HTTP-Headers-Hashing_HHHash
NOTE
if some of this looks wonky it's because i developed it on a Java 1.6 system, and
since them some things i had to do are now in the standard library (like String.join).
*/
public class Hhhash {
private static String join(String separator, List < String > input) {
if (input == null || input.size() <= 0)
return "";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.size(); i++) {
sb.append(input.get(i));
//if not the last item
if (i != input.size() - 1) {
sb.append(separator);
}
}
return sb.toString();
}
public static String getSha256(String value) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(value.getBytes());
return bytesToHex(md.digest());
} catch(Exception ex) {
throw new RuntimeException(ex);
}
}
private static String bytesToHex(byte[] bytes) {
StringBuffer result = new StringBuffer();
for (byte b: bytes)
result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
return result.toString();
}
public static void main(String[] args) {
for (String s:args) {
List<String> l = new ArrayList < String > ();
try {
URL u = new URL(s);
URLConnection conn = u.openConnection();
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry: map.entrySet()) {
l.add(entry.getKey());
}
String hhhval = getSha256(join(":", l));
System.out.println(u.getHost() + " -> hhh:1:" + hhhval);
} catch(IOException e) {
System.out.println(e.getMessage());
continue;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment