Esempio n. 1
0
  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;
  }
Esempio n. 2
0
 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;
 }