All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eurodyn.qlack.fuse.aaa.util.Md5PasswordEncoder Maven / Gradle / Ivy

There is a newer version: 3.8.9
Show newest version
package com.eurodyn.qlack.fuse.aaa.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * Provides the legacy password encoder as the default for the AAA.
 * !!! It should not be used as it is not secured.
 */
public class Md5PasswordEncoder implements PasswordEncoder {

    @Override
    public String encode(CharSequence rawPassword) {
        return DigestUtils.md5Hex(rawPassword.toString());
    }

    /**
     * Constant time comparison to prevent against timing attacks.
     */
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        String rawHash = DigestUtils.md5Hex(rawPassword.toString());
        byte[] expected = Hex.decode(rawHash);
        byte[] actual = Hex.decode(encodedPassword);

        if (expected.length != actual.length) {
            return false;
        }

        int result = 0;

        for (int i = 0; i < expected.length; i++) {
            result |= expected[i] ^ actual[i];
        }

        return result == 0;
    }

    public byte[] generateSalt(int saltLength) {
        return KeyGenerators.secureRandom(saltLength).generateKey();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy