public void asBytes(byte[] bytes) { asBytes(bytes, 0); }
public void asBytes(byte[] bytes) { asBytes(bytes, 0); }
/** * @return hash as hex string */ public String asString() { return toHexString(asBytes()); }
/** * get hash from given bytes and check for equals it with hashBytes */ public boolean check(byte[] b, byte[] hashBytes) { return Arrays.equals(update(b).asBytes(), hashBytes); } }
/** * @return hash as hex string */ public String asString() { return toHexString(asBytes()); }
/** * get hash from given bytes and check for equals it with hashBytes */ public boolean check(byte[] b, byte[] hashBytes) { return Arrays.equals(update(b).asBytes(), hashBytes); } }
public static byte[] toTransfer(SecretKey key) { byte[] b = new byte[32]; System.arraycopy(key.getEncoded(), 0, b, 0, 16); System.arraycopy(MD5.create().update(key.getEncoded()).asBytes(), 0, b, 16, 16); return b; }
public static byte[] toTransfer(SecretKey key) { byte[] b = new byte[32]; System.arraycopy(key.getEncoded(), 0, b, 0, 16); System.arraycopy(MD5.create().update(key.getEncoded()).asBytes(), 0, b, 16, 16); return b; }
public static SecretKey generateKeyAsMD5(String key) { return generateKey(MD5.create().update(key).asBytes()); }
public static SecretKey generateKeyAsMD5(String key) { return generateKey(MD5.create().update(key).asBytes()); }
public String generateToken(Request request) { String auth = request.header(Header.KEY_AUTHORIZATION); if (auth == null) return request.paramWithDefault("token", ""); byte[] token = new byte[40]; // 16+16+8 MD5.create().update(getUser(request)).asBytes(token, 0); // key BytesHolder secret = hashes.get(new BytesHolder(token, 0, 16)); long timestamp = System.currentTimeMillis() + ttl; BytesTools.toBytes(timestamp, token, 32, 8); MD5.create().update(token, 32, 8).update(secret.bytes).asBytes(token, 16); return Base64.encodeToString(token, false, true); }
@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; }
@Test public void sha1_test() { String data = "foo bar\n"; Assert.assertEquals("d53a205a336e07cf9eac45471b3870f9489288ec", SHA1.create().update(data).asString()); byte[] hash = new byte[20]; SHA1.create().update(data).asBytes(hash); Assert.assertArrayEquals(SHA1.create().update(data).asBytes(), hash); }
key = Base64.encodeToString(SHA1.create().update(key.getBytes()).asBytes());