first WIP
This commit is contained in:
parent
7eef2c3ca5
commit
1000c4efc0
112
main.ts
112
main.ts
@ -1,4 +1,5 @@
|
||||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
import { App, MarkdownView, Plugin, PluginSettingTab, Setting } from "obsidian";
|
||||
import { encryptMarkdown } from "src/encryption";
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
@ -7,8 +8,8 @@ interface MyPluginSettings {
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: MyPluginSettings = {
|
||||
mySetting: 'default'
|
||||
}
|
||||
mySetting: "default",
|
||||
};
|
||||
|
||||
export default class MyPlugin extends Plugin {
|
||||
settings: MyPluginSettings;
|
||||
@ -16,74 +17,45 @@ export default class MyPlugin extends Plugin {
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
// This creates an icon in the left ribbon.
|
||||
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
new Notice('This is a notice!');
|
||||
});
|
||||
// Perform additional things with the ribbon
|
||||
ribbonIconEl.addClass('my-plugin-ribbon-class');
|
||||
|
||||
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||
const statusBarItemEl = this.addStatusBarItem();
|
||||
statusBarItemEl.setText('Status Bar Text');
|
||||
|
||||
// This adds a simple command that can be triggered anywhere
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-simple',
|
||||
name: 'Open sample modal (simple)',
|
||||
callback: () => {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
});
|
||||
// This adds an editor command that can perform some operation on the current editor instance
|
||||
this.addCommand({
|
||||
id: 'sample-editor-command',
|
||||
name: 'Sample editor command',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
console.log(editor.getSelection());
|
||||
editor.replaceSelection('Sample Editor Command');
|
||||
}
|
||||
});
|
||||
// This adds a complex command that can check whether the current state of the app allows execution of the command
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-complex',
|
||||
name: 'Open sample modal (complex)',
|
||||
id: "open-sample-modal-complex",
|
||||
name: "Open sample modal (complex)",
|
||||
checkCallback: (checking: boolean) => {
|
||||
// Conditions to check
|
||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
const markdownView =
|
||||
this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
// If checking is true, we're simply "checking" if the command can be run.
|
||||
// If checking is false, then we want to actually perform the operation.
|
||||
if (!checking) {
|
||||
new SampleModal(this.app).open();
|
||||
this.shareNote(markdownView);
|
||||
}
|
||||
|
||||
// This command will only show up in Command Palette when the check function returns true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
||||
|
||||
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
|
||||
// Using this function will automatically remove the event listener when this plugin is disabled.
|
||||
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
||||
console.log('click', evt);
|
||||
});
|
||||
|
||||
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
|
||||
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
|
||||
}
|
||||
|
||||
onunload() {
|
||||
|
||||
// ENCRYPT AND SHARE MD NOTE
|
||||
private shareNote(mdView: MarkdownView) {
|
||||
const cryptData = encryptMarkdown(mdView);
|
||||
console.log(cryptData);
|
||||
}
|
||||
|
||||
onunload() {}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
this.settings = Object.assign(
|
||||
{},
|
||||
DEFAULT_SETTINGS,
|
||||
await this.loadData()
|
||||
);
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
@ -91,22 +63,6 @@ export default class MyPlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
class SampleModal extends Modal {
|
||||
constructor(app: App) {
|
||||
super(app);
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
const {contentEl} = this;
|
||||
contentEl.setText('Woah!');
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const {contentEl} = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
|
||||
class SampleSettingTab extends PluginSettingTab {
|
||||
plugin: MyPlugin;
|
||||
|
||||
@ -116,22 +72,24 @@ class SampleSettingTab extends PluginSettingTab {
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
const { containerEl } = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
|
||||
containerEl.createEl("h2", { text: "Settings for my awesome plugin." });
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Setting #1')
|
||||
.setDesc('It\'s a secret')
|
||||
.addText(text => text
|
||||
.setPlaceholder('Enter your secret')
|
||||
.setValue(this.plugin.settings.mySetting)
|
||||
.onChange(async (value) => {
|
||||
console.log('Secret: ' + value);
|
||||
this.plugin.settings.mySetting = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
.setName("Setting #1")
|
||||
.setDesc("It's a secret")
|
||||
.addText((text) =>
|
||||
text
|
||||
.setPlaceholder("Enter your secret")
|
||||
.setValue(this.plugin.settings.mySetting)
|
||||
.onChange(async (value) => {
|
||||
console.log("Secret: " + value);
|
||||
this.plugin.settings.mySetting = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"id": "obsidian-sample-plugin",
|
||||
"name": "Sample Plugin",
|
||||
"name": "Obsidian Note Sharing (working title)",
|
||||
"version": "1.0.1",
|
||||
"minAppVersion": "0.12.0",
|
||||
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
|
||||
|
5025
package-lock.json
generated
Normal file
5025
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
10
package.json
10
package.json
@ -6,19 +6,27 @@
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
"test": "vitest",
|
||||
"coverage": "vitest run --coverage",
|
||||
"version": "node version-bump.mjs && git add manifest.json versions.json"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/crypto-js": "^4.1.1",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.2.0",
|
||||
"@typescript-eslint/parser": "^5.2.0",
|
||||
"builtin-modules": "^3.2.0",
|
||||
"c8": "^7.11.3",
|
||||
"esbuild": "0.13.12",
|
||||
"obsidian": "latest",
|
||||
"tslib": "2.3.1",
|
||||
"typescript": "4.4.4"
|
||||
"typescript": "4.4.4",
|
||||
"vitest": "^0.15.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"crypto-js": "^4.1.1"
|
||||
}
|
||||
}
|
||||
|
38
src/crypto.test.ts
Normal file
38
src/crypto.test.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { encryptString, decryptString, generateKey } from "./crypto";
|
||||
|
||||
const testKey = "0123456789ABCDEF";
|
||||
const testData = "This is the test data.";
|
||||
const derivedTestKey = "1UPCi_Wvhl8EsfW2cERtOL9KB5RbZkmmIa5wMrLLz6E";
|
||||
|
||||
describe("Encryption suite", () => {
|
||||
it("should generate 256-bit keys", () => {
|
||||
const key = generateKey(testData);
|
||||
expect(key).toHaveLength(43);
|
||||
});
|
||||
|
||||
it("should generate deterministic 256-bit keys from seed material", () => {
|
||||
const key = generateKey(testData);
|
||||
expect(key).toEqual(derivedTestKey);
|
||||
});
|
||||
|
||||
it("should encrypt", () => {
|
||||
const encryptedData = encryptString(testData, testKey);
|
||||
expect(encryptedData).toHaveProperty("ciphertext");
|
||||
expect(encryptedData).toHaveProperty("hmac");
|
||||
});
|
||||
|
||||
it("should decrypt encrypted data with the correct key", () => {
|
||||
const encryptedData = encryptString(testData, testKey);
|
||||
const data = decryptString(encryptedData, testKey);
|
||||
expect(data).toEqual(testData);
|
||||
});
|
||||
|
||||
it("should fail decrypting with wrong key", () => {
|
||||
const ciphertext = encryptString(testData, testKey);
|
||||
const tempKey = generateKey("wrong key");
|
||||
expect(() => {
|
||||
decryptString(ciphertext, tempKey);
|
||||
}).toThrowError(/Cannot decrypt ciphertext with this key./g);
|
||||
});
|
||||
});
|
40
src/crypto.ts
Normal file
40
src/crypto.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { AES, PBKDF2, HmacSHA256, enc } from "crypto-js";
|
||||
|
||||
interface EncryptedData {
|
||||
ciphertext: string;
|
||||
hmac: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a 256-bit key from a
|
||||
* Note: I don't add a salt because the key will be derived from a different
|
||||
* passphrase for every shared note anyways..
|
||||
* @param seed passphrase-like data to generate the key from.
|
||||
*/
|
||||
export function generateKey(seed: string): string {
|
||||
const salt = "";
|
||||
const key256 = PBKDF2(seed, salt, { keySize: 256 / 32 });
|
||||
return key256.toString(enc.Base64url);
|
||||
}
|
||||
|
||||
export function encryptString(md: string, key: string): EncryptedData {
|
||||
// Generate message authentication code
|
||||
const msg = enc.Utf8.parse(md);
|
||||
const ciphertext = AES.encrypt(msg, key).toString();
|
||||
const hmac = HmacSHA256(ciphertext, key).toString();
|
||||
return { ciphertext, hmac };
|
||||
}
|
||||
|
||||
export function decryptString(
|
||||
{ ciphertext, hmac }: EncryptedData,
|
||||
key: string
|
||||
): string {
|
||||
const hmac_calculated = HmacSHA256(ciphertext, key).toString();
|
||||
const is_authentic = hmac_calculated == hmac;
|
||||
|
||||
if (!is_authentic) {
|
||||
throw Error("Cannot decrypt ciphertext with this key.");
|
||||
}
|
||||
const md = AES.decrypt(ciphertext, key).toString(enc.Utf8);
|
||||
return md;
|
||||
}
|
15
src/encryption.ts
Normal file
15
src/encryption.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import { MarkdownView, moment } from "obsidian";
|
||||
import { generateKey, encryptString } from "./crypto";
|
||||
|
||||
interface encryptedMarkdown {
|
||||
ciphertext: string;
|
||||
hmac: string;
|
||||
key: string;
|
||||
}
|
||||
|
||||
export function encryptMarkdown(mdView: MarkdownView): encryptedMarkdown {
|
||||
const plaintext = mdView.getViewData();
|
||||
const key = generateKey(moment.now() + plaintext);
|
||||
const { ciphertext, hmac } = encryptString(plaintext, key);
|
||||
return { ciphertext, hmac, key };
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
/* Sets all the text color to red! */
|
||||
body {
|
||||
color: red;
|
||||
}
|
7
vite.config.ts
Normal file
7
vite.config.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
// ...
|
||||
},
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user