Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private static String parseVals(String key, String val, String prefix) throws InvalidKeyException, NoSuchAlgorithmException, IOException {
long ts = System.currentTimeMillis() / 1000;
String[] parts = val.split("\\|");
String u_prefix = parts[0];
String u_b64 = parts[1];
String u_sig = parts[2];
String sig = Util.hmacSign(key, u_prefix + "|" + u_b64);
if (!Util.hmacSign(key, sig).equals(Util.hmacSign(key, u_sig))) {
return null;
}
if (!u_prefix.equals(prefix)) {
return null;
}
byte[] decoded = Base64.decode(u_b64);
String cookie = new String(decoded);
String[] cookie_parts = cookie.split("\\|");
String username = cookie_parts[0];
String expire = cookie_parts[2];
long expire_ts = Long.parseLong(expire);
if (ts >= expire_ts) {
return null;
}
return username;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.