Compare commits

...
7 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
fyears cef623f493 bump ver 2021-11-02 10:14:11 +08:00
fyears 0c00c1a2e3 no need cryptojs? 2021-11-02 10:14:02 +08:00
fyears ab8805f272 use default enc 2021-11-02 10:11:45 +08:00
fyears da180fcc92 overwrite buffer 2021-11-02 10:07:00 +08:00
7 changed files with 77 additions and 81 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.3", "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 -3
View File
@@ -1,6 +1,6 @@
{ {
"name": "obsidian-save-remote", "name": "obsidian-save-remote",
"version": "0.0.1", "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",
@@ -13,7 +13,6 @@
"author": "", "author": "",
"license": "Apache-2.0", "license": "Apache-2.0",
"devDependencies": { "devDependencies": {
"@types/crypto-js": "^4.0.2",
"@types/node": "^14.14.37", "@types/node": "^14.14.37",
"prettier": "^2.4.1", "prettier": "^2.4.1",
"terser-webpack-plugin": "^5.2.4", "terser-webpack-plugin": "^5.2.4",
@@ -37,9 +36,9 @@
"console-browserify": "^1.2.0", "console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0", "constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0", "crypto-browserify": "^3.12.0",
"crypto-js": "^4.1.1",
"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",
+57 -71
View File
@@ -1,113 +1,99 @@
import * as CryptoJS from "crypto-js"; 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 { import { bufferToArrayBuffer, arrayBufferToBuffer } from "./misc";
bufferToArrayBuffer,
arrayBufferToBuffer,
arrayBufferToBase64,
base64ToArrayBuffer,
} from "./misc";
const DEFAULT_ITER = 10000; const DEFAULT_ITER = 10000;
export const encryptWordArray = ( export const encryptBuffer = async (
wa: CryptoJS.lib.WordArray, buf: Buffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const prefix = CryptoJS.enc.Utf8.parse("Salted__"); const salt = await promisify(randomBytes)(8);
const salt = CryptoJS.lib.WordArray.random(8); const derivedKey = await pbkdf2({
const derivedKey = CryptoJS.PBKDF2(password, salt, { password: password,
keySize: 32 + 16, salt: salt,
iterations: rounds, iterations: rounds,
hasher: CryptoJS.algo.SHA256, hashLength: 32 + 16,
hashFunction: createSHA256(),
outputType: "binary",
}); });
const key = CryptoJS.lib.WordArray.create(derivedKey.words.slice(0, 32 / 4)); const key = derivedKey.slice(0, 32);
const iv = CryptoJS.lib.WordArray.create( const iv = derivedKey.slice(32, 32 + 16);
derivedKey.words.slice(32 / 4, (32 + 16) / 4) const cipher = createCipheriv("aes-256-cbc", key, iv);
); cipher.write(buf);
const encrypted = CryptoJS.AES.encrypt(wa, key, { iv: iv }).ciphertext; cipher.end();
const res = CryptoJS.lib.WordArray.create() const encrypted = cipher.read();
.concat(prefix) const res = Buffer.concat([Buffer.from("Salted__"), salt, encrypted]);
.concat(salt)
.concat(encrypted);
return res; return res;
}; };
export const decryptWordArray = ( export const decryptBuffer = async (
wa: CryptoJS.lib.WordArray, buf: Buffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const prefix = CryptoJS.lib.WordArray.create(wa.words.slice(0, 8 / 4)); const prefix = buf.slice(0, 8);
const salt = buf.slice(8, 16);
const salt = CryptoJS.lib.WordArray.create( const derivedKey = await pbkdf2({
wa.words.slice(8 / 4, (8 + 8) / 4) password: password,
); salt: salt,
const derivedKey = CryptoJS.PBKDF2(password, salt, {
keySize: 32 + 16,
iterations: rounds, iterations: rounds,
hasher: CryptoJS.algo.SHA256, hashLength: 32 + 16,
hashFunction: createSHA256(),
outputType: "binary",
}); });
const key = CryptoJS.lib.WordArray.create(derivedKey.words.slice(0, 32 / 4)); const key = derivedKey.slice(0, 32);
const iv = CryptoJS.lib.WordArray.create( const iv = derivedKey.slice(32, 32 + 16);
derivedKey.words.slice(32 / 4, 32 / 4 + 16 / 4) const decipher = createDecipheriv("aes-256-cbc", key, iv);
); decipher.write(buf.slice(16));
const decrypted = CryptoJS.AES.decrypt( decipher.end();
CryptoJS.lib.CipherParams.create({ const decrypted = decipher.read();
ciphertext: CryptoJS.lib.WordArray.create(wa.words.slice((8 + 8) / 4)), return decrypted as Buffer;
}),
key,
{ iv: iv }
);
return decrypted;
}; };
export const encryptArrayBuffer = ( export const encryptArrayBuffer = async (
arrBuf: ArrayBuffer, arrBuf: ArrayBuffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const b64 = arrayBufferToBase64(arrBuf); return bufferToArrayBuffer(
const wa = CryptoJS.enc.Base64.parse(b64); await encryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds)
const enc = encryptWordArray(wa, password, rounds); );
const resb64 = CryptoJS.enc.Base64.stringify(enc);
const res = base64ToArrayBuffer(resb64);
return res;
}; };
export const decryptArrayBuffer = ( export const decryptArrayBuffer = async (
arrBuf: ArrayBuffer, arrBuf: ArrayBuffer,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const b64 = arrayBufferToBase64(arrBuf); return bufferToArrayBuffer(
const wa = CryptoJS.enc.Base64.parse(b64); await decryptBuffer(arrayBufferToBuffer(arrBuf), password, rounds)
const dec = decryptWordArray(wa, password, rounds); );
const resb64 = CryptoJS.enc.Base64.stringify(dec);
const res = base64ToArrayBuffer(resb64);
return res;
}; };
export const encryptStringToBase32 = ( export const encryptStringToBase32 = async (
text: string, text: string,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const wa = CryptoJS.enc.Utf8.parse(text); return base32.encode(
const enc = encryptWordArray(wa, password, rounds); await encryptBuffer(Buffer.from(text), password, rounds)
const enctext = CryptoJS.enc.Base64.stringify(enc); );
const res = base32.encode(base64ToArrayBuffer(enctext));
return res;
}; };
export const decryptBase32ToString = ( export const decryptBase32ToString = async (
text: string, text: string,
password: string, password: string,
rounds: number = DEFAULT_ITER rounds: number = DEFAULT_ITER
) => { ) => {
const enc = Buffer.from(base32.decode.asBytes(text)).toString("base64"); return (
const wa = CryptoJS.enc.Base64.parse(enc); await decryptBuffer(
const dec = decryptWordArray(wa, password, rounds); Buffer.from(base32.decode.asBytes(text)),
const dectext = CryptoJS.enc.Utf8.stringify(dec); password,
return dectext; rounds
)
).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.3": "0.12.15" "0.0.5": "0.12.15"
} }
+12 -1
View File
@@ -1,4 +1,5 @@
const path = require("path"); const path = require("path");
const webpack = require("webpack");
const TerserPlugin = require("terser-webpack-plugin"); const TerserPlugin = require("terser-webpack-plugin");
module.exports = { module.exports = {
@@ -9,6 +10,16 @@ module.exports = {
path: __dirname, path: __dirname,
libraryTarget: "commonjs", libraryTarget: "commonjs",
}, },
plugins: [
// Work around for Buffer is undefined:
// https://github.com/webpack/changelog-v5/issues/10
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
}),
new webpack.ProvidePlugin({
process: "process/browser",
}),
],
module: { module: {
rules: [ rules: [
{ {
@@ -20,7 +31,7 @@ module.exports = {
}, },
resolve: { resolve: {
extensions: [".tsx", ".ts", ".js"], extensions: [".tsx", ".ts", ".js"],
mainFields: ["module", "main"], mainFields: ["browser", "module", "main"],
fallback: { fallback: {
assert: require.resolve("assert"), assert: require.resolve("assert"),
buffer: require.resolve("buffer/"), buffer: require.resolve("buffer/"),