Compare commits

...
3 Commits
Author SHA1 Message Date
fyears 5e3d413b33 bump ver 2021-11-03 00:58:48 +08:00
fyears bcf8586fbf use hash-wasm 2021-11-03 00:57:57 +08:00
fyears 6431182ec3 promisify data 2021-11-03 00:15:23 +08:00
6 changed files with 47 additions and 38 deletions
+1 -1
View File
@@ -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.5",
"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
View File
@@ -1,6 +1,6 @@
{ {
"name": "obsidian-save-remote", "name": "obsidian-save-remote",
"version": "0.0.4", "version": "0.0.5",
"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",
+36 -28
View File
@@ -1,25 +1,28 @@
import * as crypto from "crypto"; import { randomBytes, createDecipheriv, createCipheriv } from "crypto";
import { pbkdf2, createSHA256 } from "hash-wasm";
import { promisify } from "util";
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 = ( export const encryptBuffer = async (
buf: Buffer, buf: Buffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const salt = crypto.randomBytes(8); const salt = await promisify(randomBytes)(8);
const derivedKey = crypto.pbkdf2Sync( const derivedKey = await pbkdf2({
password, password: password,
salt, salt: salt,
rounds, iterations: rounds,
32 + 16, hashLength: 32 + 16,
"sha256" hashFunction: createSHA256(),
); outputType: "binary",
});
const key = derivedKey.slice(0, 32); const key = derivedKey.slice(0, 32);
const iv = derivedKey.slice(32, 32 + 16); const iv = derivedKey.slice(32, 32 + 16);
const cipher = crypto.createCipheriv("aes-256-cbc", key, iv); const cipher = createCipheriv("aes-256-cbc", key, iv);
cipher.write(buf); cipher.write(buf);
cipher.end(); cipher.end();
const encrypted = cipher.read(); const encrypted = cipher.read();
@@ -27,65 +30,70 @@ export const encryptBuffer = (
return res; return res;
}; };
export const decryptBuffer = ( export const decryptBuffer = async (
buf: Buffer, buf: Buffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const prefix = buf.slice(0, 8); const prefix = buf.slice(0, 8);
const salt = buf.slice(8, 16); const salt = buf.slice(8, 16);
const derivedKey = crypto.pbkdf2Sync( const derivedKey = await pbkdf2({
password, password: password,
salt, salt: salt,
rounds, iterations: rounds,
32 + 16, hashLength: 32 + 16,
"sha256" hashFunction: createSHA256(),
); outputType: "binary",
});
const key = derivedKey.slice(0, 32); const key = derivedKey.slice(0, 32);
const iv = derivedKey.slice(32, 32 + 16); const iv = derivedKey.slice(32, 32 + 16);
const decipher = crypto.createDecipheriv("aes-256-cbc", key, iv); const decipher = createDecipheriv("aes-256-cbc", key, iv);
decipher.write(buf.slice(16)); decipher.write(buf.slice(16));
decipher.end(); decipher.end();
const decrypted = decipher.read(); const decrypted = decipher.read();
return decrypted as Buffer; return decrypted as Buffer;
}; };
export const encryptArrayBuffer = ( export const encryptArrayBuffer = async (
arrBuf: ArrayBuffer, arrBuf: ArrayBuffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return bufferToArrayBuffer( return bufferToArrayBuffer(
encryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds) await encryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds)
); );
}; };
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( return bufferToArrayBuffer(
decryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds) await decryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds)
); );
}; };
export const encryptStringToBase32 = ( export const encryptStringToBase32 = async (
text: string, text: string,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return base32.encode(encryptBuffer(Buffer.from(text), password, rounds)); return base32.encode(
await encryptBuffer(Buffer.from(text), password, rounds)
);
}; };
export const decryptBase32ToString = ( export const decryptBase32ToString = async (
text: string, text: string,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
return decryptBuffer( return (
await decryptBuffer(
Buffer.from(base32.decode.asBytes(text)), Buffer.from(base32.decode.asBytes(text)),
password, password,
rounds rounds
)
).toString(); ).toString();
}; };
+2 -2
View File
@@ -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,
+2 -2
View File
@@ -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);
} }
} }
+1 -1
View File
@@ -1,3 +1,3 @@
{ {
"0.0.4": "0.12.15" "0.0.5": "0.12.15"
} }