跳到內容
Tauri

Stronghold

使用 IOTA Stronghold 秘密管理引擎儲存秘密和金鑰。

支援的平台

此外掛程式需要至少 1.77.2 的 Rust 版本

平台 等級 備註
windows
linux
macos
android
ios

設定

安裝 stronghold 外掛程式以開始使用。

使用專案的套件管理器新增依賴項

npm run tauri add stronghold

用法

外掛程式必須使用密碼雜湊函數初始化,該函數接受密碼字串,並且必須傳回從密碼字串衍生的 32 位元組雜湊值。

使用 argon2 密碼雜湊函數初始化

Stronghold 外掛程式提供使用 argon2 演算法的預設雜湊函數。

src-tauri/src/lib.rs
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 建構函式來提供您自己的雜湊演算法。

src-tauri/src/lib.rs
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 store
async 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 store
async 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 store
insertRecord(store, key, 'secret value');
// Read a record from store
const value = await getRecord(store, key);
console.log(value); // 'secret value'
// Save your updates
await stronghold.save();
// Remove a record from store
await store.remove(key);

權限

預設情況下,所有潛在危險的外掛程式命令和範圍都遭到封鎖,並且無法存取。您必須修改 capabilities 組態中的權限才能啟用這些。

請參閱功能總覽以取得更多資訊,並參閱逐步指南以使用外掛程式權限。

src-tauri/capabilities/default.json
{
...,
"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

權限表

識別碼 描述

stronghold:allow-create-client

啟用 create_client 命令,而無需任何預先設定的範圍。

stronghold:deny-create-client

拒絕 create_client 命令,而無需任何預先設定的範圍。

stronghold:allow-destroy

啟用 destroy 命令,而無需任何預先設定的範圍。

stronghold:deny-destroy

拒絕 destroy 命令,而無需任何預先設定的範圍。

stronghold:allow-execute-procedure

啟用 execute_procedure 命令,而無需任何預先設定的範圍。

stronghold:deny-execute-procedure

拒絕 execute_procedure 命令,而無需任何預先設定的範圍。

stronghold:allow-get-store-record

啟用 get_store_record 命令,而無需任何預先設定的範圍。

stronghold:deny-get-store-record

拒絕 get_store_record 命令,而無需任何預先設定的範圍。

stronghold:allow-initialize

啟用 initialize 命令,而無需任何預先設定的範圍。

stronghold:deny-initialize

拒絕 initialize 命令,而無需任何預先設定的範圍。

stronghold:allow-load-client

啟用 load_client 命令,而無需任何預先設定的範圍。

stronghold:deny-load-client

拒絕 load_client 命令,而無需任何預先設定的範圍。

stronghold:allow-remove-secret

啟用 remove_secret 命令,而無需任何預先設定的範圍。

stronghold:deny-remove-secret

拒絕 remove_secret 命令,而無需任何預先設定的範圍。

stronghold:allow-remove-store-record

啟用 remove_store_record 命令,而無需任何預先設定的範圍。

stronghold:deny-remove-store-record

拒絕 remove_store_record 命令,而無需任何預先設定的範圍。

stronghold:allow-save

啟用 save 命令,而無需任何預先設定的範圍。

stronghold:deny-save

拒絕 save 命令,而無需任何預先設定的範圍。

stronghold:allow-save-secret

啟用 save_secret 命令,而無需任何預先設定的範圍。

stronghold:deny-save-secret

拒絕 save_secret 命令,而無需任何預先設定的範圍。

stronghold:allow-save-store-record

啟用 save_store_record 命令,而無需任何預先設定的範圍。

stronghold:deny-save-store-record

拒絕 save_store_record 命令,而無需任何預先設定的範圍。


© 2025 Tauri Contributors. CC-BY / MIT