package com.vctgo.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

@Configuration
public class AsymmetricEncryptionConfig {

    @Bean
    public KeyPair keyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }

    @Bean
    public String publicKey(KeyPair keyPair) {
        return new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded()));
    }

    @Bean
    public String privateKey(KeyPair keyPair) {
        return new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded()));
    }
}