Sign and Encript now works

This commit is contained in:
Luis Alberto Jaramillo Gonzalez 2024-01-20 18:31:31 -05:00
parent 8c80230399
commit 52ba1f11e6
No known key found for this signature in database
GPG key ID: 0444CB0073EDFBB5
4 changed files with 14 additions and 10 deletions

7
gpg.ts
View file

@ -89,7 +89,7 @@ export async function getListPublicKey(exec: string): Promise<{ keyID: string; u
}
// Function to encrypt a plainText with a list of GPG public keys ID
export async function gpgEncrypt(exec: string, plainText:string, publicKeyIds: string[]): Promise<GpgResult> {
export async function gpgEncrypt(exec: string, plainText:string, publicKeyIds: string[], signPublicKeyId: string): Promise<GpgResult> {
// Check if at least one public key is selected
if (publicKeyIds.length <= 0) {
// And return with error message
@ -100,6 +100,11 @@ export async function gpgEncrypt(exec: string, plainText:string, publicKeyIds: s
}
// List of Args before publicKeyIds
let args: string[] = ["--encrypt", "--armor"];
// Check if Sign is necesary in this encryption
if (signPublicKeyId != "0") {
// Add args to Sign with a key
args = args.concat(["--sign", "--local-user", signPublicKeyId]);
}
// Iterate over each GPG public key ID
publicKeyIds.forEach((publicKey) => {
// Add to args this recipient GPG public key ID

View file

@ -4,13 +4,11 @@ import { Plugin } from 'obsidian';
interface GpgEncryptSettings {
pgpExecPath: string;
pgpRequireSign: boolean;
pgpSignPublicKeyId: string
}
const DEFAULT_SETTINGS: GpgEncryptSettings = {
pgpExecPath: '/usr/local/bin/gpg',
pgpRequireSign: false,
pgpSignPublicKeyId: "0"
}

View file

@ -10,6 +10,7 @@ export enum EncryptModalMode {
// Encrypt modal (Works for inline and document encryption)
export class EncryptModal extends Modal {
// List of public keys to encript text
private listPublicKeyToEncrypt: string[];
// Encrypt modal mode
@ -20,6 +21,7 @@ export class EncryptModal extends Modal {
private view: MarkdownView;
// Current plugin instance
plugin: GpgEncryptPlugin;
// Constructor of modal encrypt
constructor(app: App, plugin: GpgEncryptPlugin, mode: EncryptModalMode, editor: Editor, view: MarkdownView) {
super(app);
@ -29,6 +31,7 @@ export class EncryptModal extends Modal {
this.view = view;
this.listPublicKeyToEncrypt = [];
}
// OnOpen Method
async onOpen() {
// Get an instance of this Element
@ -80,6 +83,7 @@ export class EncryptModal extends Modal {
await this.EncryptText();
}));
}
// OnClose Method
onClose() {
// Get an instance of this Element
@ -87,12 +91,13 @@ export class EncryptModal extends Modal {
// Clear element
contentEl.empty();
}
// Method to encript text with previous configuration
private async EncryptText() {
// Check if EncryptMode is Inline
if (this.encryptMode == EncryptModalMode.INLINE) {
// Send Encrypt command with list of GPG public keys IDs
let encryptedTextResult: GpgResult = await gpgEncrypt(this.plugin.settings.pgpExecPath, this.editor.getSelection(), this.listPublicKeyToEncrypt);
let encryptedTextResult: GpgResult = await gpgEncrypt(this.plugin.settings.pgpExecPath, this.editor.getSelection(), this.listPublicKeyToEncrypt, this.plugin.settings.pgpSignPublicKeyId);
// Check if any error exists
if (encryptedTextResult.error) {
// Show the error message

View file

@ -62,7 +62,7 @@ export class GpgSettingsTab extends PluginSettingTab {
.setDesc("Sign the encrypted text with GPG")
.addToggle((toggle) => {
// Toggle component default value is false
toggle.setValue(this.plugin.settings.pgpRequireSign);
toggle.setValue(this.plugin.settings.pgpSignPublicKeyId != "0");
// Toggle component is created with onChange event
toggle.onChange((value: boolean) => {
// Call method to refresh list to Sign text
@ -74,7 +74,7 @@ export class GpgSettingsTab extends PluginSettingTab {
// ---------- GPG Key ID to Sign text ----------
this.gpgSignKeyId = new Setting(containerEl);
// Call method to refresh list to Sign text
this.RefreshListSign(this.plugin.settings.pgpRequireSign);
this.RefreshListSign(this.plugin.settings.pgpSignPublicKeyId != "0");
// ---------- GPG Key ID to Sign text ----------
}
@ -210,10 +210,6 @@ export class GpgSettingsTab extends PluginSettingTab {
// Function to refresh List of Sign Keys ID
private async RefreshListSign(requireSign: boolean) {
// Set settig variable pgpRequireSign with new value
this.plugin.settings.pgpRequireSign = requireSign;
// Save settings with change
await this.plugin.saveSettings();
// Clear gpgSignKeyId setting
this.gpgSignKeyId.clear();
// Re-Create gpgSignKeyId setting