<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2015 LCI Technology Group, LLC
# All rights reserved

import requests
import pysodium
import os
import base64
import json
import cmd


# CONSTANTS
HOMEDIR = os.path.expanduser("~")
ZKMDIR = os.path.join(HOMEDIR, '.zkm')
CONFIG = os.path.join(ZKMDIR, 'config')
CONTACT = os.path.join(ZKMDIR, 'contacts')


def load_data(filename):
    """
    Load a key:value file into a dictionary and return it.
    """
    data = {}
    with open(filename, 'rb') as f:
        for line in f:
            line = line.rstrip(b'\n')
            k, v = line.split(b'|')
            data[k] = v

    return data


def save_data(filename, data):
    """
    Save the given data to a file in key:value format.
    """
    with open(filename, 'wb') as f:
        for k, v in data.items():
            f.write(b'|'.join([k, v]))
            f.write(b'\n')


def encrypt(ssk, spk, rpk, msg):
    """
    Encrypt a message using the provided information.
    """
    ssk = base64.b64decode(ssk)
    rpk = base64.b64decode(rpk)
    nonce = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES)

    enc = pysodium.crypto_box_easy(msg, nonce, rpk, ssk)

    nonce = base64.b64encode(nonce)
    enc = base64.b64encode(enc)

    # Return sender's public_key, nonce, and the encrypted message
    return b':'.join([spk, nonce, enc])


def decrypt(rsk, msg):
    """
    Decrypt a message using the provided information.
    """
    spk, nonce, enc_msg = msg.split(b':')

    spk = base64.b64decode(spk)
    rsk = base64.b64decode(rsk)
    nonce = base64.b64decode(nonce)
    enc_msg = base64.b64decode(enc_msg)

    dec_msg = pysodium.crypto_box_open_easy(enc_msg, nonce, spk, rsk)
    print(dec_msg.decode())

    # Return the sender's public key and the decrypted message.
    return spk, dec_msg


def print_msg(contacts, their_public, msg):
    """
    Print the sender and message.

    Lookup the public key of the sender to see if they are in our
    contacts. If they are print the username, if not print the public key.
    """
    for username, contact_public in contacts.iteritems():
        if their_public == contact_public:
            sender = username
        else:
            sender = their_public

    print('{0}'.format(sender))
    print('-' * len(sender))
    print(msg)


def send(server, method, endpoint, data=None):
    """
    Send a message to the server and process the response.
    """
    url = '{0}{1}'.format(server, endpoint)
    resp = None

    if method == 'POST':
        resp = requests.post(url, data=data)
    else:
        resp = requests.get(url, params=data)

    if resp.status_code == 200:
        j = resp.json()
        if j['error'] is not None:
            print('[-] {0}'.format(j['error']))
            return None
        else:
            return j['response']
    else:
        print('[-] Server error: {0}'.format(resp.status_code))


def initialize():
    """
    Create a ~/.zkm directory with a config file inside.

    The config file will hold our public key, secret key, and since value.
    """
    if os.path.exists(ZKMDIR) is False:
        print('[+] Creating ZKM configuration directory.')
        os.mkdir(ZKMDIR, 0o750)
<commit_msg>Requests wants a string and not bytes.<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2015 LCI Technology Group, LLC
# All rights reserved

import requests
import pysodium
import os
import base64
import json
import cmd


# CONSTANTS
HOMEDIR = os.path.expanduser("~")
ZKMDIR = os.path.join(HOMEDIR, '.zkm')
CONFIG = os.path.join(ZKMDIR, 'config')
CONTACT = os.path.join(ZKMDIR, 'contacts')


def load_data(filename):
    """
    Load a key:value file into a dictionary and return it.
    """
    data = {}
    with open(filename, 'rb') as f:
        for line in f:
            line = line.rstrip(b'\n')
            k, v = line.split(b'|')
            data[k] = v

    return data


def save_data(filename, data):
    """
    Save the given data to a file in key:value format.
    """
    with open(filename, 'wb') as f:
        for k, v in data.items():
            f.write(b'|'.join([k, v]))
            f.write(b'\n')


def encrypt(ssk, spk, rpk, msg):
    """
    Encrypt a message using the provided information.
    """
    ssk = base64.b64decode(ssk)
    rpk = base64.b64decode(rpk)
    nonce = pysodium.randombytes(pysodium.crypto_box_NONCEBYTES)

    enc = pysodium.crypto_box_easy(msg, nonce, rpk, ssk)

    nonce = base64.b64encode(nonce)
    enc = base64.b64encode(enc)

    # Return sender's public_key, nonce, and the encrypted message
    return b':'.join([spk, nonce, enc])


def decrypt(rsk, msg):
    """
    Decrypt a message using the provided information.
    """
    spk, nonce, enc_msg = msg.split(b':')

    spk = base64.b64decode(spk)
    rsk = base64.b64decode(rsk)
    nonce = base64.b64decode(nonce)
    enc_msg = base64.b64decode(enc_msg)

    dec_msg = pysodium.crypto_box_open_easy(enc_msg, nonce, spk, rsk)
    print(dec_msg.decode())

    # Return the sender's public key and the decrypted message.
    return spk, dec_msg


def print_msg(contacts, their_public, msg):
    """
    Print the sender and message.

    Lookup the public key of the sender to see if they are in our
    contacts. If they are print the username, if not print the public key.
    """
    for username, contact_public in contacts.iteritems():
        if their_public == contact_public:
            sender = username
        else:
            sender = their_public

    print('{0}'.format(sender))
    print('-' * len(sender))
    print(msg)


def send(server, method, endpoint, data=None):
    """
    Send a message to the server and process the response.
    """
    url = '{0}{1}'.format(server, endpoint)
    url = url.decode('utf8')  # Requests wants a string not bytes.
    resp = None

    if method == 'POST':
        resp = requests.post(url, data=data)
    else:
        resp = requests.get(url, params=data)

    if resp.status_code == 200:
        j = resp.json()
        if j['error'] is not None:
            print('[-] {0}'.format(j['error']))
            return None
        else:
            return j['response']
    else:
        print('[-] Server error: {0}'.format(resp.status_code))


def initialize():
    """
    Create a ~/.zkm directory with a config file inside.

    The config file will hold our public key, secret key, and since value.
    """
    if os.path.exists(ZKMDIR) is False:
        print('[+] Creating ZKM configuration directory.')
        os.mkdir(ZKMDIR, 0o750)

{'fraction_non_alphanumeric': 0.12682260024301337, 'fraction_numerical': 0.01594775212636695, 'mean_word_length': 2.5807504078303425, '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': '159960', 'n_tokens_mistral': 2296, 'n_tokens_neox': 2178, 'n_words': 640}