code: private static String getHash(String str, String algorithm) throws NoSuchAlgorithmException {
  
          MessageDigest md = MessageDigest.getInstance(algorithm);
          md.update(str.getBytes());
  
          byte byteData[] = md.digest();
  
          StringBuilder hexString = new StringBuilder();
          for (int i = 0; i < byteData.length; i++) {
              String hex = Integer.toHexString(0xff & byteData[i]);
              if (hex.length() == 1) {
                  hexString.append('0');
              }
              hexString.append(hex);
          }
  
          return hexString.toString();
      }
docs: Return the hezadecimal representation of the string digest given the
  specified algorithm.
  
  @param str
  @param algorithm
  @return
  @throws NoSuchAlgorithmException