docs/api/safe-storage.md
Allows access to simple encryption and decryption of strings for storage on the local machine.
Process: Main
This module adds extra protection to data being stored on disk by using OS-provided cryptography systems. Current security semantics for each platform are outlined below.
[!NOTE] We recommend using the asynchronous API (
encryptStringAsync/decryptStringAsync) over the synchronous API. The async API is non-blocking, supports key rotation, and handles temporary unavailability gracefully. The synchronous API may be deprecated in a future version of Electron.
kwallet, kwallet5, kwallet6 and gnome-libsecret, but more may be available in future versions of Electron. As such, the
security semantics of content protected via the safeStorage API vary between window managers and secret stores.
safeStorage API will be unprotected as they are encrypted via hardcoded plaintext password. You can detect when this happens when safeStorage.getSelectedStorageBackend() returns basic_text.Note that on macOS, access to the system Keychain is required and these calls can block the current thread to collect user input. The same is true for Linux, if a password management tool is available.
The asynchronous API uses pluggable key providers that vary by platform:
org.freedesktop.portal.Secret: Uses the Portal Secret D-Bus interface to retrieve application-specific secrets. This is the preferred provider for sandboxed environments like Flatpak.Unlike the synchronous API, these operations are non-blocking and support additional features like key rotation (indicated by shouldReEncrypt) and temporary unavailability handling (indicated by isTemporarilyUnavailable).
The safeStorage module emits the following events:
The safeStorage module has the following methods:
safeStorage.isEncryptionAvailable()Returns boolean - Whether encryption is available.
On Linux, returns true if the app has emitted the ready event and the secret key is available.
On MacOS, returns true if Keychain is available.
On Windows, returns true once the app has emitted the ready event.
safeStorage.isAsyncEncryptionAvailable()Returns Promise<Boolean> - Whether encryption is available for asynchronous safeStorage operations.
safeStorage.encryptString(plainText)plainText stringReturns Buffer - An array of bytes representing the encrypted string.
This function will throw an error if encryption fails.
safeStorage.decryptString(encrypted)encrypted BufferReturns string - the decrypted string. Decrypts the encrypted buffer
obtained with safeStorage.encryptString back into a string.
safeStorage.encryptStringAsync(plainText)plainText stringReturns Promise<Buffer> - An array of bytes representing the encrypted string.
safeStorage.decryptStringAsync(encrypted)encrypted BufferReturns Promise<Object> - Resolve with an object containing the following:
shouldReEncrypt boolean - whether data that has just been returned from the decrypt operation should be
re-encrypted, as the key has been rotated or a new key is available that provides a different security level. If true, you should call decryptStringAsync again to receive the new decrypted string.result string - the decrypted string.safeStorage.setUsePlainTextEncryption(usePlainText)usePlainText booleanThis function on Linux will force the module to use an in memory password for creating symmetric key that is used for encrypt/decrypt functions when a valid OS password manager cannot be determined for the current active desktop environment. This function is a no-op on Windows and MacOS.
safeStorage.getSelectedStorageBackend() LinuxReturns string - User friendly name of the password manager selected on Linux.
This function will return one of the following values:
basic_text - When the desktop environment is not recognised or if the following
command line flag is provided --password-store="basic".gnome_libsecret - When the desktop environment is X-Cinnamon, Deepin, GNOME, Pantheon, XFCE, UKUI, unity or if the following command line flag is provided --password-store="gnome-libsecret".kwallet - When the desktop session is kde4 or if the following command line flag
is provided --password-store="kwallet".kwallet5 - When the desktop session is kde5 or if the following command line flag
is provided --password-store="kwallet5".kwallet6 - When the desktop session is kde6 or if the following command line flag
is provided --password-store="kwallet6".unknown - When the function is called before app has emitted the ready event.