Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
60d5b78df0 | ||
|
|
f04017e05b | ||
|
|
cb0098ec85 | ||
|
|
d007d5f230 | ||
|
|
07c6fbe747 | ||
|
|
5e3d413b33 | ||
|
|
bcf8586fbf | ||
|
|
6431182ec3 |
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"id": "obsdian-save-remote",
|
"id": "obsdian-save-remote",
|
||||||
"name": "Save remote",
|
"name": "Save remote",
|
||||||
"version": "0.0.4",
|
"version": "0.0.6",
|
||||||
"minAppVersion": "0.12.15",
|
"minAppVersion": "0.12.15",
|
||||||
"description": "This is yet another plugin allowing users to sync notes between local device and the cloud.",
|
"description": "This is yet another plugin allowing users to sync notes between local device and the cloud.",
|
||||||
"author": "fyears",
|
"author": "fyears",
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "obsidian-save-remote",
|
"name": "obsidian-save-remote",
|
||||||
"version": "0.0.4",
|
"version": "0.0.6",
|
||||||
"description": "This is yet another sync plugin for Obsidian app.",
|
"description": "This is yet another sync plugin for Obsidian app.",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "webpack --mode development --watch",
|
"dev": "webpack --mode development --watch",
|
||||||
@@ -38,6 +38,7 @@
|
|||||||
"crypto-browserify": "^3.12.0",
|
"crypto-browserify": "^3.12.0",
|
||||||
"domain-browser": "^4.22.0",
|
"domain-browser": "^4.22.0",
|
||||||
"events": "^3.3.0",
|
"events": "^3.3.0",
|
||||||
|
"hash-wasm": "^4.9.0",
|
||||||
"hi-base32": "^0.5.1",
|
"hi-base32": "^0.5.1",
|
||||||
"https-browserify": "^1.0.0",
|
"https-browserify": "^1.0.0",
|
||||||
"lovefield-ts": "^0.7.0",
|
"lovefield-ts": "^0.7.0",
|
||||||
|
|||||||
+99
-65
@@ -1,91 +1,125 @@
|
|||||||
import * as crypto from "crypto";
|
|
||||||
import * as base32 from "hi-base32";
|
import * as base32 from "hi-base32";
|
||||||
import { bufferToArrayBuffer, arrayBufferToBuffer } from "./misc";
|
import { bufferToArrayBuffer, arrayBufferToBuffer } from "./misc";
|
||||||
|
|
||||||
const DEFAULT_ITER = 10000;
|
const DEFAULT_ITER = 10000;
|
||||||
|
|
||||||
export const encryptBuffer = (
|
const getKeyIVFromPassword = async (
|
||||||
buf: Buffer,
|
salt: Uint8Array,
|
||||||
password: string,
|
password: string,
|
||||||
rounds: number = DEFAULT_ITER
|
rounds: number = DEFAULT_ITER
|
||||||
) => {
|
) => {
|
||||||
const salt = crypto.randomBytes(8);
|
const k1 = await window.crypto.subtle.importKey(
|
||||||
const derivedKey = crypto.pbkdf2Sync(
|
"raw",
|
||||||
password,
|
new TextEncoder().encode(password),
|
||||||
salt,
|
{ name: "PBKDF2" },
|
||||||
rounds,
|
false,
|
||||||
32 + 16,
|
["deriveKey", "deriveBits"]
|
||||||
"sha256"
|
|
||||||
);
|
);
|
||||||
const key = derivedKey.slice(0, 32);
|
|
||||||
const iv = derivedKey.slice(32, 32 + 16);
|
const k2 = await window.crypto.subtle.deriveBits(
|
||||||
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv);
|
{
|
||||||
cipher.write(buf);
|
name: "PBKDF2",
|
||||||
cipher.end();
|
salt: salt,
|
||||||
const encrypted = cipher.read();
|
iterations: rounds,
|
||||||
const res = Buffer.concat([Buffer.from("Salted__"), salt, encrypted]);
|
hash: "SHA-256",
|
||||||
return res;
|
},
|
||||||
|
k1,
|
||||||
|
256 + 128
|
||||||
|
);
|
||||||
|
|
||||||
|
return k2;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const decryptBuffer = (
|
export const encryptArrayBuffer = async (
|
||||||
buf: Buffer,
|
|
||||||
password: string,
|
|
||||||
rounds: number = DEFAULT_ITER
|
|
||||||
) => {
|
|
||||||
const prefix = buf.slice(0, 8);
|
|
||||||
const salt = buf.slice(8, 16);
|
|
||||||
const derivedKey = crypto.pbkdf2Sync(
|
|
||||||
password,
|
|
||||||
salt,
|
|
||||||
rounds,
|
|
||||||
32 + 16,
|
|
||||||
"sha256"
|
|
||||||
);
|
|
||||||
const key = derivedKey.slice(0, 32);
|
|
||||||
const iv = derivedKey.slice(32, 32 + 16);
|
|
||||||
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv);
|
|
||||||
decipher.write(buf.slice(16));
|
|
||||||
decipher.end();
|
|
||||||
const decrypted = decipher.read();
|
|
||||||
return decrypted as Buffer;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const encryptArrayBuffer = (
|
|
||||||
arrBuf: ArrayBuffer,
|
arrBuf: ArrayBuffer,
|
||||||
password: string,
|
password: string,
|
||||||
rounds: number = DEFAULT_ITER
|
rounds: number = DEFAULT_ITER
|
||||||
) => {
|
) => {
|
||||||
return bufferToArrayBuffer(
|
const salt = window.crypto.getRandomValues(new Uint8Array(8));
|
||||||
encryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds)
|
|
||||||
|
const derivedKey = await getKeyIVFromPassword(salt, password, rounds);
|
||||||
|
const key = derivedKey.slice(0, 32);
|
||||||
|
const iv = derivedKey.slice(32, 32 + 16);
|
||||||
|
|
||||||
|
const keyCrypt = await window.crypto.subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
key,
|
||||||
|
{ name: "AES-CBC" },
|
||||||
|
false,
|
||||||
|
["encrypt", "decrypt"]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const enc = (await window.crypto.subtle.encrypt(
|
||||||
|
{ name: "AES-CBC", iv },
|
||||||
|
keyCrypt,
|
||||||
|
arrBuf
|
||||||
|
)) as ArrayBuffer;
|
||||||
|
|
||||||
|
const prefix = new TextEncoder().encode("Salted__");
|
||||||
|
|
||||||
|
const res = new Uint8Array(
|
||||||
|
[
|
||||||
|
...prefix,
|
||||||
|
...salt,
|
||||||
|
...new Uint8Array(enc)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return bufferToArrayBuffer(res);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const decryptArrayBuffer = (
|
export const decryptArrayBuffer = async (
|
||||||
arrBuf: ArrayBuffer,
|
arrBuf: ArrayBuffer,
|
||||||
password: string,
|
password: string,
|
||||||
rounds: number = DEFAULT_ITER
|
rounds: number = DEFAULT_ITER
|
||||||
) => {
|
) => {
|
||||||
return bufferToArrayBuffer(
|
const prefix = arrBuf.slice(0, 8);
|
||||||
decryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds)
|
const salt = arrBuf.slice(8, 16);
|
||||||
);
|
const derivedKey = await getKeyIVFromPassword(
|
||||||
};
|
new Uint8Array(salt),
|
||||||
|
|
||||||
export const encryptStringToBase32 = (
|
|
||||||
text: string,
|
|
||||||
password: string,
|
|
||||||
rounds: number = DEFAULT_ITER
|
|
||||||
) => {
|
|
||||||
return base32.encode(encryptBuffer(Buffer.from(text), password, rounds));
|
|
||||||
};
|
|
||||||
|
|
||||||
export const decryptBase32ToString = (
|
|
||||||
text: string,
|
|
||||||
password: string,
|
|
||||||
rounds: number = DEFAULT_ITER
|
|
||||||
) => {
|
|
||||||
return decryptBuffer(
|
|
||||||
Buffer.from(base32.decode.asBytes(text)),
|
|
||||||
password,
|
password,
|
||||||
rounds
|
rounds
|
||||||
|
);
|
||||||
|
const key = derivedKey.slice(0, 32);
|
||||||
|
const iv = derivedKey.slice(32, 32 + 16);
|
||||||
|
|
||||||
|
const keyCrypt = await window.crypto.subtle.importKey(
|
||||||
|
"raw",
|
||||||
|
key,
|
||||||
|
{ name: "AES-CBC" },
|
||||||
|
false,
|
||||||
|
["encrypt", "decrypt"]
|
||||||
|
);
|
||||||
|
|
||||||
|
const dec = (await window.crypto.subtle.decrypt(
|
||||||
|
{ name: "AES-CBC", iv },
|
||||||
|
keyCrypt,
|
||||||
|
arrBuf.slice(16)
|
||||||
|
)) as ArrayBuffer;
|
||||||
|
|
||||||
|
return dec;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const encryptStringToBase32 = async (
|
||||||
|
text: string,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER
|
||||||
|
) => {
|
||||||
|
return base32.encode(
|
||||||
|
await encryptArrayBuffer(new TextEncoder().encode(text), password, rounds)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const decryptBase32ToString = async (
|
||||||
|
text: string,
|
||||||
|
password: string,
|
||||||
|
rounds: number = DEFAULT_ITER
|
||||||
|
) => {
|
||||||
|
return (
|
||||||
|
await decryptArrayBuffer(
|
||||||
|
bufferToArrayBuffer(Uint8Array.from(base32.decode.asBytes(text))),
|
||||||
|
password,
|
||||||
|
rounds
|
||||||
|
)
|
||||||
).toString();
|
).toString();
|
||||||
};
|
};
|
||||||
|
|||||||
+13
-19
@@ -62,13 +62,6 @@ export default class SaveRemotePlugin extends Plugin {
|
|||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
// this.addRibbonIcon("dice", "Misc", async () => {
|
|
||||||
// const a = this.app.vault.getAllLoadedFiles();
|
|
||||||
// console.log(a);
|
|
||||||
// const h = await getAllRecords(this.db);
|
|
||||||
// console.log(h);
|
|
||||||
// });
|
|
||||||
|
|
||||||
this.addRibbonIcon("switch", "Save Remote", async () => {
|
this.addRibbonIcon("switch", "Save Remote", async () => {
|
||||||
if (this.syncStatus !== "idle") {
|
if (this.syncStatus !== "idle") {
|
||||||
new Notice("Save Remote already running!");
|
new Notice("Save Remote already running!");
|
||||||
@@ -221,18 +214,6 @@ class SaveRemoteSettingTab extends PluginSettingTab {
|
|||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
new Setting(containerEl)
|
|
||||||
.setName("password")
|
|
||||||
.setDesc("password")
|
|
||||||
.addText((text) =>
|
|
||||||
text
|
|
||||||
.setPlaceholder("")
|
|
||||||
.setValue(`${this.plugin.settings.password}`)
|
|
||||||
.onChange(async (value) => {
|
|
||||||
this.plugin.settings.password = value;
|
|
||||||
await this.plugin.saveSettings();
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
new Setting(containerEl)
|
new Setting(containerEl)
|
||||||
.setName("s3BucketName")
|
.setName("s3BucketName")
|
||||||
@@ -246,5 +227,18 @@ class SaveRemoteSettingTab extends PluginSettingTab {
|
|||||||
await this.plugin.saveSettings();
|
await this.plugin.saveSettings();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
new Setting(containerEl)
|
||||||
|
.setName("password")
|
||||||
|
.setDesc("password")
|
||||||
|
.addText((text) =>
|
||||||
|
text
|
||||||
|
.setPlaceholder("")
|
||||||
|
.setValue(`${this.plugin.settings.password}`)
|
||||||
|
.onChange(async (value) => {
|
||||||
|
this.plugin.settings.password = value;
|
||||||
|
await this.plugin.saveSettings();
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -46,7 +46,7 @@ export const mkdirpInVault = async (thePath: string, vault: Vault) => {
|
|||||||
* @param b Buffer
|
* @param b Buffer
|
||||||
* @returns ArrayBuffer
|
* @returns ArrayBuffer
|
||||||
*/
|
*/
|
||||||
export const bufferToArrayBuffer = (b: Buffer) => {
|
export const bufferToArrayBuffer = (b: Buffer | Uint8Array) => {
|
||||||
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
return b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ export const uploadToRemote = async (
|
|||||||
const localContent = await vault.adapter.readBinary(fileOrFolderPath);
|
const localContent = await vault.adapter.readBinary(fileOrFolderPath);
|
||||||
let remoteContent = localContent;
|
let remoteContent = localContent;
|
||||||
if (password !== "") {
|
if (password !== "") {
|
||||||
remoteContent = encryptArrayBuffer(localContent, password);
|
remoteContent = await encryptArrayBuffer(localContent, password);
|
||||||
}
|
}
|
||||||
const body = arrayBufferToBuffer(remoteContent);
|
const body = arrayBufferToBuffer(remoteContent);
|
||||||
await s3Client.send(
|
await s3Client.send(
|
||||||
@@ -214,7 +214,7 @@ export const downloadFromRemote = async (
|
|||||||
);
|
);
|
||||||
let localContent = remoteContent;
|
let localContent = remoteContent;
|
||||||
if (password !== "") {
|
if (password !== "") {
|
||||||
localContent = decryptArrayBuffer(remoteContent, password);
|
localContent = await decryptArrayBuffer(remoteContent, password);
|
||||||
}
|
}
|
||||||
await vault.adapter.writeBinary(fileOrFolderPath, localContent, {
|
await vault.adapter.writeBinary(fileOrFolderPath, localContent, {
|
||||||
mtime: mtime,
|
mtime: mtime,
|
||||||
|
|||||||
+9
-3
@@ -62,7 +62,7 @@ export const ensembleMixedStates = async (
|
|||||||
const remoteEncryptedKey = entry.Key;
|
const remoteEncryptedKey = entry.Key;
|
||||||
let key = remoteEncryptedKey;
|
let key = remoteEncryptedKey;
|
||||||
if (password !== "") {
|
if (password !== "") {
|
||||||
key = decryptBase32ToString(remoteEncryptedKey, password);
|
key = await decryptBase32ToString(remoteEncryptedKey, password);
|
||||||
}
|
}
|
||||||
const backwardMapping = await getSyncMetaMappingByRemoteKeyS3(
|
const backwardMapping = await getSyncMetaMappingByRemoteKeyS3(
|
||||||
db,
|
db,
|
||||||
@@ -299,7 +299,7 @@ export const doActualSync = async (
|
|||||||
if (password !== "") {
|
if (password !== "") {
|
||||||
remoteEncryptedKey = state.remote_encrypted_key;
|
remoteEncryptedKey = state.remote_encrypted_key;
|
||||||
if (remoteEncryptedKey === undefined || remoteEncryptedKey === "") {
|
if (remoteEncryptedKey === undefined || remoteEncryptedKey === "") {
|
||||||
remoteEncryptedKey = encryptStringToBase32(key, password);
|
remoteEncryptedKey = await encryptStringToBase32(key, password);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -355,7 +355,13 @@ export const doActualSync = async (
|
|||||||
remoteEncryptedKey
|
remoteEncryptedKey
|
||||||
);
|
);
|
||||||
} else if (state.decision === "delremote_clearhist") {
|
} else if (state.decision === "delremote_clearhist") {
|
||||||
await deleteFromRemote(s3Client, s3Config, state.key);
|
await deleteFromRemote(
|
||||||
|
s3Client,
|
||||||
|
s3Config,
|
||||||
|
state.key,
|
||||||
|
password,
|
||||||
|
remoteEncryptedKey
|
||||||
|
);
|
||||||
await clearDeleteRenameHistoryOfKey(db, state.key);
|
await clearDeleteRenameHistoryOfKey(db, state.key);
|
||||||
} else if (state.decision === "upload") {
|
} else if (state.decision === "upload") {
|
||||||
const remoteObjMeta = await uploadToRemote(
|
const remoteObjMeta = await uploadToRemote(
|
||||||
|
|||||||
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"0.0.4": "0.12.15"
|
"0.0.6": "0.12.15"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user