Skip to content

Instantly share code, notes, and snippets.

@SeanPONeil
Created May 1, 2013 20:03

Revisions

  1. SeanPONeil created this gist May 1, 2013.
    31 changes: 31 additions & 0 deletions parseParams.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    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;
    }