<commit_before>// 'use strict';

const crypto = require('crypto');

/**
 * Generates a pseudo-random string of 40
 * characters of length.
 * 
 * example
 *    hzn5f4cdr4nco5p54288y3nmi1611nopkvivouuvhtlqnf80k9
 */
module.exports.generateSeed = function () {
    const timestamp = (new Date()).getTime().toString(36);

    const randomString = () => {
        return (Math.random() * 10000000000000000).toString(36).replace('.', '');
    };

    let token = `${timestamp}${randomString()}${randomString()}${randomString()}`;

    return (token + '00000')
        .slice(-40)
        .substring(0, 40);
};

/**
 * Hashes a given string using the crypto package.
 * Default algorithm is **aes-256-ctr**.
 * 
 * @param {String} text Source text to be encrypted
 * @param {String} secret Salt used for hashing
 * @param {String} [algorithm=aes-256-ctr] Valid cipher algorithm
 */
module.exports.encrypt = function $encrypt(text, secret, algorithm = 'aes-256-ctr') {
    const cipher = crypto.createCipher(algorithm, secret);
    let crypted = cipher.update(text, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
};

/**
 * Returns a string from a hash.
 * 
 * Default algorithm is **aes-256-ctr**.
 * 
 * @param {String} text Encrypted text to be cleared
 * @param {String} secret Salt used for hashing
 * @param {String} [algorithm=aes-256-ctr] Valid cipher algorithm
 */
module.exports.decrypt = function $decrypt(text, secret, algorithm = 'aes-256-ctr') {
    const decipher = crypto.createDecipher(algorithm, secret);
    let dec = decipher.update(text, 'hex', 'utf8');
    dec += decipher.final('utf8');
    return dec;
};

const COMMANDS = {
    AUTHORIZE: 'AUTH',
    EXIT: 'EXT',
    CONNECT: 'CNT',
    END_CONECT: 'TNC',
    UNDEFINED: 'UNDEFINED',
    DIRTY_CONTEXT: 'DTC'
};

module.exports.COMMANDS = COMMANDS;

/**
 * Parses a string and into a command object.
 * 
 * @param {Buffer} data Data buffer from socket
 * @return {Object}
 */
module.exports.parseCommand = function (data) {
    if (!data) return {};

    data = (data.toString() || COMMANDS.UNDEFINED);

    const parts = data.split(' ');
    const cmd = parts.shift();
    const args = parts.concat();

    const out = {
        args: args,
        label: cmd,
        getSeed: function () {
            return args[0];
        },
        getErrorMessage: function (verbose) {
            return verbose ? args[0] : 'exit';
        }
    };

    return out;
};

/**
 * NOTE: This prevents from using "use strict".
 * 
 * @param {String} title 
 */
module.exports.setTerminalTitle = function (title) {
    // process.stdout.write('\033]0;' + title + '\007');
    process.stdout.write('\033]2;' + title + '\033\\');
};

//ansi:
//https://github.com/jonschlinkert/ansi-wrap

const ansi = {};

ansi.wrapAnsi = function (a, b, msg) {
    return '\u001b[' + a + 'm' + msg + '\u001b[' + b + 'm';
};

ansi.magenta = function (msg) {
    return ansi.wrapAnsi(35, 39, msg);
};

ansi.cyan = function (msg) {
    return ansi.wrapAnsi(36, 39, msg);
};

ansi.yellow = function (msg) {
    return ansi.wrapAnsi(33, 39, msg);
};

ansi.bold = function (msg) {
    return ansi.wrapAnsi(1, 22, msg);
};

ansi.color = function (acolor, msg) {
    var color = ansi[acolor] || 'magenta';
    return color(msg);
};

ansi.clear = function () {
    return '\u001b[2J\u001b[;H';
};

module.exports.ansi = ansi;<commit_msg>update strings for utils ansi<commit_after>// 'use strict';

const crypto = require('crypto');

/**
 * Generates a pseudo-random string of 40
 * characters of length.
 * 
 * example
 *    hzn5f4cdr4nco5p54288y3nmi1611nopkvivouuvhtlqnf80k9
 */
module.exports.generateSeed = function () {
    const timestamp = (new Date()).getTime().toString(36);

    const randomString = () => {
        return (Math.random() * 10000000000000000).toString(36).replace('.', '');
    };

    let token = `${timestamp}${randomString()}${randomString()}${randomString()}`;

    return (token + '00000')
        .slice(-40)
        .substring(0, 40);
};

/**
 * Generate a unique ID
 */
module.exports.getGuid = function () {
    const timestamp = (new Date()).getTime().toString(36);

    const randomString = () => {
        return (Math.random() * 10000000000000000).toString(36).replace('.', '');
    };

    return `${timestamp}-${randomString()}`;
};

/**
 * Hashes a given string using the crypto package.
 * Default algorithm is **aes-256-ctr**.
 * 
 * @param {String} text Source text to be encrypted
 * @param {String} secret Salt used for hashing
 * @param {String} [algorithm=aes-256-ctr] Valid cipher algorithm
 */
module.exports.encrypt = function $encrypt(text, secret, algorithm = 'aes-256-ctr') {
    const cipher = crypto.createCipher(algorithm, secret);
    let crypted = cipher.update(text, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
};

/**
 * Returns a string from a hash.
 * 
 * Default algorithm is **aes-256-ctr**.
 * 
 * @param {String} text Encrypted text to be cleared
 * @param {String} secret Salt used for hashing
 * @param {String} [algorithm=aes-256-ctr] Valid cipher algorithm
 */
module.exports.decrypt = function $decrypt(text, secret, algorithm = 'aes-256-ctr') {
    const decipher = crypto.createDecipher(algorithm, secret);
    let dec = decipher.update(text, 'hex', 'utf8');
    dec += decipher.final('utf8');
    return dec;
};

const COMMANDS = {
    AUTHORIZE: 'AUTH',
    EXIT: 'EXT',
    CONNECT: 'CNT',
    END_CONECT: 'TNC',
    UNDEFINED: 'UNDEFINED',
    DIRTY_CONTEXT: 'DTC'
};

module.exports.COMMANDS = COMMANDS;

/**
 * Parses a string and into a command object.
 * 
 * @param {Buffer} data Data buffer from socket
 * @return {Object}
 */
module.exports.parseCommand = function (data) {
    if (!data) return {};

    data = (data.toString() || COMMANDS.UNDEFINED);

    const parts = data.split(' ');
    const cmd = parts.shift();
    const args = parts.concat();

    const out = {
        args: args,
        label: cmd,
        getSeed: function () {
            return args[0];
        },
        getErrorMessage: function (verbose) {
            return verbose ? args[0] : 'exit';
        }
    };

    return out;
};

/**
 * NOTE: This prevents from using "use strict".
 * 
 * @param {String} title 
 */
module.exports.setTerminalTitle = function (title) {
    // process.stdout.write('\033]0;' + title + '\007');
    process.stdout.write('\033]2;' + title + '\033\\');
};

const ansi = {};

ansi.wrap = function (a, b, msg) {
    return '\u001b[' + a + 'm' + msg + '\u001b[' + b + 'm';
};

ansi.magenta = function (msg) {
    return ansi.wrap(35, 39, msg);
};

ansi.red = function(msg) {
    return ansi.wrap(31, 39, msg);
};

ansi.cyan = function (msg) {
    return ansi.wrap(36, 39, msg);
};

ansi.yellow = function (msg) {
    return ansi.wrap(33, 39, msg);
};

ansi.bold = function (msg) {
    return ansi.wrap(1, 22, msg);
};

ansi.color = function (acolor, msg) {
    const color = ansi[acolor] || ansi.magenta;
    return color(msg);
};

ansi.clear = function () {
    return '\u001b[2J\u001b[;H';
};

ansi.inverse = function(msg){
    return ansi.wrap(7, 27, msg);
};

module.exports.ansi = ansi;
{'fraction_non_alphanumeric': 0.16969020373988278, 'fraction_numerical': 0.036003349148758024, 'mean_word_length': 3.0860889395667046, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, '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': '4571879', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2411, 'n_words': 608}