Text file streams - mikahama/mikatools GitHub Wiki

  • Always UTF-8! If you save anything with mikatools, you will know the file will be read correctly in any operating system!
  • Built-in encryption/decryption, just pass password and salt parameters to use it!

Saving a file

Save a file in UTF-8 format!

open_write(file_path, allow_overwrite=True, append=False, password=None, salt="", key=None, key_password=None)

form mikatools import *
f = open_write("output.txt")
f.write("hello")
f.close()

The example above is a minimal one for saving data in UTF-8 format. If allow_overwrite is False, the stream will not write on top of an existing file. With append set to True, the stream will write to the end of an existing file.

If the password parameter is set, open_write will return a mikatools.crypto.CryptoWriteStream object. This will encrypt anything written to the stream with Fernet. This is a symmetric encryption (the same password is used to encrypt and decrypt). This stream works almost like a regular file stream.

form mikatools import *
f = open_write("output.txt", password="passw0rd", salt="so_secret")
f.write("my greatest secrets")
f.close()

If the key parameter is set, open_write will return a mikatools.crypto.CryptoWriteStream object. This will encrypt anything written to the stream with RSA. This is an asymmetric encryption (public key is used to encrypt and private key to decrypt). This stream works almost like a regular file stream.

form mikatools import *
from mikatools import crypto
private, public = crypto.generate_keys()
f = open_write("output.txt", key=public)
f.write("my greatest secrets")
f.close()

Read more about saving and loading the RSA keys.

text_dump(file_path, text, allow_overwrite=True, append=False, password=None, salt="", key=None, key_password=None)

This will save text to a file in UTF-8 and close the file stream. See open_write for the parameters.

from mikatools import *
text_dump("file.txt", "text to save")

Reading a file

Open a UTF-8 file

open_read(file_path, password=None, salt="", key=None, key_password=None)

from mikatools import *
f = open_read("file.txt")
for line in f:
    print(line)
f.close()

The example above shows how to open and read a UTF-8 file.

For opening files encrypted with mikatools symmetric encryption, just pass a password and salt. This will return a mikatools.crypto.CryptoReadStream object that behaves mostly like a regular file stream.

from mikatools import *
f = open_read("file.txt", password="passw0rd", salt="so_secret")
for line in f:
    print(line)
f.close()

For opening files encrypted with mikatools asymmetric encryption, just pass a private key. This will return a mikatools.crypto.CryptoReadStream object that behaves mostly like a regular file stream.

from mikatools import *
f = open_read("file.txt", key="/path/to/private_key/id_rsa")
for line in f:
    print(line)
f.close()

Read more about saving and loading the RSA keys.