private static void appendThumbPrint(X509Certificate cert, String hash, StringBuilder sb) {
   try {
     MessageDigest md = MessageDigest.getInstance(hash);
     byte[] der = cert.getEncoded();
     md.update(der);
     byte[] digest = md.digest();
     char[] encode = Hex.encode(digest);
     appendHexSpace(encode, sb);
   } catch (NoSuchAlgorithmException e) {
     sb.append("Error calculating thumbprint: " + e.getMessage());
   } catch (CertificateEncodingException e) {
     sb.append("Error calculating thumbprint: " + e.getMessage());
   }
 }
  /**
   * Encodes the rawPass using a MessageDigest. If a salt is specified it will be merged with the
   * password before encoding.
   *
   * @param rawPass The plain text password
   * @param salt The salt to sprinkle
   * @return Hex string of password digest (or base64 encoded string if encodeHashAsBase64 is
   *     enabled.
   */
  public String encodePassword(String rawPass, Object salt) {
    String saltedPass = mergePasswordAndSalt(rawPass, salt, false);

    MessageDigest messageDigest = getMessageDigest();

    byte[] digest;

    try {
      digest = messageDigest.digest(saltedPass.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      throw new IllegalStateException("UTF-8 not supported!");
    }

    // "stretch" the encoded value if configured to do so
    for (int i = 1; i < iterations; i++) {
      digest = messageDigest.digest(digest);
    }

    if (getEncodeHashAsBase64()) {
      return new String(Base64.encode(digest));
    } else {
      return new String(Hex.encode(digest));
    }
  }
Esempio n. 3
0
 public static final String toHex(byte[] digest) {
   String hexString = new String(Hex.encode(digest));
   return hexString;
 }