1
0
Fork 0
passwordhash/ui/zenity.ts

66 lines
1.8 KiB
TypeScript

// UI based on zenity dialogs
import { Hasher } from "../hashing.ts";
async function askInput(label: string, password = false): Promise<string> {
const process = Deno.run({
cmd: ["zenity", password ? "--password" : "--entry", `--text=${label}`],
stdout: "piped",
});
const output = await process.output();
const result = new TextDecoder().decode(output).replace(/[ \n]+$/, "");
return result;
}
async function showMessage(message: string): Promise<void> {
const process = Deno.run({
cmd: ["zenity", "--info", `--text=${message}`],
});
await process.status();
}
async function copyToClipboard(text: string): Promise<void> {
const process = Deno.run({
cmd: ["xclip"],
stdin: "piped",
});
if (process.stdin) {
await Deno.writeAll(process.stdin, new TextEncoder().encode(text));
process.stdin.close();
}
await process.status();
}
async function readPrivateKey(): Promise<string> {
let content: string;
try {
content = await Deno.readTextFile(".local/config.json");
} catch {
content = await Deno.readTextFile(
`${Deno.env.get("HOME")}/.config/thunderk-passwordhash.json`,
);
}
const config = JSON.parse(content);
return config.privateKey;
}
export async function showZenityUI(): Promise<void> {
const privateKey = await readPrivateKey();
const siteTag = await askInput("site tag:");
let password = await askInput("password:", true);
if (password.slice(password.length - 1) != "#") {
password += "#";
}
const hasher = new Hasher(privateKey);
hasher.site_tag = siteTag;
const result = hasher.tryHashing(password);
if (result) {
await copyToClipboard(result);
await showMessage(`Your hashed password (copied to clipboard): ${result}`);
} else {
await showMessage(`Error in hashing password`);
}
}