package com.server.RoadToInerview.security;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.security.MessageDigest;
public class SHA512PasswordEncoder implements PasswordEncoder {
private final Log logger = LogFactory.getLog(getClass()); // 로그생성하기
@Override
public String encode(CharSequence rawPassword) {//인코딩하기
if (rawPassword == null){
throw new IllegalArgumentException("빈칸은 넣으면 안됨");
}
return this.getSHA512Pw(rawPassword);
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
//인코딩하여 저장한 것과 비교하기
if (rawPassword == null) {
throw new IllegalArgumentException("빈칸 X");
}
if (encodedPassword == null || encodedPassword.length() == 0) {
this.logger.warn("빈칸 X");
return false;
}
String encodedRawPw = this.getSHA512Pw(rawPassword);
if (encodedPassword.length() != encodedRawPw.length()) { //길이 다르면 탈락
return false;
}
for (int i = 0; i < encodedPassword.length(); i++) {
if(encodedPassword.charAt(i) != encodedRawPw.charAt(i)) return false;
}
return true;
}
}