/** Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as * fast as {@link #decode(byte[])}. The preconditions are:<br> * + The array must have a line length of 76 chars OR no line separators at all (one line).<br> * + Line separator must be "\r\n", as specified in RFC 2045 * + The array must not contain illegal characters within the encoded string<br> * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. * @return The decoded array of bytes. May be of length 0. */ public final static byte[] decodeFast(byte[] sArr) { return decodeFast(sArr, false); }
/** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as * fast as {@link #decode(char[])}. The preconditions are:<br> * + The array must have a line length of 76 chars OR no line separators at all (one line).<br> * + Line separator must be "\r\n", as specified in RFC 2045 * + The array must not contain illegal characters within the encoded string<br> * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. * @return The decoded array of bytes. May be of length 0. */ public final static byte[] decodeFast(char[] sArr) { return decodeFast(sArr, false); }
/** Decodes a BASE64 encoded byte array that is known to be resonably well formatted. The method is about twice as * fast as {@link #decode(byte[])}. The preconditions are:<br> * + The array must have a line length of 76 chars OR no line separators at all (one line).<br> * + Line separator must be "\r\n", as specified in RFC 2045 * + The array must not contain illegal characters within the encoded string<br> * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. * @return The decoded array of bytes. May be of length 0. */ public final static byte[] decodeFast(byte[] sArr) { return decodeFast(sArr, false); }
/** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as * fast as {@link #decode(String)}. The preconditions are:<br> * + The array must have a line length of 76 chars OR no line separators at all (one line).<br> * + Line separator must be "\r\n", as specified in RFC 2045 * + The array must not contain illegal characters within the encoded string<br> * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> * @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception. * @return The decoded array of bytes. May be of length 0. */ public final static byte[] decodeFast(String s) { return decodeFast(s, false); }
/** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as * fast as {@link #decode(char[])}. The preconditions are:<br> * + The array must have a line length of 76 chars OR no line separators at all (one line).<br> * + Line separator must be "\r\n", as specified in RFC 2045 * + The array must not contain illegal characters within the encoded string<br> * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> * @param sArr The source array. Length 0 will return an empty array. <code>null</code> will throw an exception. * @return The decoded array of bytes. May be of length 0. */ public final static byte[] decodeFast(char[] sArr) { return decodeFast(sArr, false); }
/** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as * fast as {@link #decode(String)}. The preconditions are:<br> * + The array must have a line length of 76 chars OR no line separators at all (one line).<br> * + Line separator must be "\r\n", as specified in RFC 2045 * + The array must not contain illegal characters within the encoded string<br> * + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br> * @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception. * @return The decoded array of bytes. May be of length 0. */ public final static byte[] decodeFast(String s) { return decodeFast(s, false); }
@Override public String getUser(Request request) { String header = request.header(Header.KEY_AUTHORIZATION); if (header == null) return null; header = new String(Base64.decodeFast(header.substring(6)), StandardCharsets.UTF_8); int i = header.indexOf(':'); if (i == -1) return null; return header.substring(0, i); }
@Override public boolean filter(Request request, Response response) { String token; byte[] data; try { if ((token = request.param("token")) == null || (data = Base64.decodeFast(token, true)).length < 40) return authFilter.filter(request, response); } catch (Exception ignored) { return authFilter.filter(request, response); } BytesHolder secret = hashes.get(new BytesHolder(data, 0, 16)); if (secret == null) return authFilter.filter(request, response); MD5.create().update(data, 32, 8).update(secret.bytes).asBytes(data, 0); if (!BytesHolder.equals(data, 0, 16, data, 16, 16)) return authFilter.filter(request, response); long time = BytesTools.toLong(data, 32); if (System.currentTimeMillis() > time) return authFilter.filter(request, response); return true; }