<commit_before>package org.jgrades.lic.app.service.crypto.encrypt;

import org.apache.commons.io.IOUtils;
import org.jgrades.lic.api.model.Licence;
import org.jgrades.lic.api.service.LicenceMarshallingFactory;
import org.jgrades.lic.app.service.crypto.KeyStoreContentExtractor;
import org.jgrades.lic.app.utils.LicConstants;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class LicenceEncryptionProvider {
    private final KeyStoreContentExtractor extractor;

    public LicenceEncryptionProvider(KeyStoreContentExtractor extractor) {
        this.extractor = extractor;
    }

    public byte[] encrypt(Licence licence) {
        byte[] licXmlBytes = transformToBytesArray(licence);
        return encrypt(licXmlBytes);
    }

    private byte[] transformToBytesArray(Licence licence) {
        Marshaller marshaller = LicenceMarshallingFactory.getMarshaller();
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            marshaller.marshal(licence, os);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return os.toByteArray();
    }

    private byte[] encrypt(byte[] licXmlBytes) {
        try {
            Cipher cipher = Cipher.getInstance(LicConstants.CIPHER_PROVIDER_INTERFACE);
            SecretKeySpec privKey = extractor.getPrivateKeyForEncryptionAndDecryption();
            cipher.init(Cipher.ENCRYPT_MODE, privKey);
            return getEncryptedOutputStream(licXmlBytes, cipher).toByteArray();
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {
            e.printStackTrace();<commit_msg>trying to fix test on CI<commit_after>package org.jgrades.lic.app.service.crypto.encrypt;

import org.apache.commons.io.IOUtils;
import org.jgrades.lic.api.model.Licence;
import org.jgrades.lic.api.service.LicenceMarshallingFactory;
import org.jgrades.lic.app.service.crypto.KeyStoreContentExtractor;
import org.jgrades.lic.app.utils.LicConstants;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Locale;

public class LicenceEncryptionProvider {
    private final KeyStoreContentExtractor extractor;

    public LicenceEncryptionProvider(KeyStoreContentExtractor extractor) {
        this.extractor = extractor;
    }

    public byte[] encrypt(Licence licence) {
        byte[] licXmlBytes = transformToBytesArray(licence);
        System.out.println("licxml:" + Arrays.toString(licXmlBytes));
        System.out.println(Charset.defaultCharset() + " " + Locale.getDefault());
        return encrypt(licXmlBytes);
    }

    private byte[] transformToBytesArray(Licence licence) {
        Marshaller marshaller = LicenceMarshallingFactory.getMarshaller();
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            marshaller.marshal(licence, os);
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return os.toByteArray();
    }

    private byte[] encrypt(byte[] licXmlBytes) {
        try {
            Cipher cipher = Cipher.getInstance(LicConstants.CIPHER_PROVIDER_INTERFACE);
            SecretKeySpec privKey = extractor.getPrivateKeyForEncryptionAndDecryption();
            cipher.init(Cipher.ENCRYPT_MODE, privKey);
            return getEncryptedOutputStream(licXmlBytes, cipher).toByteArray();
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException e) {
            e.printStackTrace();
{'fraction_non_alphanumeric': 0.09175405147759771, 'fraction_numerical': 0.0, 'mean_word_length': 3.9551357733175916, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1700827', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1160, 'n_words': 206}