<commit_before>#!/usr/bin/env python
# coding: utf8
"""
* Tox DNS Discovery Management Daemon - draft API server for Tox ID publishing.
* Since "Tox DNS Discovery Management Daemon" is a mouthful, just call it "yuu"
*
* Author: stal, stqism; April 2014
* Copyright (c) 2014 Zodiac Labs.
* You are free to do whatever you want with this file -- provided that this
* notice is retained.
"""
import requests
import sys
import time
import mmap
import binascii
import json
import nacl.encoding as crypto_encode
import nacl.public as crypto
import os
import random

def _compute_checksum(data, iv=(0, 0)):
    checksum = list(iv)
    for ind, byte in enumerate(binascii.unhexlify(data)):
        checksum[ind % 2] ^= byte
    return "".join(hex(byte)[2:].zfill(2) for byte in checksum).upper()

def _read_tox_cherry(open_file):
    m = mmap.mmap(open_file.fileno(), 0, prot=mmap.PROT_READ)
    idx = m.find(b"KEYs") + 8
    m.seek(idx)
    bs = m.read(68)
    
    pk = binascii.hexlify(bs[:32]).upper().decode("ascii")
    sk = binascii.hexlify(bs[32:64]).upper().decode("ascii")
    ns = binascii.hexlify(bs[64:]).upper().decode("ascii")

    m.close()
    return (pk, sk, ns)

def _read_tox_mono(open_file):
    m = mmap.mmap(open_file.fileno(), 0, prot=mmap.PROT_READ)
    m.seek(0x10)
    bs = m.read(68)

    pk = binascii.hexlify(bs[4:36]).upper().decode("ascii")
    sk = binascii.hexlify(bs[36:]).upper().decode("ascii")
    ns = binascii.hexlify(bs[:4]).upper().decode("ascii")

    m.close()
    return (pk, sk, ns)

def read_tox(fil):
    with open(fil, "rb") as file_:
        magic = file_.read(4)
        print(repr(magic))
        if magic == "桜\x00".encode("utf8"):
            return _read_tox_cherry(file_)
        elif magic == b"\x00\x00\x00\x00":
            return _read_tox_mono(file_)

def push(addr, name, fil):
    pkr = requests.get(addr + "/pk", verify=False)
    sk = pkr.json()["key"]
    
    mypub, mysec, nospam = read_tox(fil)
    check = _compute_checksum(mypub + nospam)
    print("kotone: Publishing {0}/{1} to server.".format(mypub, check))
    inner = json.dumps({
        "s": mypub + check,<commit_msg>kotone: show pin on push<commit_after>#!/usr/bin/env python
# coding: utf8
"""
* Tox DNS Discovery Management Daemon - draft API server for Tox ID publishing.
* Since "Tox DNS Discovery Management Daemon" is a mouthful, just call it "yuu"
*
* Author: stal, stqism; April 2014
* Copyright (c) 2014 Zodiac Labs.
* You are free to do whatever you want with this file -- provided that this
* notice is retained.
"""
import requests
import sys
import time
import mmap
import binascii
import json
import nacl.encoding as crypto_encode
import nacl.public as crypto
import os
import random
import base64

def _compute_checksum(data, iv=(0, 0)):
    checksum = list(iv)
    for ind, byte in enumerate(binascii.unhexlify(data)):
        checksum[ind % 2] ^= byte
    return "".join(hex(byte)[2:].zfill(2) for byte in checksum).upper()

def _read_tox_cherry(open_file):
    m = mmap.mmap(open_file.fileno(), 0, prot=mmap.PROT_READ)
    idx = m.find(b"KEYs") + 8
    m.seek(idx)
    bs = m.read(68)
    
    pk = binascii.hexlify(bs[:32]).upper().decode("ascii")
    sk = binascii.hexlify(bs[32:64]).upper().decode("ascii")
    ns = binascii.hexlify(bs[64:]).upper().decode("ascii")

    m.close()
    return (pk, sk, ns)

def _read_tox_mono(open_file):
    m = mmap.mmap(open_file.fileno(), 0, prot=mmap.PROT_READ)
    m.seek(0x10)
    bs = m.read(68)

    pk = binascii.hexlify(bs[4:36]).upper().decode("ascii")
    sk = binascii.hexlify(bs[36:]).upper().decode("ascii")
    ns = binascii.hexlify(bs[:4]).upper().decode("ascii")

    m.close()
    return (pk, sk, ns)

def read_tox(fil):
    with open(fil, "rb") as file_:
        magic = file_.read(4)
        print(repr(magic))
        if magic == "桜\x00".encode("utf8"):
            return _read_tox_cherry(file_)
        elif magic == b"\x00\x00\x00\x00":
            return _read_tox_mono(file_)

def push(addr, name, fil):
    pkr = requests.get(addr + "/pk", verify=False)
    sk = pkr.json()["key"]
    
    mypub, mysec, nospam = read_tox(fil)
    check = _compute_checksum(mypub + nospam)
    print("kotone: Publishing {0}/{1} to server.".format(mypub, check))
    pin = base64.b64encode(nospam.decode("hex"))[:6]
    print("kotone: By the way, your PIN is {0}.".format(pin))
    inner = json.dumps({
        "s": mypub + check,
{'fraction_non_alphanumeric': 0.15511775362318841, 'fraction_numerical': 0.025362318840579712, 'mean_word_length': 3.4081836327345307, 'pattern_counts': {'":': 4, '<': 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': '1935005', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1609, 'n_words': 397}