/** * Creates a salted hash for a password using a SHA based hash function. * <p> * This method supports the following algorithms: * <ul> * <li>sha-256</li> * <li>sha-512</li> * </ul> * * @param hashFunction The hash function to use. * @param salt Salt in the form of byte array (may be {@code null}). * @param password The clear text password. * @return The salted hash as defined by * <a href="https://www.eclipse.org/hono/api/credentials-api/#hashed-password">Hono's Credentials API</a>. */ public static String encode(final String hashFunction, final byte[] salt, final String password) { Objects.requireNonNull(hashFunction); Objects.requireNonNull(password); final PasswordEncoder encoder = new MessageDigestPasswordEncoder(hashFunction); final StringBuilder passwordToEncode = new StringBuilder(); // Prepare password in the "{salt}password" format as that's expected by MessageDigestPasswordEncoder class append(salt, passwordToEncode); passwordToEncode.append(password); return encoder.encode(passwordToEncode.toString()); }