Ejemplo n.º 1
0
    def test_export_public_key_method(self):
        with NamedTemporaryFile() as key_file:
            dsa_key = DSAKey()
            dsa_key.p = 6
            dsa_key.q = 0
            dsa_key.g = 9
            dsa_key.y = 6

            DSAKeyIO.export_key(dsa_key, key_file.name)

            key_file.seek(0)
            key_file_contents = key_file.readlines()

            self.assertEqual(5, len(key_file_contents))
            self.assertEqual('# public key', key_file_contents[0].strip())
            self.assertEqual('6', key_file_contents[1].strip())
            self.assertEqual('0', key_file_contents[2].strip())
            self.assertEqual('9', key_file_contents[3].strip())
            self.assertEqual('6', key_file_contents[4].strip())
Ejemplo n.º 2
0
    def import_key(filename):
        with open(filename, 'r') as import_file:
            import_file_contents = import_file.readlines()

            if len(import_file_contents) < 5:
                raise IOError('Invalid file contents')

            dsa_key = DSAKey()

            try:
                dsa_key.p = long(import_file_contents[1].strip())
                dsa_key.q = long(import_file_contents[2].strip())
                dsa_key.g = long(import_file_contents[3].strip())
                dsa_key.y = long(import_file_contents[4].strip())

                if len(import_file_contents) >= 6:
                    dsa_key.x = long(import_file_contents[5].strip())
            except:
                raise IOError('Invalid file contents')

            return dsa_key
Ejemplo n.º 3
0
    def test_export_private_key_method(self):
        with NamedTemporaryFile() as key_file:
            dsa_key = DSAKey()
            dsa_key.p = 1
            dsa_key.q = 3
            dsa_key.g = 5
            dsa_key.y = 6
            dsa_key.x = 7

            DSAKeyIO.export_key(dsa_key, key_file.name)

            key_file.seek(0)
            key_file_contents = key_file.readlines()

            self.assertEqual(6, len(key_file_contents))
            self.assertEqual('# private key', key_file_contents[0].strip())
            self.assertEqual('1', key_file_contents[1].strip())
            self.assertEqual('3', key_file_contents[2].strip())
            self.assertEqual('5', key_file_contents[3].strip())
            self.assertEqual('6', key_file_contents[4].strip())
            self.assertEqual('7', key_file_contents[5].strip())