<commit_before>package solutions.oneguard.impassable.core.util;

import org.uncommons.maths.random.AESCounterRNG;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.util.Random;

public class AESCryptoUtil {
    private static final String cipherDescription = "AES/CBC/PKCS5PADDING";
    private static final String cipher = "AES";
    private static final int ivLength = 16;

    private static Random random;

    static {
        try {
            random = new AESCounterRNG();
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
    }

    public static Key generateKey() {
        try {
            int keyLength = Cipher.getMaxAllowedKeyLength(cipherDescription);

            return CryptoUtil.generateKey(cipher, keyLength > 32 ? 32 : keyLength); // max 256 bits
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] encrypt(Key secret, byte[] raw) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        byte[] initVector = new byte[ivLength];
        random.nextBytes(initVector);

        byte[] encrypted = CryptoUtil.encrypt(cipherDescription, secret, initVector, raw);

        byte[] encryptedIVAndText = new byte[initVector.length + encrypted.length];
        System.arraycopy(initVector, 0, encryptedIVAndText, 0, initVector.length);
        System.arraycopy(encrypted, 0, encryptedIVAndText, initVector.length, encrypted.length);

        return encryptedIVAndText;
    }

    public static byte[] decrypt(Key secret, byte[] encrypted) throws IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
        byte[] initVector = new byte[ivLength];
        System.arraycopy(encrypted, 0, initVector, 0, initVector.length);
<commit_msg>fixed throwing exceptions that can never occur<commit_after>package solutions.oneguard.impassable.core.util;

import org.uncommons.maths.random.AESCounterRNG;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.util.Random;

public class AESCryptoUtil {
    private static final String cipherDescription = "AES/CBC/PKCS5PADDING";
    private static final String cipher = "AES";
    private static final int ivLength = 16;

    private static Random random;

    static {
        try {
            random = new AESCounterRNG();
        } catch (GeneralSecurityException e) {
            throw new RuntimeException(e);
        }
    }

    public static Key generateKey() {
        try {
            int keyLength = Cipher.getMaxAllowedKeyLength(cipherDescription);

            return CryptoUtil.generateKey(cipher, keyLength > 32 ? 32 : keyLength); // max 256 bits
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] encrypt(Key secret, byte[] raw) throws IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
        byte[] initVector = new byte[ivLength];
        random.nextBytes(initVector);

        byte[] encrypted;
        try {
            encrypted = CryptoUtil.encrypt(cipherDescription, secret, initVector, raw);
        } catch (NoSuchPaddingException|NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        byte[] encryptedIVAndText = new byte[initVector.length + encrypted.length];
        System.arraycopy(initVector, 0, encryptedIVAndText, 0, initVector.length);
        System.arraycopy(encrypted, 0, encryptedIVAndText, initVector.length, encrypted.length);

        return encryptedIVAndText;
    }

    public static byte[] decrypt(Key secret, byte[] encrypted) throws IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeyException {
        byte[] initVector = new byte[ivLength];
        System.arraycopy(encrypted, 0, initVector, 0, initVector.length);

{'fraction_non_alphanumeric': 0.0845103033109516, 'fraction_numerical': 0.00694605232692753, 'mean_word_length': 3.5617740232312567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '377328', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1090, 'n_words': 288}