Skip to content

Instantly share code, notes, and snippets.

@SeanPONeil
Created May 1, 2013 20:03
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 SeanPONeil/5497929 to your computer and use it in GitHub Desktop.
Save SeanPONeil/5497929 to your computer and use it in GitHub Desktop.
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