<commit_before>'''
   pyecc - Copyright 2009 Slide, Inc.
 
  This library is free software; you can redistribute it and/or 
  modify it under the terms of the GNU Lesser General Public License 
  as published by the Free Software Foundation; either version 2.1 of 
  the License, or (at your option) any later version.
 
  This library is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
  for more details. 
 
  You should have received a copy of the GNU Lesser General Public License 
  along with this library; if not, write to the 
  Free Software Foundation, Inc., 
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''

import _pyecc

DEFAULT_CURVE = _pyecc.DEFAULT_CURVE

class ECC(object):
    '''
        The ECC object must be instantiated to work with
        any encrypted data, as some amount of state is required
        at once
    '''
    def __init__(self, *args, **kwargs):
        self._private = kwargs.get('private')
        self._public = kwargs.get('public')
        assert self._private and self._public
        self._state = _pyecc.new_state()
        self._kp = _pyecc.new_keypair(self._public, self._private, self._state)

    @staticmethod
    def public_keygen(private_key):
        assert private_key, 'Private key cannot be empty or None'
        return _pyecc.pubkey_gen(private_key)

    def encrypt(self, plaintext):
        assert plaintext, 'You cannot encrypt "nothing"'
        return _pyecc.encrypt(plaintext, len(plaintext), self._kp, self._state)

    def decrypt(self, ciphertext):
        assert ciphertext, 'You cannot decrypt "nothing"'
        return _pyecc.decrypt(ciphertext, self._kp, self._state)

    def sign(self, data):
        if not self._kp:
            print 'You need a keypair object to verify a signature'
            return False

        if not self._state:
            print 'ECC object should have an internal _state member'
            return False

        return _pyecc.sign(data, self._kp, self._state)

    def verify(self, data, signature):
        if not self._kp:
            print 'You need a keypair object to verify a signature'
            return False

        if not self._state:
            print 'ECC object should have an internal _state member'
            return False

        return _pyecc.verify(data, signature, self._kp, self._state)<commit_msg>Create a generate method for Python to generate an arbitrary private key and then the corresponding public key<commit_after>'''
   pyecc - Copyright 2009 Slide, Inc.
 
  This library is free software; you can redistribute it and/or 
  modify it under the terms of the GNU Lesser General Public License 
  as published by the Free Software Foundation; either version 2.1 of 
  the License, or (at your option) any later version.
 
  This library is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
  for more details. 
 
  You should have received a copy of the GNU Lesser General Public License 
  along with this library; if not, write to the 
  Free Software Foundation, Inc., 
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
'''

import _pyecc

DEFAULT_CURVE = _pyecc.DEFAULT_CURVE

class ECC(object):
    '''
        The ECC object must be instantiated to work with
        any encrypted data, as some amount of state is required
        at once
    '''
    def __init__(self, *args, **kwargs):
        self._private = kwargs.get('private')
        self._public = kwargs.get('public')
        assert self._private and self._public
        self._state = _pyecc.new_state()
        self._kp = _pyecc.new_keypair(self._public, self._private, self._state)

    @staticmethod
    def public_keygen(private_key):
        assert private_key, 'Private key cannot be empty or None'
        return _pyecc.pubkey_gen(private_key)

    @staticmethod
    def generate(read_func, progress_func=None, entropy=4096):
        '''
            Allow ECC to generate a private and public key
            by reading entropy from `read_func` (assumes no args)

            The `progress_func` if specified should take no arguments
            and be used to indicate that the function is working
        '''
        privkey = []
        for i in xrange(entropy):
            char = read_func(1)
            if i % 10:
                progress_func()
            privkey.append(char)
        privkey = ''.join(privkey)
        pubkey = ECC.public_keygen(privkey)
        return (privkey, pubkey, DEFAULT_CURVE)

    def encrypt(self, plaintext):
        assert plaintext, 'You cannot encrypt "nothing"'
        return _pyecc.encrypt(plaintext, len(plaintext), self._kp, self._state)

    def decrypt(self, ciphertext):
        assert ciphertext, 'You cannot decrypt "nothing"'
        return _pyecc.decrypt(ciphertext, self._kp, self._state)

    def sign(self, data):
        if not self._kp:
            print 'You need a keypair object to verify a signature'
            return False

        if not self._state:
            print 'ECC object should have an internal _state member'
            return False

        return _pyecc.sign(data, self._kp, self._state)

    def verify(self, data, signature):
        if not self._kp:
            print 'You need a keypair object to verify a signature'
            return False

        if not self._state:
            print 'ECC object should have an internal _state member'
            return False

        return _pyecc.verify(data, signature, self._kp, self._state)
{'fraction_non_alphanumeric': 0.06719778316591618, 'fraction_numerical': 0.008139937651541392, 'mean_word_length': 2.589185829707893, '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': '4831512', 'n_tokens_mistral': 1653, 'n_tokens_neox': 1530, 'n_words': 646}