
package cd.casic.module.terminal.common;

import cd.casic.module.terminal.common.config.ConfigRef;
import cd.casic.module.terminal.common.config.ConfigStore;
import cd.casic.module.terminal.common.constant.ConfigKeys;
import cn.orionsec.kit.lang.utils.crypto.RSA;
import java.security.interfaces.RSAPrivateKey;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * rsa 加密器
 *
 * @author Jiahang Li
 * @version 1.0.0
 * @since 2025/1/7 11:32
 */
public class RsaDecryptorImpl implements RsaDecryptor {

    private static final String SPLIT = "\\|";

    private final ConfigRef<RSAPrivateKey> privateKey;

    public RsaDecryptorImpl(ConfigStore configStore) {
        this.privateKey = configStore.ref(ConfigKeys.ENCRYPT_PRIVATE_KEY, RSA::getPrivateKey);
    }

    @Override
    public String decrypt(String value) {
        return Arrays.stream(value.split(SPLIT))
                .map(s -> RSA.decrypt(s, privateKey.value))
                .collect(Collectors.joining());
    }

}
