Ejemplo n.º 1
0
def test_import_custom_keystore(tmpdir):

    # Too short - 32 bytes is required
    custom_secret = b'tooshort'
    with pytest.raises(ValueError, match=f'Entropy bytes bust be exactly {SecretKey.serialized_size()}.'):
        _keystore = Keystore.import_secure(key_material=custom_secret,
                                           password=INSECURE_DEVELOPMENT_PASSWORD,
                                           keystore_dir=tmpdir)

    # Too short - 32 bytes is required
    custom_secret = b'thisisabunchofbytesthatisabittoolong'
    with pytest.raises(ValueError, match=f'Entropy bytes bust be exactly {SecretKey.serialized_size()}.'):
        _keystore = Keystore.import_secure(key_material=custom_secret,
                                           password=INSECURE_DEVELOPMENT_PASSWORD,
                                           keystore_dir=tmpdir)

    # Import private key
    custom_secret = os.urandom(SecretKey.serialized_size())  # insecure but works
    keystore = Keystore.import_secure(key_material=custom_secret,
                                      password=INSECURE_DEVELOPMENT_PASSWORD,
                                      keystore_dir=tmpdir)
    keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
    assert keystore._Keystore__secret == custom_secret
    keystore.lock()

    path = keystore.keystore_path
    del keystore

    # Restore custom secret from encrypted keystore file
    keystore = Keystore(keystore_path=path)
    keystore.unlock(password=INSECURE_DEVELOPMENT_PASSWORD)
    assert keystore._Keystore__secret == custom_secret
Ejemplo n.º 2
0
    def write_keystore(self,
                       password: str,
                       key_material: Optional[bytes] = None,
                       interactive: bool = True) -> Keystore:
        if key_material:
            self.__keystore = Keystore.import_secure(
                key_material=key_material,
                password=password,
                keystore_dir=self.keystore_dir)
        else:
            if interactive:
                self.__keystore = Keystore.generate(
                    password=password,
                    keystore_dir=self.keystore_dir,
                    interactive=interactive)
            else:
                self.__keystore, _ = Keystore.generate(
                    password=password,
                    keystore_dir=self.keystore_dir,
                    interactive=interactive)

        return self.keystore