package com.lutao.cloud.common.security.password;

import com.lutao.cloud.common.security.util.SHAEncrypt;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.*;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;

import java.util.HashMap;
import java.util.Map;


public class PasswordEncoderSha implements PasswordEncoder{

    private final static String SALT_0 = "e&96b$329#0e%01*";

    private final static String SALT_1 = "d$d3135ba@ca20e7";

    @Override
    public String encode(CharSequence charSequence){

        return SHAEncrypt.SHA256(String.format("%s%s",SHAEncrypt.SHA256(String.format("%s%s",charSequence.toString(),SALT_0)),SALT_1));
    }

    @Override
    public boolean matches(CharSequence charSequence, String s){
        String digested = SHAEncrypt.SHA256(String.format("%s%s",SHAEncrypt.SHA256(String.format("%s%s",charSequence.toString(),SALT_0)),SALT_1));
        return digested.equals(s);
    }

    public static PasswordEncoder getInstance(){
        String encodingId = "bcrypt";
        Map<String, PasswordEncoder> encoders = new HashMap();
        encoders.put(encodingId, new BCryptPasswordEncoder());
        encoders.put("ldap", new LdapShaPasswordEncoder());
        encoders.put("MD4", new Md4PasswordEncoder());
        encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
        encoders.put("noop", NoOpPasswordEncoder.getInstance());
        encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
        encoders.put("scrypt", new SCryptPasswordEncoder());
        encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
        encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
        encoders.put("sha256", new StandardPasswordEncoder());
        encoders.put("argon2", new Argon2PasswordEncoder());

        // 注意这个标识和下文{cloud} 保持一致，这个就是自定义解析器的别名
        encoders.put("cloud", new PasswordEncoderSha());
        return new DelegatingPasswordEncoder(encodingId, encoders);
    }

    public static void main(String[] args){
//        System.out.println(new PasswordEncoderSha().encode("test"));
//        System.out.println(new PasswordEncoderSha().encode("cast:123456"));
//        System.out.println(new PasswordEncoderSha().encode("casst@castyy"));
//
//        System.out.println(new PasswordEncoderSha().encode("Aky#2023#aky"));
//        System.out.println("7777777----" + new BCryptPasswordEncoder().encode("test123"));
//        AES aes = new AES(Mode.CFB, Padding.NoPadding,
//                new SecretKeySpec("sdjurli%*iw18s^1".getBytes(StandardCharsets.UTF_8), "AES"),
//                new IvParameterSpec("sdjurli%*iw18s^1".getBytes(StandardCharsets.UTF_8)));
//
//        System.out.println("appKey: "+aes.encryptBase64("appKey".getBytes(StandardCharsets.UTF_8)));
//
//        System.out.println("casst: "+aes.encryptBase64("casst@castyy".getBytes(StandardCharsets.UTF_8)));
//        System.out.println("sdeeeee: "+aes.encryptBase64("test123".getBytes(StandardCharsets.UTF_8)));
//
//        System.out.println("--- " + aes.encryptBase64("Aky#2023#aky".getBytes(StandardCharsets.UTF_8)));
//
//        String code = "web:web";
//        String base = Base64.getEncoder().encodeToString(code.getBytes(StandardCharsets.UTF_8));
//        System.out.println(base);

//        String key = "wehyy%kwehdwdese";
//        AES aes = new AES(Mode.CFB, Padding.NoPadding,
//        new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"),
//        new IvParameterSpec(key.getBytes(StandardCharsets.UTF_8)));
//        System.out.println(aes.encryptBase64("1234567890"));
//
//        System.out.println(aes.decryptStr("pAGroBedfhz8Qw=="));
    }
}
