<commit_before>""" 
    Python SDK for LaunchKey API 
    For use in implementing LaunchKey
    Version 1.0
    @author LaunchKey
    @created 2013-03-20
"""

import requests

def generate_RSA(bits=2048):
    '''
        Generate an RSA keypair
        @bits The key length in bits
        Return private key and public key
    '''
    from Crypto.PublicKey import RSA
    new_key = RSA.generate(bits, e=65537)
    public_key = new_key.publickey().exportKey("PEM")
    private_key = new_key.exportKey("PEM")
    return private_key, public_key

def decrypt_RSA(key, package):
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    from base64 import b64decode
    rsakey = RSA.importKey(key)
    rsakey = PKCS1_OAEP.new(rsakey)
    decrypted = rsakey.decrypt(b64decode(package))
    return decrypted

def encrypt_RSA(key, message):
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    from base64 import b64encode
    rsakey = RSA.importKey(key)
    rsakey = PKCS1_OAEP.new(rsakey)
    encrypted = rsakey.encrypt(message)
    return encrypted.encode('base64')

def sign_data(priv_key, data):
    from Crypto.PublicKey import RSA
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.Hash import SHA256
    from base64 import b64encode, b64decode
    rsakey = RSA.importKey(priv_key)
    signer = PKCS1_v1_5.new(rsakey)
    digest = SHA256.new()
    digest.update(b64decode(data))
    sign = signer.sign(digest)
    return b64encode(sign)


class API():
    """ Needed """

    API_HOST = "https://api.launchkey.com"
    api_pub_key = None

    def __init__(self, app_key, app_secret, private_key, domain, version, test=False):
        self.app_key = app_key
        self.app_secret = app_secret
        self.private_key = private_key
        self.domain = domain
        self.verify = not test
        if test:
            self.API_HOST = "https://dev-api.int.lkey:8000" + "/" + version + "/"
            #self.API_HOST = "https://sandbox-api.launchkey.com" + "/" + version + "/"
        else:
            self.API_HOST += "/" + version + "/"

    def _prepare_auth(self):
        ''' Encrypts app_secret with RSA key and signs '''
        if self.api_pub_key is None:
            #Ping to get key
            response = requests.get(self.API_HOST + "ping", verify=self.verify)
            self.api_pub_key = response.json()['key']
        encrypted_app_secret = encrypt_RSA(self.api_pub_key, self.app_secret)
        signature = sign_data(self.private_key, encrypted_app_secret)
        return {'app_key': self.app_key, 'secret_key': encrypted_app_secret,
                'signature': signature}

    def authorize(self, username):
        ''' Used to send an authorization request for a specific username '''
        params = self._prepare_auth()
        params['username'] = username
        response = requests.post(self.API_HOST + "auths", params=params, verify=self.verify)<commit_msg>Updated to take in a host when initializing API object.<commit_after>""" 
    Python SDK for LaunchKey API 
    For use in implementing LaunchKey
    Version 1.0
    @author LaunchKey
    @created 2013-03-20
"""

import requests

def generate_RSA(bits=2048):
    '''
        Generate an RSA keypair
        @bits The key length in bits
        Return private key and public key
    '''
    from Crypto.PublicKey import RSA
    new_key = RSA.generate(bits, e=65537)
    public_key = new_key.publickey().exportKey("PEM")
    private_key = new_key.exportKey("PEM")
    return private_key, public_key

def decrypt_RSA(key, package):
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    from base64 import b64decode
    rsakey = RSA.importKey(key)
    rsakey = PKCS1_OAEP.new(rsakey)
    decrypted = rsakey.decrypt(b64decode(package))
    return decrypted

def encrypt_RSA(key, message):
    from Crypto.PublicKey import RSA
    from Crypto.Cipher import PKCS1_OAEP
    from base64 import b64encode
    rsakey = RSA.importKey(key)
    rsakey = PKCS1_OAEP.new(rsakey)
    encrypted = rsakey.encrypt(message)
    return encrypted.encode('base64')

def sign_data(priv_key, data):
    from Crypto.PublicKey import RSA
    from Crypto.Signature import PKCS1_v1_5
    from Crypto.Hash import SHA256
    from base64 import b64encode, b64decode
    rsakey = RSA.importKey(priv_key)
    signer = PKCS1_v1_5.new(rsakey)
    digest = SHA256.new()
    digest.update(b64decode(data))
    sign = signer.sign(digest)
    return b64encode(sign)


class API(object):
    """ Needed """

    #self.API_HOST = "https://sandbox-api.launchkey.com"
    API_HOST = "https://api.launchkey.com"
    api_pub_key = None

    def __init__(self, app_key, app_secret, private_key, domain, version, api_host="", test=False):
        self.app_key = app_key
        self.app_secret = app_secret
        self.private_key = private_key
        self.domain = domain
        self.verify = not test
        if api_host != "":
            self.API_HOST = api_host
        self.API_HOST += "/" + version + "/"

    def _prepare_auth(self):
        ''' Encrypts app_secret with RSA key and signs '''
        if self.api_pub_key is None:
            #Ping to get key
            response = requests.get(self.API_HOST + "ping", verify=self.verify)
            self.api_pub_key = response.json()['key']
        encrypted_app_secret = encrypt_RSA(self.api_pub_key, self.app_secret)
        signature = sign_data(self.private_key, encrypted_app_secret)
        return {'app_key': self.app_key, 'secret_key': encrypted_app_secret,
                'signature': signature}

    def authorize(self, username):
        ''' Used to send an authorization request for a specific username '''
        params = self._prepare_auth()
        params['username'] = username
        response = requests.post(self.API_HOST + "auths", params=params, verify=self.verify)
{'fraction_non_alphanumeric': 0.09945596735804148, 'fraction_numerical': 0.020061203672220335, 'mean_word_length': 2.906374501992032, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, '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': '1141458', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1871, 'n_words': 452}