Skip to content

Commit a304d8f

Browse files
committed
Electron fix encryption
1 parent 789b8f9 commit a304d8f

File tree

9 files changed

+66
-59
lines changed

9 files changed

+66
-59
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
# 5.0.5-1 (2023-07-01)
2+
3+
### Chore
4+
5+
- Update to @capacitor/core@5.0.5
6+
- Update to @capacitor/ios@5.0.5
7+
- Update to @capacitor/android@5.0.5
8+
9+
### Bug Fixes
10+
11+
- Electron fix encryption
12+
113
# 5.0.4 (2023-06-29)
214

315
### Removed Features
0 Bytes
Binary file not shown.

electron/src/electron-utils/utilsFile.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export class UtilsFile {
5353
this.isEncryption = this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
5454
? this.capConfig.plugins.CapacitorSQLite.electronIsEncryption
5555
: false;
56-
console.log(`$$$$$$ this.isEncryption: ${this.isEncryption} $$$$$$`);
5756
this.osType = this.Os.type();
5857
switch (this.osType) {
5958
case 'Darwin':
@@ -84,7 +83,6 @@ export class UtilsFile {
8483
* @returns
8584
*/
8685
public getIsEncryption(): boolean {
87-
console.log(`>>>>>> getIsEncryption this.isEncryption: ${this.isEncryption} >>>>>>`);
8886
return this.isEncryption;
8987
}
9088
/**
@@ -598,6 +596,26 @@ export class UtilsFile {
598596
fileStream.on('finish', resolve);
599597
});
600598
}
599+
public readFileAsPromise(path: string, options: {start: number, end: number}): Promise<string> {
600+
return new Promise((resolve, reject) => {
601+
const fileStream = this.NodeFs.createReadStream(path, options);
602+
603+
const chunks: any[] = [];
604+
fileStream.on('data', (data: any) => {
605+
chunks.push(data);
606+
});
607+
608+
fileStream.on('close', () => {
609+
resolve(chunks.toString());
610+
});
611+
612+
fileStream.on('error', (err:any) => {
613+
const msg = err.message ? err.message : err;
614+
reject(msg);
615+
});
616+
});
617+
}
618+
601619
/**
602620
* CreateFolderIfNotExists
603621
* Create directory

electron/src/electron-utils/utilsSQLite.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -947,13 +947,13 @@ export class UtilsSQLite {
947947
throw new Error(`${msg} ${errmsg}`);
948948
}
949949
}
950-
public isDatabaseEncrypted(dbName: string): boolean {
950+
public async isDatabaseEncrypted(dbName: string): Promise<boolean> {
951951
const msg = "isDatabaseEncrypted";
952952
try {
953953
const isExists: boolean = this.fileUtil.isFileExists(dbName);
954954
if(isExists) {
955-
const filePath = this.fileUtil.getFilePath(dbName + 'SQLite.db');
956-
return this.isDBEncrypted(filePath);
955+
const filePath = this.fileUtil.getFilePath(dbName);
956+
return await this.isDBEncrypted(filePath);
957957
} else {
958958
throw new Error(`${msg}: Database ${dbName} does not exist`);
959959
}
@@ -964,16 +964,11 @@ export class UtilsSQLite {
964964
}
965965

966966
}
967-
public isDBEncrypted(filePath: string): boolean {
967+
public async isDBEncrypted(filePath: string): Promise<boolean> {
968968
try {
969-
const mDB = this.openOrCreateDatabase(
970-
filePath,
971-
"",
972-
true,
973-
);
974-
this.getVersion(mDB);
975-
this.closeDB(mDB);
976-
return false;
969+
const retStr = await this.fileUtil.readFileAsPromise(filePath, {start:0, end:12});
970+
if (retStr === 'SQLite format') return false;
971+
else return true;
977972
} catch (error) {
978973
return true;
979974
}
@@ -1032,5 +1027,4 @@ export class UtilsSQLite {
10321027
}
10331028
return retStmt;
10341029
}
1035-
10361030
}

electron/src/electron-utils/utilsSecret.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ export class UtilsSecret {
1919
public setEncryptSecret(passphrase: string): void {
2020
try {
2121
let oldpassphrase = this.getPassphrase();
22-
console.log(`>>>> setEncryptSecret oldpassphrase: ${oldpassphrase}`)
2322
if(oldpassphrase.length > 0 ) {
2423
throw new Error(`setEncryptSecret: passphrase already stored`);
2524
} else {
2625
oldpassphrase = this.globalUtil != null ? this.globalUtil.secret : "";
2726
if(oldpassphrase.length <= 0 ) {
2827
throw new Error(`setEncryptSecret: globalUtil is null`);
2928
}
30-
console.log(`>>>> setEncryptSecret oldpassphrase: ${oldpassphrase}, passphrase ${passphrase}`)
3129
// check if some databases were encrypted with the initial secret 'sqlite secret'
3230
this.changeDatabaseSecret(oldpassphrase, passphrase).then(() => {
3331

@@ -82,20 +80,13 @@ export class UtilsSecret {
8280
}
8381
private async changeDatabaseSecret(oldpassphrase: string, newpassphrase: string): Promise<void> {
8482
try {
85-
console.log(`>>> changeDatabaseSecret oldpassphrase: ${oldpassphrase}`);
86-
console.log(`>>> changeDatabaseSecret newpassphrase: ${newpassphrase}`);
8783
// get the database folder
8884
const pathDatabase = this.fileUtil.getDatabasesPath();
89-
console.log(`>>> changeDatabaseSecret pathDatabase: ${pathDatabase}`);
9085
// get the list of databases
9186
const files: string[] = await this.fileUtil.getFileList(pathDatabase);
92-
console.log(`>>> changeDatabaseSecret files: ${files}`);
93-
files.forEach(dbName => {
94-
console.log(`>>> changeDatabaseSecret dbName: ${dbName}`);
87+
files.forEach(async dbName => {
9588
const filePath = this.fileUtil.getFilePath(dbName);
96-
console.log(`>>> changeDatabaseSecret filePath: ${filePath}`);
97-
const isEncrypt: boolean = this.sqliteUtil.isDBEncrypted(filePath);
98-
console.log(`>>> changeDatabaseSecret isEncrypt: ${isEncrypt}`);
89+
const isEncrypt: boolean = await this.sqliteUtil.isDBEncrypted(filePath);
9990
if(isEncrypt) {
10091

10192
this.sqliteUtil.changePassword(filePath, oldpassphrase, newpassphrase);
@@ -108,7 +99,6 @@ export class UtilsSecret {
10899
}
109100
public getPassphrase(): string {
110101
const data = this.storage.getSync('userData');
111-
console.log(`>>>>>> data: `, data)
112102
const keys = Object.keys(data);
113103
if(data == null || keys.length <= 0) return "";
114104
if (Object.keys(data).includes('passphrase')) {
@@ -120,15 +110,13 @@ export class UtilsSecret {
120110
public setPassphrase(passphrase: string): void {
121111
const data = this.storage.getSync('userData');
122112
data.passphrase = passphrase;
123-
console.log(`>>>>>> set data: `, data)
124113
this.storage.set('userData', data,function (error:any) {
125114
if(error) throw new Error(`setPassphrase: ${error.message}`);
126115
});
127116
}
128117
public removePassphrase(): void {
129118
const data = this.storage.getSync('userData');
130119
delete data.passphrase;
131-
console.log(`>>>>>> set data: `, data)
132120
this.storage.set('userData', data,function (error:any) {
133121
if(error) throw new Error(`removePassphrase: ${error.message}`);
134122
});

electron/src/index.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
6262
if (!optionKeys.includes('database')) {
6363
throw new Error('Must provide a database name');
6464
}
65-
6665
const dbName: string = options.database;
6766
const version: number = options.version ? options.version : 1;
6867

@@ -112,13 +111,11 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
112111
const readonly: boolean = options.readonly ? options.readonly : false;
113112
const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
114113
const database = this.getDatabaseConnectionOrThrowError(connName);
115-
console.log(`@@@@@ connName: ${connName}`);
116114

117115
try {
118116
if (database.isDBOpen()) {
119117
// close the database
120118
database.dbClose();
121-
console.log(`@@@@@ after database.dbClose()`);
122119
}
123120
} catch (err) {
124121
throw new Error(
@@ -236,7 +233,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
236233
throw new Error(`Execute: ${msg}`);
237234
}
238235
try {
239-
console.log(`in index.ts statements`)
240236
const executeResult: number = database.executeSQL(
241237
statements,
242238
transaction,
@@ -738,7 +734,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
738734

739735
// get the list of databases
740736
const files: string[] = await this.fileUtil.getFileList(pathDatabase);
741-
742737
if (files.length > 0) {
743738
return { values: files };
744739
} else {
@@ -833,7 +828,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
833828
throw new Error(`setEncryptionSecret: passphrase already in store`);
834829
}
835830
await this.closeAllConnections();
836-
console.log(`setEncryptionSecret after closeAllConnections`);
837831
this.secretUtil.setEncryptSecret(passphrase);
838832
return;
839833
} catch(err) {
@@ -852,8 +846,6 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
852846
if(oldpassphrase.length <= 0) {
853847
throw new Error(`changeEncryptionSecret: You must give the oldpassphrase`);
854848
}
855-
console.log(`##### oldsecret: ${oldsecret}`)
856-
console.log(`##### oldpassphrase: ${oldpassphrase}`)
857849
if(oldpassphrase !== oldsecret) {
858850
throw new Error(`changeEncryptionSecret: the given oldpassphrase is wrong`);
859851
}
@@ -896,7 +888,7 @@ export class CapacitorSQLite implements CapacitorSQLitePlugin {
896888
): Promise<capSQLiteResult> {
897889
const dbName: string = this.getOptionValue(options, 'database');
898890
try {
899-
const isEncrypt: boolean = this.sqliteUtil.isDatabaseEncrypted(dbName + 'SQLite.db');
891+
const isEncrypt: boolean = await this.sqliteUtil.isDatabaseEncrypted(dbName + 'SQLite.db');
900892
return {result: isEncrypt};
901893
} catch(err) {
902894
throw new Error(`isDatabaseEncrypted: ${err}`);

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
"prepublishOnly": "npm run build && npm run build-electron && npm run docgen"
5757
},
5858
"devDependencies": {
59-
"@capacitor/android": "^5.0.0",
60-
"@capacitor/cli": "^5.0.0",
61-
"@capacitor/core": "^5.0.0",
59+
"@capacitor/android": "^5.0.5",
60+
"@capacitor/cli": "^5.0.5",
61+
"@capacitor/core": "^5.0.5",
6262
"@capacitor/docgen": "^0.0.17",
63-
"@capacitor/ios": "^5.0.0",
63+
"@capacitor/ios": "^5.0.5",
6464
"@ionic/eslint-config": "^0.3.0",
6565
"@ionic/prettier-config": "^1.0.1",
6666
"@ionic/swiftlint-config": "^1.1.2",

src/definitions.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1570,7 +1570,10 @@ export class SQLiteConnection implements ISQLiteConnection {
15701570
async getDatabaseList(): Promise<capSQLiteValues> {
15711571
try {
15721572
const res = await this.sqlite.getDatabaseList();
1573-
return Promise.resolve(res);
1573+
const values: string[] = res.values;
1574+
values.sort();
1575+
const ret = {values: values};
1576+
return Promise.resolve(ret);
15741577
} catch (err) {
15751578
return Promise.reject(err);
15761579
}

0 commit comments

Comments
 (0)