package cn.novalon.gym.manage.sys.util;

import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import static org.junit.jupiter.api.Assertions.*;

public class PasswordHashGenerator {
    
    @Test
    public void generatePasswordHash() {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(12);
        
        String password = "Test@123";
        String hash = passwordEncoder.encode(password);
        
        System.out.println("========================================");
        System.out.println("密码: " + password);
        System.out.println("哈希: " + hash);
        System.out.println("========================================");
        
        boolean matches = passwordEncoder.matches(password, hash);
        System.out.println("验证结果: " + matches);
        
        String hash2b = "$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/X4.VTtYA/7.J6LlZy";
        boolean matches2b = passwordEncoder.matches(password, hash2b);
        System.out.println("验证$2b$哈希结果: " + matches2b);
    }
    
    @Test
    public void verifyBCryptVersions() {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(12);
        
        String password = "Test@123";
        
        // $2a$ hash (测试环境当前使用)
        String hash2a = "$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C";
        boolean matches2a = passwordEncoder.matches(password, hash2a);
        System.out.println("========================================");
        System.out.println("验证 $2a$ hash:");
        System.out.println("密码: " + password);
        System.out.println("Hash: " + hash2a);
        System.out.println("验证结果: " + matches2a);
        System.out.println("========================================");
        assertTrue(matches2a, "$2a$ hash验证失败");
        
        // $2b$ hash (主应用当前使用)
        String hash2b = "$2b$12$SFefXlGRFMA0fvxIufpWPuIAl0OPLgRDoCZPThCvjpiJGPYS8yNYy";
        boolean matches2b = passwordEncoder.matches("admin123", hash2b);
        System.out.println("验证 $2b$ hash:");
        System.out.println("密码: admin123");
        System.out.println("Hash: " + hash2b);
        System.out.println("验证结果: " + matches2b);
        System.out.println("========================================");
        assertTrue(matches2b, "$2b$ hash验证失败");
    }
    
    @Test
    public void verifyPasswordConsistency() {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(12);
        
        String password = "Test@123";
        String hash = "$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C";
        
        boolean matches = passwordEncoder.matches(password, hash);
        
        System.out.println("========================================");
        System.out.println("密码一致性验证:");
        System.out.println("明文密码: " + password);
        System.out.println("Hash: " + hash);
        System.out.println("验证结果: " + matches);
        System.out.println("========================================");
        
        assertTrue(matches, "密码配置不一致");
    }
}
