<commit_before>define('soteria.crypto-service-loader', ['require', 'jquery', 'cookies', 'forge.min', 'pako', 'src/utils', 'src/password-crypto', 'src/rsa-crypto', 'src/aes-crypto'], function(require) {
    'use strict';
    var jquery = require('jquery'),
        Cookies = require('cookies'),
        Forge = require('forge.min'),
        Pako = require('pako'),
        PasswordCryptoService = require('src/password-crypto'),
        RsaCryptoService = require('src/rsa-crypto'),
        AesCryptoService = require('src/aes-crypto'),
        SecurityUtils = require('src/utils');

    var BASE_URL = 'http://localhost:8081/v1',
        DIR_URL = '/directory',
        PUB_URL = '/public',
        PRIV_URL = '/private',
        OBJ_URL = '/object',
        INT_SIZE = 4;

    function CryptoServiceLoader(password) {
        if (!(this instanceof CryptoServiceLoader)) {
            throw new TypeError("CryptoServiceLoader constructor cannot be called as a function.");
        }
        this.passwordCryptoService = new PasswordCryptoService(password);
    };

    CryptoServiceLoader.prototype = {
        constructor: CryptoServiceLoader
    };

    CryptoServiceLoader.prototype.getPasswordCryptoService = function() {
        return this.passwordCryptoService;
    };

    CryptoServiceLoader.prototype.getRsaCryptoService = function() {
        var deferred = new jquery.Deferred();
        if (typeof this.rsaCryptoService === 'undefined') {
            loadRsaKeys.call(this).then(function(keypair) {
                this.rsaCryptoService = new RsaCryptoService(keypair.privateKey, keypair.publicKey);
                deferred.resolve(this.rsaCryptoService);
            }.bind(this));
        } else {
            deferred.resolve(this.rsaCryptoService);
        }
        return deferred.promise();
    };

    CryptoServiceLoader.prototype.getObjectCryptoService = function(id) {
        var deferred = new jquery.Deferred();
        var privateKey;
        var cryptoServiceResponse;
        jquery.when(
            this.getRsaCryptoService(),
            loadCryptoService(id)
        ).then(function(rsaCryptoService, cryptoServiceResponse) {
            var deflatedCryptoService = rsaCryptoService.decrypt(atob(cryptoServiceResponse[0].data));
            var buffer = Forge.util.createBuffer(deflatedCryptoService, 'raw');
            buffer.getBytes(INT_SIZE); // remove the prepended length integer
            var compBytes = buffer.getBytes(buffer.length());
            var decompressedCryptoService = JSON.parse(Pako.inflate(compBytes, {
                to: 'string'
            })); // inflate crypto service
            var objectCryptoService = new AesCryptoService(atob(decompressedCryptoService.key)); // create AesCryptoService

            deferred.resolve(objectCryptoService);
        }.bind(this));
        return deferred.promise();
    };

    CryptoServiceLoader.prototype.setObjectCryptoService = function(id, cryptoService) {
        // TODO
    };

    // Helper functions
    function loadRsaKeys() {
        var deferred = new jquery.Deferred();
        var request = jquery.ajax(SecurityUtils.wrapRequest({
            url: BASE_URL + DIR_URL + PRIV_URL,
            type: 'GET'
        }));

        var resolveRsaKeys = function(blockCiphertext) {
            var privateKeyBytes  = this.getPasswordCryptoService().decrypt(blockCiphertext);
            var privateKeyBuffer = Forge.util.createBuffer(privateKeyBytes, 'raw');<commit_msg>fix bug in crypto service loader<commit_after>define('soteria.crypto-service-loader', [
    'require',
    'jquery',
    'cookies',
    'forge.min',
    'pako',
    'soteria.security-utils',
    'src/password-crypto',
    'src/rsa-crypto',
    'src/aes-crypto'
], function(require) {
    'use strict';
    var jquery                = require('jquery'),
        Cookies               = require('cookies'),
        Forge                 = require('forge.min'),
        Pako                  = require('pako'),
        PasswordCryptoService = require('src/password-crypto'),
        RsaCryptoService      = require('src/rsa-crypto'),
        AesCryptoService      = require('src/aes-crypto'),
        SecurityUtils         = require('soteria.security-utils');

    var BASE_URL = 'http://localhost:8081/v1',
        DIR_URL  = '/directory',
        PUB_URL  = '/public',
        PRIV_URL = '/private',
        OBJ_URL  = '/object',
        INT_SIZE = 4;

    function CryptoServiceLoader(password) {
        if (!(this instanceof CryptoServiceLoader)) {
            throw new TypeError("CryptoServiceLoader constructor cannot be called as a function.");
        }
        this.passwordCryptoService = new PasswordCryptoService(password);
    };

    CryptoServiceLoader.prototype = {
        constructor: CryptoServiceLoader
    };

    CryptoServiceLoader.prototype.getPasswordCryptoService = function() {
        return this.passwordCryptoService;
    };

    CryptoServiceLoader.prototype.getRsaCryptoService = function() {
        var deferred = new jquery.Deferred();
        if (typeof this.rsaCryptoService === 'undefined') {
            loadRsaKeys.call(this).then(function(keypair) {
                this.rsaCryptoService = new RsaCryptoService(keypair.privateKey, keypair.publicKey);
                deferred.resolve(this.rsaCryptoService);
            }.bind(this));
        } else {
            deferred.resolve(this.rsaCryptoService);
        }
        return deferred.promise();
    };

    CryptoServiceLoader.prototype.getObjectCryptoService = function(id) {
        var deferred = new jquery.Deferred();
        var privateKey;
        var cryptoServiceResponse;
        jquery.when(
            this.getRsaCryptoService(),
            loadCryptoService(id)
        ).then(function(rsaCryptoService, cryptoServiceResponse) {
            var deflatedCryptoService = rsaCryptoService.decrypt(atob(cryptoServiceResponse[0].data));
            var buffer = Forge.util.createBuffer(deflatedCryptoService, 'raw');
            buffer.getBytes(INT_SIZE); // remove the prepended length integer
            var compBytes = buffer.getBytes(buffer.length());
            var decompressedCryptoService = JSON.parse(Pako.inflate(compBytes, {
                to: 'string'
            })); // inflate crypto service
            var objectCryptoService = new AesCryptoService(
                decompressedCryptoService.cypher,
                atob(decompressedCryptoService.key)
            ); // create AesCryptoService

            deferred.resolve(objectCryptoService);
        }.bind(this));
        return deferred.promise();
    };

    CryptoServiceLoader.prototype.setObjectCryptoService = function(id, cryptoService) {
        // TODO
    };

    // Helper functions
    function loadRsaKeys() {
        var deferred = new jquery.Deferred();
        var request = jquery.ajax(SecurityUtils.wrapRequest({
            url: BASE_URL + DIR_URL + PRIV_URL,
            type: 'GET'
        }));

        var resolveRsaKeys = function(blockCiphertext) {
            var privateKeyBytes  = this.getPasswordCryptoService().decrypt(blockCiphertext);
            var privateKeyBuffer = Forge.util.createBuffer(privateKeyBytes, 'raw');
{'fraction_non_alphanumeric': 0.1117271971167175, 'fraction_numerical': 0.0019406709176601053, 'mean_word_length': 2.5212298682284042, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6321270', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1956, 'n_words': 348}