package cn.reghao.bnt.web.admin.security.encoder;

import cn.reghao.jutil.jdk.security.Md5Cryptor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.security.NoSuchAlgorithmException;

/**
 * @author reghao
 * @date 2019-03-26 14:46:57
 */
@Component
public class Md5PasswordEncoder implements PasswordEncoder {
    private final Md5Cryptor md5Cryptor;

    public Md5PasswordEncoder() throws NoSuchAlgorithmException {
        this.md5Cryptor = new Md5Cryptor();
    }

    @Override
    public String encode(CharSequence rawPassword) {
        // rawPassword 带盐值
        return md5Cryptor.encrypt(rawPassword.toString());
    }

    private byte[] encode(CharSequence rawPassword, byte[] salt) {
        return null;
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        String password = md5Cryptor.encrypt(rawPassword.toString());
        return encodedPassword.equals(password);
    }
}
