package cn.reghao.tnb.auth.app.security.encoder;

import cn.reghao.jutil.jdk.security.Sha256Cryptor;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.security.NoSuchAlgorithmException;

/**
 * @author reghao
 * @date 2019-03-26 14:46:57
 */
public class Sha256PasswordEncoder implements PasswordEncoder {
    private final Sha256Cryptor sha256Cryptor;

    public Sha256PasswordEncoder() throws NoSuchAlgorithmException {
        this.sha256Cryptor = new Sha256Cryptor();
    }

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

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