<commit_before>(function() {
    'use strict';

    /**
     * Crypto batch library for processing large sets of data
     */
    var CryptoBatch = function(aes, rsa, util, _) {
        this._aes = aes;
        this._rsa = rsa;
        this._util = util;
        this.__ = _;
    };

    //
    // Encrypt batch for user AES/RSA
    //

    /**
     * Encrypt and sign a list of items using AES and RSA
     * @param list [Array] The list of items to encrypt
     * @param receiverPubkeys [Array] A list of public keys used to encrypt
     * @param senderPrivkey [Array] The sender's private key used to sign
     */
    CryptoBatch.prototype.encryptListForUser = function(list, receiverPubkeys, senderPrivkey) {
        var receiverPk,
            self = this;

        // validate presence of args
        if (!list || !receiverPubkeys || !senderPrivkey || !senderPrivkey._id || !senderPrivkey.privateKey) {
            throw new Error('Arguments missing!');
        }

        // encrypt a list of items
        self.encryptList(list);

        // set sender private key
        self._rsa.init(null, senderPrivkey.privateKey);

        list.forEach(function(i) {
            // fetch correct public key for encryption
            receiverPk = null;
            receiverPk = self.__.findWhere(receiverPubkeys, {
                _id: i.receiverPk
            });

            // validate presence of args
            if (!receiverPk || !receiverPk.publicKey || !i.key || !i.iv || !i.ciphertext) {
                throw new Error('Arguments missing!');
            }

            // encrypt item for user
            self.encryptItemKeyForUser(i, receiverPk.publicKey, senderPrivkey._id);
        });

        return list;
    };

    /**
     * Encrypt and sign an item using AES and RSA
     * @param i [Object] The item to encrypt
     * @param receiverPubkey [String] The public key used to encrypt
     * @param senderKeyId [String] The sender's private key ID used to sign
     */
    CryptoBatch.prototype.encryptItemKeyForUser = function(i, receiverPubkey, senderKeyId) {
        var self = this;

        // set rsa public key used to encrypt
        self._rsa.init(receiverPubkey);
        // encrypt symmetric item key for user
        i.encryptedKey = self._rsa.encrypt(i.key);
        // set sender's keypair id for later verification
        i.senderPk = senderKeyId;
        // sign the bundle
        i.signature = self._rsa.sign([i.iv, i.ciphertext]);

        // delete plaintext values
        delete i.key;
        delete i.receiverPk;

        return i;
    };

    //
    // Decrypt batch for user AES/RSA
    //

    /**
     * Decrypt and verify a list of items using AES and RSA
     * @param list [Array] The list of items to decrypt
     * @param senderPubkeys [Array] A list of public keys used to verify
     * @param receiverPrivkey [Array] The receiver's private key used to decrypt
     */
    CryptoBatch.prototype.decryptListForUser = function(list, senderPubkeys, receiverPrivkey) {
        var j;

        // validate presence of args
        if (!list || !senderPubkeys || !receiverPrivkey || !receiverPrivkey._id || !receiverPrivkey.privateKey) {
            throw new Error('Arguments missing!');
        }

        // verify and decrypt a list of items using RSA
        this.decryptListKeysForUser(list, senderPubkeys, receiverPrivkey);

        // decrypt a list of items
        this.decryptList(list);

        // set plaintext as list item
        for (j = 0; j < list.length; j++) {
            list[j] = list[j].plaintext;
        }

        return list;
    };

    /**
     * Decrypt and verify a list of item keys using RSA
     * @param list [Array] The list of items to decrypt
     * @param senderPubkeys [Array] A list of public keys used to verify
     * @param receiverPrivkey [String] The receiver's private key used to decrypt
     */
    CryptoBatch.prototype.decryptListKeysForUser = function(list, senderPubkeys, receiverPrivkey) {
        var self = this,
            senderPk;

        // set receiver private key
        self._rsa.init(null, receiverPrivkey.privateKey);

        list.forEach(function(i) {
            // fetch correct public key for verification
            senderPk = null;
            senderPk = self.__.findWhere(senderPubkeys, {
                _id: i.senderPk
            });

            // validate presence of args
            if (!senderPk || !senderPk.publicKey || !i.encryptedKey || !i.iv || !i.ciphertext) {
                throw new Error('Arguments missing!');
            }

            // decrypt item for user
            self.decryptItemKeyForUser(i, senderPk.publicKey);
        });

        return list;
    };

    /**
     * Verfiy an item and decrypt its item key using RSA
     * @param i [Object] The item to decrypt
     * @param senderPubkey [String] A public key used to verify
     */
    CryptoBatch.prototype.decryptItemKeyForUser = function(i, senderPubkey) {
        var self = this;

        // set rsa public key used to verify
        self._rsa.init(senderPubkey);

        // verify signature
        if (!self._rsa.verify([i.iv, i.ciphertext], i.signature)) {
            throw new Error('Verifying RSA signature failed!');
        }
        // decrypt symmetric item key for user
        i.key = self._rsa.decrypt(i.encryptedKey);

        // delete ciphertext values
        delete i.signature;
        delete i.encryptedKey;
        delete i.senderPk;<commit_msg>integrate crypto lib update<commit_after>(function() {
    'use strict';

    /**
     * Crypto batch library for processing large sets of data
     */
    var CryptoBatch = function(aes, rsa, util, _) {
        this._aes = aes;
        this._rsa = rsa;
        this._util = util;
        this.__ = _;
    };

    //
    // Encrypt batch for user AES/RSA
    //

    /**
     * Encrypt and sign a list of items using AES and RSA
     * @param list [Array] The list of items to encrypt
     * @param receiverPubkeys [Array] A list of public keys used to encrypt
     * @param senderPrivkey [Array] The sender's private key used to sign
     */
    CryptoBatch.prototype.encryptListForUser = function(list, receiverPubkeys, senderPrivkey) {
        var receiverPk,
            self = this;

        // validate presence of args
        if (!list || !receiverPubkeys || !senderPrivkey || !senderPrivkey._id || !senderPrivkey.privateKey) {
            throw new Error('Arguments missing!');
        }

        // set sender private key
        self._rsa.init(null, senderPrivkey.privateKey);

        // encrypt a list of items
        self.encryptList(list);

        list.forEach(function(i) {
            // fetch correct public key for encryption
            receiverPk = null;
            receiverPk = self.__.findWhere(receiverPubkeys, {
                _id: i.receiverPk
            });

            // validate presence of args
            if (!receiverPk || !receiverPk.publicKey || !i.key || !i.iv || !i.ciphertext) {
                throw new Error('Arguments missing!');
            }

            // encrypt item for user
            self.encryptItemKeyForUser(i, receiverPk.publicKey, senderPrivkey._id);
        });

        return list;
    };

    /**
     * Encrypt and sign an item using AES and RSA
     * @param i [Object] The item to encrypt
     * @param receiverPubkey [String] The public key used to encrypt
     * @param senderKeyId [String] The sender's private key ID used to sign
     */
    CryptoBatch.prototype.encryptItemKeyForUser = function(i, receiverPubkey, senderKeyId) {
        var self = this;

        // set rsa public key used to encrypt
        self._rsa.init(receiverPubkey);
        // encrypt symmetric item key for user
        i.encryptedKey = self._rsa.encrypt(i.key);
        // set sender's keypair id for later verification
        i.senderPk = senderKeyId;
        // sign the bundle
        i.signature = self._rsa.sign([i.iv, i.key, i.ciphertext]);

        // delete plaintext values
        delete i.key;
        delete i.receiverPk;

        return i;
    };

    //
    // Decrypt batch for user AES/RSA
    //

    /**
     * Decrypt and verify a list of items using AES and RSA
     * @param list [Array] The list of items to decrypt
     * @param senderPubkeys [Array] A list of public keys used to verify
     * @param receiverPrivkey [Array] The receiver's private key used to decrypt
     */
    CryptoBatch.prototype.decryptListForUser = function(list, senderPubkeys, receiverPrivkey) {
        var j;

        // validate presence of args
        if (!list || !senderPubkeys || !receiverPrivkey || !receiverPrivkey._id || !receiverPrivkey.privateKey) {
            throw new Error('Arguments missing!');
        }

        // verify and decrypt a list of items using RSA
        this.decryptListKeysForUser(list, senderPubkeys, receiverPrivkey);

        // decrypt a list of items
        this.decryptList(list);

        // set plaintext as list item
        for (j = 0; j < list.length; j++) {
            list[j] = list[j].plaintext;
        }

        return list;
    };

    /**
     * Decrypt and verify a list of item keys using RSA
     * @param list [Array] The list of items to decrypt
     * @param senderPubkeys [Array] A list of public keys used to verify
     * @param receiverPrivkey [String] The receiver's private key used to decrypt
     */
    CryptoBatch.prototype.decryptListKeysForUser = function(list, senderPubkeys, receiverPrivkey) {
        var self = this,
            senderPk;

        // set receiver private key
        self._rsa.init(null, receiverPrivkey.privateKey);

        list.forEach(function(i) {
            // fetch correct public key for verification
            senderPk = null;
            senderPk = self.__.findWhere(senderPubkeys, {
                _id: i.senderPk
            });

            // validate presence of args
            if (!senderPk || !senderPk.publicKey || !i.encryptedKey || !i.iv || !i.ciphertext) {
                throw new Error('Arguments missing!');
            }

            // decrypt item for user
            self.decryptItemKeyForUser(i, senderPk.publicKey);
        });

        return list;
    };

    /**
     * Verfiy an item and decrypt its item key using RSA
     * @param i [Object] The item to decrypt
     * @param senderPubkey [String] A public key used to verify
     */
    CryptoBatch.prototype.decryptItemKeyForUser = function(i, senderPubkey) {
        var self = this;

        // set rsa public key used to verify
        self._rsa.init(senderPubkey);

        // decrypt symmetric item key for user
        i.key = self._rsa.decrypt(i.encryptedKey);

        // verify signature
        if (!self._rsa.verify([i.iv, i.key, i.ciphertext], i.signature)) {
            throw new Error('Verifying RSA signature failed!');
        }

        // delete ciphertext values
        delete i.signature;
        delete i.encryptedKey;
        delete i.senderPk;
{'fraction_non_alphanumeric': 0.0965366784837742, 'fraction_numerical': 0.000181801654395055, 'mean_word_length': 1.98561736770692, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4501147', 'n_tokens_mistral': 3182, 'n_tokens_neox': 3041, 'n_words': 964}