Add simple facility for storing secret in app-private storage on Android (other platforms not so good)

This commit is contained in:
Henrik Rydgård 2023-06-26 00:35:03 +02:00
parent 37fb3be4d5
commit 87846c5fcb
2 changed files with 23 additions and 0 deletions

View file

@ -87,3 +87,6 @@ void NativeShutdownGraphics();
void NativeShutdown();
void PostLoadConfig();
void NativeSaveSecret(const char *nameOfSecret, const std::string &data);
std::string NativeLoadSecret(const char *nameOfSecret);

View file

@ -1419,3 +1419,23 @@ void NativeShutdown() {
// Previously we did exit() here on Android but that makes it hard to do things like restart on backend change.
// I think we handle most globals correctly or correct-enough now.
}
Path GetSecretPath(const char *nameOfSecret) {
return g_Config.internalDataDirectory / ("ppsspp_" + std::string(nameOfSecret) + ".dat");
}
// name should be simple alphanumerics to avoid problems on Windows.
void NativeSaveSecret(const char *nameOfSecret, const std::string &data) {
// We'll simply store secrets in files under g_Config.internalDataDirectory.
// On Android, that corresponds to the app private directory. On other platforms,
// the location is less secure unfortunately - to be improved.
Path path = GetSecretPath(nameOfSecret);
File::WriteDataToFile(false, data.data(), data.size(), path);
}
std::string NativeLoadSecret(const char *nameOfSecret) {
Path path = GetSecretPath(nameOfSecret);
std::string data;
File::ReadFileToString(false, path, data);
return data;
}