package com.bsmlab.dfx.dfxconsole.app.agent.service;

import com.bsmlab.dfx.dfxconsole.framework.dto.SearchParameterDto;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.springframework.stereotype.Service;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.security.Security;
import java.util.List;

@Service
@Slf4j
@RequiredArgsConstructor
public class DfxCryptoKeyService {
    private final DfxCryptoKeyMapper cryptoKeyMapper;

    public List<DfxCryptoKeyDto> selectDfxCryptoKeyList() {
        List<DfxCryptoKeyDto> dfxCryptoKeyDtoList = cryptoKeyMapper.selectDfxCryptoKeyList(SearchParameterDto.builder().build());
        return dfxCryptoKeyDtoList;
    }

    public DfxCryptoKeyDto generateNewKey() throws NoSuchAlgorithmException, NoSuchProviderException {
        DfxCryptoKeyDto dfxCryptoKeyDto = DfxCryptoKeyDto.builder().build();
        Security.addProvider(new BouncyCastleProvider());
        KeyGenerator keyGenerator = KeyGenerator.getInstance("ARIA", "BC");
        keyGenerator.init(256, new SecureRandom());
        SecretKey key = keyGenerator.generateKey();

        return dfxCryptoKeyDto;
    }
}
