<commit_before>"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var crypto_js_1 = require("crypto-js");
var SimpleCrypto = /** @class */ (function () {
    function SimpleCrypto(secret) {
        if (secret === void 0)
            throw new Error("SimpleCrypto object MUST have a secret.");
        this.secret = secret;
        this.keySize = 256;
        this.iterations = 100;
    }
    SimpleCrypto.prototype.encrypt = function (string) {
        var salt = SimpleCrypto.generateRandom(128, true);
        var key = crypto_js_1.default.PBKDF2(this.secret, salt, {
            keySize: this.keySize / 32,
            iterations: this.iterations
        });
        var initialVector = SimpleCrypto.generateRandom(128, true);
        var encrypted = crypto_js_1.default.AES.encrypt(string, key, {
            iv: initialVector,
            padding: crypto_js_1.default.pad.Pkcs7,
            mode: crypto_js_1.default.mode.CBC
        });
        return salt.toString() + initialVector.toString() + encrypted.toString();
    };
    SimpleCrypto.prototype.decrypt = function (string, enc) {
        if (enc === void 0) { enc = crypto_js_1.default.enc.Utf8; }
        var salt = crypto_js_1.default.enc.Hex.parse(string.substr(0, 32));
        var initialVector = crypto_js_1.default.enc.Hex.parse(string.substr(32, 32));
        var encrypted = string.substring(64);
        var key = crypto_js_1.default.PBKDF2(this.secret, salt, {
            keySize: this.keySize / 32,
            iterations: this.iterations
        });
        var decrypted = crypto_js_1.default.AES.decrypt(encrypted, key, {
            iv: initialVector,
            padding: crypto_js_1.default.pad.Pkcs7,
            mode: crypto_js_1.default.mode.CBC
        });
        return decrypted.toString(enc);
    };
    SimpleCrypto.prototype.encryptObject = function (object) {
        return this.encrypt(JSON.stringify(object));
    };
    SimpleCrypto.prototype.decryptObject = function (string) {
        return JSON.parse(this.decrypt(string));
    };
    SimpleCrypto.generateRandom = function (length, wordArray) {
        if (length === void 0) { length = 128; }
        if (wordArray === void 0) { wordArray = false; }
        var random = crypto_js_1.default.lib.WordArray.random(length / 8);
        return (wordArray) ? random : random.toString();
    };
    return SimpleCrypto;
}());
exports.default = SimpleCrypto;<commit_msg>Upgrade to ES6<commit_after>import CryptoJS from 'crypto-js';
export default class SimpleCrypto {
    constructor(secret) {
        if (secret === void 0)
            throw new Error(`SimpleCrypto object MUST have a secret.`);
        this.secret = secret;
        this.keySize = 256;
        this.iterations = 100;
    }
    encrypt(string) {
        let salt = SimpleCrypto.generateRandom(128, true);
        let key = CryptoJS.PBKDF2(this.secret, salt, {
            keySize: this.keySize / 32,
            iterations: this.iterations
        });
        let initialVector = SimpleCrypto.generateRandom(128, true);
        let encrypted = CryptoJS.AES.encrypt(string, key, {
            iv: initialVector,
            padding: CryptoJS.pad.Pkcs7,
            mode: CryptoJS.mode.CBC
        });
        return salt.toString() + initialVector.toString() + encrypted.toString();
    }
    decrypt(string, enc = CryptoJS.enc.Utf8) {
        let salt = CryptoJS.enc.Hex.parse(string.substr(0, 32));
        let initialVector = CryptoJS.enc.Hex.parse(string.substr(32, 32));
        let encrypted = string.substring(64);
        let key = CryptoJS.PBKDF2(this.secret, salt, {
            keySize: this.keySize / 32,
            iterations: this.iterations
        });
        let decrypted = CryptoJS.AES.decrypt(encrypted, key, {
            iv: initialVector,
            padding: CryptoJS.pad.Pkcs7,
            mode: CryptoJS.mode.CBC
        });
        return decrypted.toString(enc);
    }
    encryptObject(object) {
        return this.encrypt(JSON.stringify(object));
    }
    decryptObject(string) {
        return JSON.parse(this.decrypt(string));
    }
    static generateRandom(length = 128, wordArray = false) {
        let random = CryptoJS.lib.WordArray.random(length / 8);
        return (wordArray) ? random : random.toString();
    }
}
{'fraction_non_alphanumeric': 0.12101466139166861, 'fraction_numerical': 0.020246683732836862, 'mean_word_length': 2.405705229793978, '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': '4058075', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1278, 'n_words': 261}