BcryptLegacyPasswordVerifier.cs 1002 B

123456789101112131415161718192021222324252627282930313233
  1. using Application.Abstractions.Authentication;
  2. namespace Infrastructure.Authentication;
  3. // dpot 1.0 legacy 회원 비밀번호(bcrypt) 검증기. BCrypt.Net-Next 사용.
  4. // Domain은 외부 의존성 0 원칙이라 bcrypt 검증은 Infrastructure에 둔다.
  5. internal sealed class BcryptLegacyPasswordVerifier : ILegacyPasswordVerifier
  6. {
  7. public bool IsLegacyHash(string? hash)
  8. {
  9. if (string.IsNullOrEmpty(hash))
  10. {
  11. return false;
  12. }
  13. return hash.StartsWith("$2a$", StringComparison.Ordinal)
  14. || hash.StartsWith("$2b$", StringComparison.Ordinal)
  15. || hash.StartsWith("$2y$", StringComparison.Ordinal);
  16. }
  17. public bool Verify(string password, string legacyHash)
  18. {
  19. try
  20. {
  21. return BCrypt.Net.BCrypt.Verify(password, legacyHash);
  22. }
  23. catch
  24. {
  25. // 손상되었거나 형식이 맞지 않는 해시는 검증 실패로 처리
  26. return false;
  27. }
  28. }
  29. }