Stronghold
使用 IOTA Stronghold 秘密管理引擎儲存秘密和金鑰。
支援的平台
此外掛程式需要至少 1.77.2 的 Rust 版本
平台 | 等級 | 備註 |
---|---|---|
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 Contributors. CC-BY / MIT