package com.nursinghome.util;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class PasswordUtil {
    private static final BCryptPasswordEncoder ENCODER = new BCryptPasswordEncoder();

    public static String hash(String raw) {
        return ENCODER.encode(raw);
    }

    public static boolean matches(String raw, String stored) {
        if (stored == null) {
            return false;
        }
        if (isBcrypt(stored)) {
            return ENCODER.matches(raw, stored);
        }
        return stored.equals(raw);
    }

    public static boolean isBcrypt(String stored) {
        return stored != null && stored.startsWith("$2");
    }
}
