Table of Contents

Class LocalFileSystem

A static async helper for persisting data to the local file system with AES-256 encryption and GZip compression.

public static class LocalFileSystem
Inheritance
LocalFileSystem
Inherited Members

Remarks

Typical use-case: save-game data, player preferences, or any sensitive runtime data that should not be readable as plain text on disk.
All read/write operations are async to avoid blocking the Unity main thread.

Methods

CompressStringGZip(string)

Compresses a string using GZip and returns the compressed byte array.

public static byte[] CompressStringGZip(string input)

Parameters

input string

Returns

byte[]

DecompressStringGZip(byte[])

Decompresses a GZip byte array back into a string.

public static string DecompressStringGZip(byte[] compressedBytes)

Parameters

compressedBytes byte[]

Returns

string

Decrypt(string, string)

Decrypts a Base64 AES-256 cipher string produced by Encrypt(string, string).

public static string Decrypt(string cipherText, string password)

Parameters

cipherText string
password string

Returns

string

DeleteFileAsync(string, string, string, string)

Asynchronously deletes a file after verifying the password via decryption.

public static Task<bool> DeleteFileAsync(string savePath, string fileName, string password, string extension = ".json")

Parameters

savePath string

Directory containing the file.

fileName string

File name without extension.

password string

Password used to verify ownership before deletion.

extension string

File extension (default ".json").

Returns

Task<bool>

true on successful deletion.

Exceptions

LocalFileSystemException

Thrown when the file is not found or decryption fails.

Encrypt(string, string)

AES-256 encrypts plainText, prepends the IV, and returns a Base64 string.

public static string Encrypt(string plainText, string password)

Parameters

plainText string
password string

Returns

string

GetKey(string)

Derives a 256-bit AES key from a plain-text password using SHA-256.

public static byte[] GetKey(string password)

Parameters

password string

The user-supplied password.

Returns

byte[]

A 32-byte key array.

LoadAllFileNames(string, string)

Returns the file names (without extensions) of all files with the given extension found under path.

public static IEnumerable<string> LoadAllFileNames(string path, string extension = ".json")

Parameters

path string

Directory path to search.

extension string

File extension filter (default ".json").

Returns

IEnumerable<string>

Enumerable of file names without extension.

LoadFileAsync(string, string, string, string)

Asynchronously reads, decrypts (AES-256), and decompresses (GZip) a file saved by SaveFileAsync(string, string, string, string, string).

public static Task<string> LoadFileAsync(string savePath, string fileName, string password, string extension = ".json")

Parameters

savePath string

Directory containing the file.

fileName string

File name without extension.

password string

The password used when the file was saved.

extension string

File extension (default ".json").

Returns

Task<string>

The original plain-text data, or null if the file does not exist.

Exceptions

LocalFileSystemException

Thrown when decryption fails (wrong password or corrupt file).

SaveFileAsync(string, string, string, string, string)

Asynchronously compresses (GZip) and encrypts (AES-256) jsonData then writes it to <savePath>/<fileName><extension>. The directory is created if it does not exist.

public static Task SaveFileAsync(string savePath, string fileName, string password, string jsonData, string extension = ".json")

Parameters

savePath string

Target directory path.

fileName string

File name without extension.

password string

Encryption password (SHA-256 derived key).

jsonData string

The raw content to persist.

extension string

File extension (default ".json").

Returns

Task