Skip to content

Instantly share code, notes, and snippets.

@p1mo
Last active February 22, 2024 23:45
Show Gist options
  • Save p1mo/ec03a9104c00dae6c7b68c02b24bee1e to your computer and use it in GitHub Desktop.
Save p1mo/ec03a9104c00dae6c7b68c02b24bee1e to your computer and use it in GitHub Desktop.
Example for Tauri's stronghold plugin

example for tauri's stronghold-plugin

First go to plugin docs and add it to your project

Aragon create used: https://crates.io/crates/rust-argon2

src-tauri/main.rs

use argon2::{ Config, ThreadMode, Variant, Version, hash_raw };

fn main (){
    tauri::Builder::default()
        .plugin(tauri_plugin_stronghold::Builder::new(move |password| {
            
            // Aragon config
            let config = Config {
                lanes       : 4,
                mem_cost    : 10_000,
                time_cost   : 10,
                thread_mode : ThreadMode::from_threads(4),
                variant     : Variant::Argon2id,
                version     : Version::Version13,
                ..Default::default()
            };
            
            // hash password
            let key = hash_raw(password.as_ref(), "my-salt".as_bytes(), &config).expect("failed to hash password");
    
            // return the hash
            key.to_vec()
    
        }).build())
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

client/main.js

import { Stronghold } from "tauri-plugin-stronghold-api";

// Load Stronghold
const password = "my_pass"; // your Stronghold (to open it)
const fullPath = "my/path/to/stronghold"; // should be your app_data dir
const stronghold = await Stronghold.load(fullPath, password)

// Create or Load a client
const client_name = "my_client";

const client = await stronghold.createClient(client_name).getStore()
// or
const client = await stronghold.loadClient(client_name).getStore()

// EXAMPLE DATA
const DATA = {
    key : "my-key",
    value : "my-super-secret",
}

// Read data
const value     = await client.get(DATA.key);
const decoded   = new TextDecoder().decode(new Uint8Array(value ?? []));

// Write data
await client.insert(DATA.key, Array.from(new TextEncoder().encode(DATA.value)));

// Save written data
await stronghold.save();

// Close Database
await stronghold.unload();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment