# Path: groundstation/crypto/rsa.py
# class RSAAdaptor(object):
#     def __init__(self, keyset):
#         """Initialize an RSAAdaptor
# 
#         keyset = a dict of identifiers to public keys to test signatures
#                  against
#         """
#         self.keyset = self._render_keyset(keyset)
# 
#     def verify(self, data, signature):
#         assert len(data) == 40, "We only sign sha1 hashes"
#         for keyname in self.keyset:
#             key = self.keyset[keyname]
#             if key.verify(str(data), signature):
#                 return keyname
#         return False
# 
#     def _render_keyset(self, keyset):
#         return {i: convert_pubkey(keyset[i]) for i in keyset}
# 
# class RSAPrivateAdaptor(object):
#     def __init__(self, key):
#         self.key = convert_privkey(key)
# 
#     def sign(self, data):
#         assert len(data) == 40, "We only sign sha1 hashes"
#         return self.key.sign(data, None)
# 
# Path: groundstation/crypto/rsa.py
# def convert_privkey(key):
#     pkey = RSA.importKey(key)
#     assert pkey.has_private()
#     return pkey
# 
# def convert_pubkey(key):
#     # get the second field from the public key file.
#     # import pdb; pdb.set_trace()
#     keydata = base64.b64decode(key.split(None)[1])
# 
#     parts = []
#     while keydata:
#         # read the length of the data
#         dlen = struct.unpack('>I', keydata[:4])[0]
# 
#         # read in <length> bytes
#         data, keydata = keydata[4:dlen+4], keydata[4+dlen:]
# 
#         parts.append(data)
#     exponent = materialise(parts[1])
#     numeric = materialise(parts[2])
# 
#     return RSA.construct((long(numeric), long(exponent)))
# 
# Path: groundstation/crypto/rsa.py
# def materialise(n):
# def convert_pubkey(key):
# def convert_privkey(key):
#     def __init__(self, keyset):
#     def verify(self, data, signature):
#     def _render_keyset(self, keyset):
#     def __init__(self, key):
#     def sign(self, data):
# class RSAAdaptor(object):
# class RSAPrivateAdaptor(object):


import unittest
from Crypto.PublicKey import RSA
from support import crypto_fixture
from groundstation.crypto.rsa import RSAAdaptor, RSAPrivateAdaptor
from groundstation.crypto.rsa import convert_privkey, convert_pubkey
from groundstation.crypto.rsa import materialise_exponent, materialise_numeric



valid_keyset = {
        "valid": crypto_fixture.valid_pubkey
        }


class CryptoRSAAdaptorTestCase(unittest.TestCase):
    def test_converts_pubkey_to_pem(self):
        key = convert_pubkey(crypto_fixture.valid_pubkey)
        self.assertIsInstance(key, RSA._RSAobj)
        self.assertFalse(key.has_private())

    def test_convertes_privkey_to_pem(self):
        key = convert_privkey(crypto_fixture.valid_key)
        self.assertIsInstance(key, RSA._RSAobj)
        self.assertTrue(key.has_private())

    def test_barfs_on_invalid_keys(self):
        self.assertRaises(TypeError, convert_pubkey, crypto_fixture.invalid_pubkey)

    def test_initializes_with_keyset(self):
        adaptor = RSAAdaptor({
            "key1": crypto_fixture.valid_pubkey,
            "key2": crypto_fixture.valid_pubkey,
            "key3": crypto_fixture.valid_pubkey,
            "key4": crypto_fixture.valid_pubkey
            })

# Path: groundstation/crypto/rsa.py
# class RSAAdaptor(object):
#     def __init__(self, keyset):
#         """Initialize an RSAAdaptor
# 
#         keyset = a dict of identifiers to public keys to test signatures
#                  against
#         """
#         self.keyset = self._render_keyset(keyset)
# 
#     def verify(self, data, signature):
#         assert len(data) == 40, "We only sign sha1 hashes"
#         for keyname in self.keyset:
#             key = self.keyset[keyname]
#             if key.verify(str(data), signature):
#                 return keyname
#         return False
# 
#     def _render_keyset(self, keyset):
#         return {i: convert_pubkey(keyset[i]) for i in keyset}
# 
# class RSAPrivateAdaptor(object):
#     def __init__(self, key):
#         self.key = convert_privkey(key)
# 
#     def sign(self, data):
#         assert len(data) == 40, "We only sign sha1 hashes"
#         return self.key.sign(data, None)
# 
# Path: groundstation/crypto/rsa.py
# def convert_privkey(key):
#     pkey = RSA.importKey(key)
#     assert pkey.has_private()
#     return pkey
# 
# def convert_pubkey(key):
#     # get the second field from the public key file.
#     # import pdb; pdb.set_trace()
#     keydata = base64.b64decode(key.split(None)[1])
# 
#     parts = []
#     while keydata:
#         # read the length of the data
#         dlen = struct.unpack('>I', keydata[:4])[0]
# 
#         # read in <length> bytes
#         data, keydata = keydata[4:dlen+4], keydata[4+dlen:]
# 
#         parts.append(data)
#     exponent = materialise(parts[1])
#     numeric = materialise(parts[2])
# 
#     return RSA.construct((long(numeric), long(exponent)))
# 
# Path: groundstation/crypto/rsa.py
# def materialise(n):
# def convert_pubkey(key):
# def convert_privkey(key):
#     def __init__(self, keyset):
#     def verify(self, data, signature):
#     def _render_keyset(self, keyset):
#     def __init__(self, key):
#     def sign(self, data):
# class RSAAdaptor(object):
# class RSAPrivateAdaptor(object):


# Path: test/test_crypto_rsaadaptor.py
import unittest
from Crypto.PublicKey import RSA
from support import crypto_fixture
from groundstation.crypto.rsa import RSAAdaptor, RSAPrivateAdaptor
from groundstation.crypto.rsa import convert_privkey, convert_pubkey
from groundstation.crypto.rsa import materialise_exponent, materialise_numeric




valid_keyset = {
        "valid": crypto_fixture.valid_pubkey
        }


class CryptoRSAAdaptorTestCase(unittest.TestCase):
    def test_converts_pubkey_to_pem(self):
        key = convert_pubkey(crypto_fixture.valid_pubkey)
        self.assertIsInstance(key, RSA._RSAobj)
        self.assertFalse(key.has_private())

    def test_convertes_privkey_to_pem(self):
        key = convert_privkey(crypto_fixture.valid_key)
        self.assertIsInstance(key, RSA._RSAobj)
        self.assertTrue(key.has_private())

    def test_barfs_on_invalid_keys(self):
        self.assertRaises(TypeError, convert_pubkey, crypto_fixture.invalid_pubkey)

    def test_initializes_with_keyset(self):
        adaptor = RSAAdaptor({
            "key1": crypto_fixture.valid_pubkey,
            "key2": crypto_fixture.valid_pubkey,
            "key3": crypto_fixture.valid_pubkey,
            "key4": crypto_fixture.valid_pubkey
            })

        self.assertIsInstance(adaptor, RSAAdaptor)