Class ObscuredFile
Allows saving any binary data into the file either with or without encryption, with or without device locking and always with integrity check to make sure file is genuine.
public class ObscuredFile
- Inheritance
-
objectObscuredFile
Remarks
Uses File IO which may cause main thread hiccups when operating with big amounts of data thus
it's recommended to use it from background thread or at the stall moments of your app
(like loading screens etc).
Please call DeviceIdHolder.ForceLockToDeviceInit() before accessing this class from background thread if you are using Device Lock feature without custom DeviceID.
// Create encryption settings with password for secure file storage
var encryptionSettings = new EncryptionSettings("MySecretPassword123");
var deviceLockSettings = new DeviceLockSettings();
var settings = new ObscuredFileSettings(encryptionSettings, deviceLockSettings, ObscuredFileLocation.Custom);
// Create ObscuredFile instance with encrypted settings
var safeFile = new ObscuredFile("mySecureData.txt", settings);
// Prepare data to save (convert to byte array)
string jsonData = "{ \"score\": 1000, \"level\": 5, \"playerName\": \"Player1\" }";
byte[] dataToSave = System.Text.Encoding.UTF8.GetBytes(jsonData);
// Write encrypted data to file with integrity check
var writeResult = safeFile.WriteAllBytes(dataToSave);
if (writeResult.Error.ErrorCode == ObscuredFileErrorCode.NoError)
Debug.Log("Encrypted data saved successfully!");
else
Debug.LogError($"Failed to save encrypted data: {writeResult.Error.ErrorCode}");
// Read and decrypt data back from file
var readResult = safeFile.ReadAllBytes();
if (readResult.Error.ErrorCode == ObscuredFileErrorCode.NoError && readResult.Data != null)
{
// Convert byte array back to string
string loadedData = System.Text.Encoding.UTF8.GetString(readResult.Data);
Debug.Log($"Loaded decrypted data: {loadedData}");
// Check if data integrity is valid (tampering detection)
if (readResult.DataIsNotGenuine)
Debug.LogWarning("Data tampering detected! File may have been modified.");
// Check if data is from another device (device lock feature)
if (readResult.DataFromAnotherDevice)
Debug.LogWarning("Data is from another device! This may indicate unauthorized access.");
}
else
{
Debug.LogError($"Failed to read encrypted data: {readResult.Error.ErrorCode}");
}
Constructors
ObscuredFile()
Creates instance with file name set to DefaultFileName and default ObscuredFileSettings.
public ObscuredFile()
ObscuredFile(ObscuredFileSettings)
Creates instance with file name set to DefaultFileName and custom specific settings.
public ObscuredFile(ObscuredFileSettings settings)
Parameters
settingsObscuredFileSettingsCustom settings to use with this instance.
ObscuredFile(string)
Creates instance with specified file name and default ObscuredFileSettings.
public ObscuredFile(string fileName)
Parameters
fileNamestringCustom file name to place at ObscuredFileLocation.PersistentData.
ObscuredFile(string, IObscuredFileSettings)
Creates instance with specified file name or file path and custom specific settings.
public ObscuredFile(string fileNameOrPath, IObscuredFileSettings settings)
Parameters
fileNameOrPathstringFile path if using ObscuredFileLocation.Custom, otherwise represents file name to use with set ObscuredFileLocation kind.
settingsIObscuredFileSettingsCustom settings to use with this instance.
Fields
DefaultFileName
Filename used by default, if other name or path was not specified in constructor.
public const string DefaultFileName = "actkfile"
Field Value
- string
Properties
CurrentSettings
Allows reading current settings.
public IObscuredFileSettings CurrentSettings { get; }
Property Value
FileExists
Returns true if file at FilePath physically exists on disk.
public bool FileExists { get; }
Property Value
- bool
FilePath
Returns path to the file. It's always not empty and valid even if file was not saved to the physical disk yet.
public string FilePath { get; }
Property Value
- string
Methods
Delete()
Deletes physical file assigned with this instance. Does nothing if file does not exists.
public void Delete()
ReadAllBytes()
Reads all bytes from the physical file on disk. Consider calling from background thread for the large data amount.
public ObscuredFileReadResult ReadAllBytes()
Returns
- ObscuredFileReadResult
Operation result structure allowing both to get read data and figure out possible violations and errors if data could not be read for some reason.
WriteAllBytes(byte[])
Writes passed data to the physical file on disk.
Consider calling from background thread for the large data amount.
public ObscuredFileWriteResult WriteAllBytes(byte[] data)
Parameters
databyte[]Custom byte array to write.
Returns
- ObscuredFileWriteResult
Operation result.
Events
DataFromAnotherDeviceDetected
Fires when saved data from some other device detected.
public event Action DataFromAnotherDeviceDetected
Event Type
- Action
Remarks
May be helpful to ban potential cheaters, trying to use someone's purchased in-app goods for example.
📝 Note: Will fire if same device ID has
changed (pretty rare case though). Read more at DeviceLockLevel.
NotGenuineDataDetected
Fires when saved data tampering detected. Will not fire when data is damaged and not readable.
public event Action NotGenuineDataDetected
Event Type
- Action