/** * Create a password hash using specific hashing algorithm * * @param input The password * @param hashType The hashing algorithm * @return The password hash */ public static String passwordHash(String input, HashType hashType) { try { MessageDigest m = MessageDigest.getInstance(hashType.toString()); byte[] out = m.digest(input.getBytes()); return new String(Base64.encodeBase64(out)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
/* * checkSum * * Checks to make sure return value of content is is UTF-8, * and has recognized mimetype text/plain * * Parameters * content - String to test * * checksumType - byte sequence to use to test against * */ static byte[] checkSum(String content, HashType checksumType) { byte[] contentBytes; try { contentBytes = content.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new RuntimeException("Encoding unknown!"); } MessageDigest md; try { md = MessageDigest.getInstance(checksumType.name()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("Algorithm unknown!"); } byte[] digest = md.digest(contentBytes); return digest; }