堡垒
使用IOTA Stronghold 密钥管理引擎存储秘密和密钥。
支持的平台
此插件需要至少 Rust 版本 1.77.2
平台 | 等级 | 备注 |
---|---|---|
windows | ||
linux | ||
macos | ||
android | ||
ios |
设置
安装 stronghold 插件开始。
使用项目的包管理器添加依赖
npm run tauri add stronghold
yarn run tauri add stronghold
pnpm tauri add stronghold
deno task tauri add stronghold
bun tauri add stronghold
cargo tauri add stronghold
-
在
src-tauri
文件夹中运行以下命令,以将插件添加到项目的Cargo.toml
依赖项中cargo add tauri-plugin-stronghold -
修改
lib.rs
初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().plugin(tauri_plugin_stronghold::Builder::new(|password| {}).build()).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您首选的 JavaScript 包管理器安装 JavaScript Guest 绑定
npm install @tauri-apps/plugin-strongholdyarn add @tauri-apps/plugin-strongholdpnpm add @tauri-apps/plugin-strongholddeno add npm:@tauri-apps/plugin-strongholdbun add @tauri-apps/plugin-stronghold
使用方法
此插件必须使用密码哈希函数初始化,该函数接受密码字符串并必须从中返回一个 32 字节哈希值。
使用 argon2 密码哈希函数初始化
Stronghold 插件提供了默认的哈希函数,使用 argon2 算法。
use tauri::Manager;
pub fn run() { tauri::Builder::default() .setup(|app| { let salt_path = app .path() .app_local_data_dir() .expect("could not resolve app local data path") .join("salt.txt"); app.handle().plugin(tauri_plugin_stronghold::Builder::with_argon2(&salt_path).build())?; Ok(()) }) .run(tauri::generate_context!()) .expect("error while running tauri application");}
使用自定义密码哈希函数初始化
或者,您可以通过使用 tauri_plugin_stronghold::Builder::new
构造函数提供自己的哈希算法。
pub fn run() { tauri::Builder::default() .plugin( tauri_plugin_stronghold::Builder::new(|password| { // Hash the password here with e.g. argon2, blake2b or any other secure algorithm // Here is an example implementation using the `rust-argon2` crate for hashing the password use argon2::{hash_raw, Config, Variant, Version};
let config = Config { lanes: 4, mem_cost: 10_000, time_cost: 10, variant: Variant::Argon2id, version: Version::Version13, ..Default::default() }; let salt = "your-salt".as_bytes(); let key = hash_raw(password.as_ref(), salt, &config).expect("failed to hash password");
key.to_vec() }) .build(), ) .run(tauri::generate_context!()) .expect("error while running tauri application");}
从 JavaScript 使用
stronghold 插件在 JavaScript 中可用。
import { Client, Stronghold } from '@tauri-apps/plugin-stronghold';// when using `"withGlobalTauri": true`, you may use// const { Client, Stronghold } = window.__TAURI__.stronghold;import { appDataDir } from '@tauri-apps/api/path';// when using `"withGlobalTauri": true`, you may use// const { appDataDir } = window.__TAURI__.path;
const initStronghold = async () => { const vaultPath = `${await appDataDir()}/vault.hold`; const vaultPassword = 'vault password'; const stronghold = await Stronghold.load(vaultPath, vaultPassword);
let client: Client; const clientName = 'name your client'; try { client = await stronghold.loadClient(clientName); } catch { client = await stronghold.createClient(clientName); }
return { stronghold, client, };};
// Insert a record to the storeasync function insertRecord(store: any, key: string, value: string) { const data = Array.from(new TextEncoder().encode(value)); await store.insert(key, data);}
// Read a record from storeasync function getRecord(store: any, key: string): Promise<string> { const data = await store.get(key); return new TextDecoder().decode(new Uint8Array(data));}
const { stronghold, client } = await initStronghold();
const store = client.getStore();const key = 'my_key';
// Insert a record to the storeinsertRecord(store, key, 'secret value');
// Read a record from storeconst value = await getRecord(store, key);console.log(value); // 'secret value'
// Save your updatesawait stronghold.save();
// Remove a record from storeawait store.remove(key);
权限
默认情况下,所有潜在的插件命令和作用域都被阻止,无法访问。您必须修改 capabilities
配置中的权限以启用这些权限。
有关更多信息,请参阅功能概述,以及使用插件权限的逐步指南。
{ ..., "permissions": [ "stronghold:default", ]}
默认权限
此权限集配置了从 stronghold 插件可用的操作类型。
已授予权限
默认情况下启用所有非破坏性操作。
allow-create-client
allow-get-store-record
allow-initialize
allow-execute-procedure
allow-load-client
allow-save-secret
allow-save-store-record
allow-save
权限表
标识符 | 描述 |
---|---|
|
允许 create_client 命令,而不需要预先配置的作用域。 |
|
拒绝 create_client 命令,而不需要预先配置的作用域。 |
|
允许 destroy 命令,而不需要预先配置的作用域。 |
|
拒绝 destroy 命令,而不需要预先配置的作用域。 |
|
允许 execute_procedure 命令,而不需要预先配置的作用域。 |
|
拒绝 execute_procedure 命令,而不需要预先配置的作用域。 |
|
允许 get_store_record 命令,而不需要预先配置的作用域。 |
|
拒绝 get_store_record 命令,而不需要预先配置的作用域。 |
|
允许 initialize 命令,而不需要预先配置的作用域。 |
|
拒绝 initialize 命令,而不需要预先配置的作用域。 |
|
允许 load_client 命令,而不需要预先配置的作用域。 |
|
拒绝 load_client 命令,而不需要预先配置的作用域。 |
|
允许 remove_secret 命令,而不需要预先配置的作用域。 |
|
拒绝 remove_secret 命令,而不需要预先配置的作用域。 |
|
允许 remove_store_record 命令,而不需要预先配置的作用域。 |
|
拒绝 remove_store_record 命令,而不需要预先配置的作用域。 |
|
允许 save 命令,而不需要预先配置的作用域。 |
|
拒绝 save 命令,而不需要预先配置的作用域。 |
|
允许 save_secret 命令,而不需要预先配置的作用域。 |
|
拒绝 save_secret 命令,而不需要预先配置的作用域。 |
|
允许 save_store_record 命令,而不需要预先配置的作用域。 |
|
拒绝 save_store_record 命令,而不需要预先配置的作用域。 |
© 2025 Tauri 贡献者。 CC-BY / MIT