public static String hash(String target, String algorithm, Coding coding) { MessageDigest md = null; try { md = MessageDigest.getInstance(algorithm); } catch (Exception e) { e.printStackTrace(); } byte[] bytes = target.getBytes(); byte[] byteHash = md.digest(bytes); String encodedHash = null; switch (coding) { case BASE_64: encodedHash = Base64.encodeBytes(byteHash); break; case HEX: encodedHash = toHex(byteHash); break; default: throw new IllegalArgumentException("Unsuported coding:" + coding.name()); } return encodedHash; }
private String encodePassword(String password) { String pw = ""; try { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.reset(); byte byteData[] = md.digest(password.getBytes()); pw = Base64.encodeBytes(byteData); } catch (Exception e) { System.out.println("Error: " + e); } return pw; }
@SuppressWarnings("unchecked") public static Authentication getAuthenticationToken(Message message) { Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS); String token = null; for (Cookie c : extractCookies(headers.get("cookie"))) { if (AUTH_TOKEN.equalsIgnoreCase(c.getName())) { token = c.getValue(); break; } } if (token == null) { return null; } try { return (Authentication) Base64.decodeToObject(token); } catch (Exception e) { return null; } }