<commit_before># -*- coding: utf-8 -*-
# Copyright (c) <NAME> <<EMAIL>> 2015. See LICENSE for details.

# WORK IN PROGRESS

import base64
import bcrypt
import ed25519
import hashlib
import os
import re
import struct


class SignifyError(Exception):
    pass


class InvalidSignature(SignifyError):
    pass


def xorbuf(buf1, buf2):
    res = []
    for i in range(len(buf1)):
        res.append(chr(ord(buf1[i]) ^ ord(buf2[i])))
    return ''.join(res)


class Signify(object):
    def __init__(self):
        pass

    def _decrypt_secret_key(self, blob, password):
        comment, b64 = blob.split('\n', 1)

        extracted_comment = re.findall(r'^untrusted comment: (.*?) secret key$', comment)
        if extracted_comment:
            extracted_comment = extracted_comment[0]
        else:
            extracted_comment = 'signify'

        buf = base64.b64decode(b64)
        pkalg, kdfalg, kdfrounds, salt, checksum, keynum, seckey = \
            struct.unpack('!2s2sL16s8s8s64s', buf)

        assert pkalg == 'Ed'
        assert kdfalg == 'BK'
        assert kdfrounds in [0, 42]

        if kdfrounds == 0:
            xorkey = '\x00' * 64
        else:
            xorkey = bcrypt.kdf(password, salt, 64, kdfrounds)
        priv = xorbuf(seckey, xorkey)

        checksum_ref = hashlib.sha512(priv).digest()[0:8]
        if checksum != checksum_ref:
            raise KeyError('incorrect password')

        return priv, keynum, extracted_comment

    def _parse_public_key(self, blob):
        comment, b64 = blob.split('\n', 1)

        buf = base64.b64decode(b64)
        pkalg, keynum, pubkey = \
            struct.unpack('!2s8s32s', buf)

        assert pkalg == 'Ed'

        return pubkey, keynum

    def _parse_sigfile(self, blob):
        comment, b64 = blob.split('\n', 1)

        buf = base64.b64decode(b64)
        pkalg, keynum, sig = \
            struct.unpack('!2s8s64s', buf)

        assert pkalg == 'Ed'

        return sig, keynum

    def generate(self, comment, password):
        """Generate a signify keypair.

        @param comment: A comment to name the keypair, or None.
        @param password: A password to protect the private key, or None.
        """

        if comment is None:
            comment = 'signify'
<commit_msg>Error handling in pure code.<commit_after># -*- coding: utf-8 -*-
# Copyright (c) <NAME> <<EMAIL>> 2015. See LICENSE for details.

# WORK IN PROGRESS

import base64
import bcrypt
import ed25519
import hashlib
import os
import re
import struct


class SignifyError(Exception):
    pass


class InvalidSignature(SignifyError):
    pass


def xorbuf(buf1, buf2):
    res = []
    for i in range(len(buf1)):
        res.append(chr(ord(buf1[i]) ^ ord(buf2[i])))
    return ''.join(res)


class Signify(object):
    def __init__(self):
        pass

    def _decrypt_secret_key(self, blob, password):
        try:
            comment, b64 = blob.split('\n', 1)
        except:
            raise SignifyError('malformed blob')

        extracted_comment = re.findall(r'^untrusted comment: (.*?) secret key$', comment)
        if extracted_comment:
            extracted_comment = extracted_comment[0]
        else:
            extracted_comment = 'signify'

        try:
            buf = base64.b64decode(b64)
            pkalg, kdfalg, kdfrounds, salt, checksum, keynum, seckey = \
                struct.unpack('!2s2sL16s8s8s64s', buf)

            assert pkalg == 'Ed'
            assert kdfalg == 'BK'
            assert kdfrounds in [0, 42]
        except Exception, e:
            raise SignifyError(e)

        if kdfrounds == 0:
            xorkey = '\x00' * 64
        else:
            xorkey = bcrypt.kdf(password, salt, 64, kdfrounds)
        priv = xorbuf(seckey, xorkey)

        checksum_ref = hashlib.sha512(priv).digest()[0:8]
        if checksum != checksum_ref:
            raise KeyError('incorrect password')

        return priv, keynum, extracted_comment

    def _parse_public_key(self, blob):
        try:
            comment, b64 = blob.split('\n', 1)

            buf = base64.b64decode(b64)
            pkalg, keynum, pubkey = \
                struct.unpack('!2s8s32s', buf)

            assert pkalg == 'Ed'
        except Exception, e:
            raise SignifyError('malformed public key')

        return pubkey, keynum

    def _parse_sigfile(self, blob):
        try:
            comment, b64 = blob.split('\n', 1)

            buf = base64.b64decode(b64)
            pkalg, keynum, sig = \
                struct.unpack('!2s8s64s', buf)

            assert pkalg == 'Ed'
        except Exception, e:
            raise SignifyError('malformed signature blob')

        return sig, keynum

    def generate(self, comment, password):
        """Generate a signify keypair.

        @param comment: A comment to name the keypair, or None.
        @param password: A password to protect the private key, or None.
        """

        if comment is None:
            comment = 'signify'

{'fraction_non_alphanumeric': 0.11068778824944857, 'fraction_numerical': 0.03047924603970323, 'mean_word_length': 2.1000621504039776, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3167446', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1581, 'n_words': 425}