safeStorage
允许访问简单的字符串加密和解密,以便在本地机器上存储。
进程:主进程
🌐 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.
- macOS:加密密钥会以一种防止其他应用在未经用户授权的情况下加载它们的方式,存储在你的应用的 钥匙串访问 中。因此,内容可以防止同一用户空间中的其他用户和其他应用访问。
- Windows:加密密钥是通过 DPAPI 生成的。根据 Windows 文档:“通常,只有使用与加密数据的用户相同登录凭据的用户才能解密数据”。因此,内容对同一台机器上的其他用户是受保护的,但对运行在同一用户空间的其他应用则不受保护。
- Linux:加密密钥会生成并存储在一个秘密存储中,该存储取决于你的窗口管理器和系统设置。目前支持的选项有
kwallet、kwallet5、kwallet6和gnome-libsecret,但未来 Electron 版本中可能会提供更多选项。因此,通过safeStorageAPI 保护的内容的安全语义在不同的窗口管理器和秘密存储之间有所不同。- 请注意,并非所有的 Linux 系统都提供可用的密钥存储。如果没有可用的密钥存储,使用
safeStorageAPI 存储的项目将不受保护,因为它们是通过硬编码的明文密码加密的。当safeStorage.getSelectedStorageBackend()返回basic_text时,可以检测到这种情况。
- 请注意,并非所有的 Linux 系统都提供可用的密钥存储。如果没有可用的密钥存储,使用
请注意,在 Mac 上,需要访问系统钥匙串,并且这些调用可能会阻塞当前线程以收集用户输入。对于 Linux 来说,如果可用密码管理工具,同样适用。
🌐 Note that on Mac, 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.
方法
🌐 Methods
safeStorage 模块具有以下方法:
🌐 The safeStorage module has the following methods:
safeStorage.isEncryptionAvailable()
返回 boolean - 是否可用加密。
🌐 Returns boolean - Whether encryption is available.
在 Linux 上,如果应用已触发 ready 事件并且密钥可用,则返回 true。
在 MacOS 上,如果 Keychain 可用,则返回 true。
在 Windows 上,一旦应用触发 ready 事件,则返回 true。
🌐 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.encryptString(plainText)
plainText字符串
返回 Buffer - 一个表示加密字符串的字节数组。
🌐 Returns Buffer - An array of bytes representing the encrypted string.
如果加密失败,此函数将抛出错误。
🌐 This function will throw an error if encryption fails.
safeStorage.decryptString(encrypted)
encrypted缓冲区
返回 string - 解密后的字符串。将使用 safeStorage.encryptString 获得的加密缓冲区解密回字符串。
🌐 Returns string - the decrypted string. Decrypts the encrypted buffer
obtained with safeStorage.encryptString back into a string.
如果解密失败,该函数将抛出错误。
🌐 This function will throw an error if decryption fails.
safeStorage.setUsePlainTextEncryption(usePlainText)
usePlainText布尔值
在 Linux 上,此功能将强制模块使用内存中的密码来创建对称密钥,该密钥用于加密/解密功能,当无法为当前活动桌面环境确定有效的操作系统密码管理器时使用。在 Windows 和 MacOS 上,此功能无操作效果。
🌐 This 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() Linux
返回 string - 在 Linux 上选择的密码管理器的用户友好名称。
🌐 Returns string - User friendly name of the password manager selected on Linux.
该函数将返回以下值之一:
🌐 This function will return one of the following values:
basic_text- 当桌面环境未被识别或提供以下命令行标志--password-store="basic"时。gnome_libsecret- 当桌面环境是X-Cinnamon、Deepin、GNOME、Pantheon、XFCE、UKUI、unity时,或者提供了以下命令行参数--password-store="gnome-libsecret"。kwallet- 当桌面会话为kde4时,或提供以下命令行参数时--password-store="kwallet"。kwallet5- 当桌面会话为kde5或提供以下命令行标志--password-store="kwallet5"时。kwallet6- 当桌面会话为kde6或者提供了以下命令行参数--password-store="kwallet6"时。unknown- 当函数在应用发出ready事件之前被调用时。