migration change on default value and encrypted storage
This commit is contained in:
parent
03a5bb3592
commit
a476bf6823
11 changed files with 82 additions and 36 deletions
|
@ -441,6 +441,7 @@ export default abstract class BackupHelper {
|
|||
"user.firstname",
|
||||
"user.lastname",
|
||||
"user.secret",
|
||||
"user.routine",
|
||||
"user.isOwner",
|
||||
])
|
||||
.addSelect(["permissions.permission"])
|
||||
|
|
|
@ -9,12 +9,13 @@ export abstract class CodingHelper {
|
|||
static entityBaseCoding(key: string = "", fallback: string = ""): ValueTransformer {
|
||||
return {
|
||||
from(val: string | null | undefined): string {
|
||||
if (!val) return fallback;
|
||||
if (!val || val == "") return fallback;
|
||||
try {
|
||||
return CodingHelper.decrypt(key, val) || fallback;
|
||||
return CodingHelper.decrypt(key, val, true);
|
||||
} catch (error) {
|
||||
console.error("Decryption error:", error);
|
||||
return fallback;
|
||||
if (fallback == "<self>") return val;
|
||||
else return fallback;
|
||||
}
|
||||
},
|
||||
to(val: string | null | undefined): string {
|
||||
|
@ -22,40 +23,47 @@ export abstract class CodingHelper {
|
|||
if (valueToEncrypt === "") return "";
|
||||
|
||||
try {
|
||||
return CodingHelper.encrypt(key, valueToEncrypt);
|
||||
return CodingHelper.encrypt(key, valueToEncrypt, true);
|
||||
} catch (error) {
|
||||
console.error("Encryption error:", error);
|
||||
if (fallback == "<self>") return val;
|
||||
return "";
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
public static encrypt(phrase: string, content: string): string {
|
||||
public static encrypt(phrase: string, content: string, passError = false): string {
|
||||
if (!content) return "";
|
||||
|
||||
// Generiere zufälligen IV für jede Verschlüsselung (sicherer als statischer IV)
|
||||
const iv = randomBytes(this.ivLength);
|
||||
const key = scryptSync(phrase, "salt", 32);
|
||||
try {
|
||||
// Generiere zufälligen IV für jede Verschlüsselung (sicherer als statischer IV)
|
||||
const iv = randomBytes(this.ivLength);
|
||||
const key = scryptSync(phrase, "salt", 32);
|
||||
|
||||
const cipher = createCipheriv(this.algorithm, Uint8Array.from(key), Uint8Array.from(iv));
|
||||
const cipher = createCipheriv(this.algorithm, Uint8Array.from(key), Uint8Array.from(iv));
|
||||
|
||||
// Verschlüssele den Inhalt
|
||||
let encrypted = cipher.update(content, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
// Verschlüssele den Inhalt
|
||||
let encrypted = cipher.update(content, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
|
||||
// Speichere das Auth-Tag für GCM (wichtig für die Entschlüsselung)
|
||||
const authTag = cipher.getAuthTag();
|
||||
// Speichere das Auth-Tag für GCM (wichtig für die Entschlüsselung)
|
||||
const authTag = cipher.getAuthTag();
|
||||
|
||||
// Gib das Format: iv:verschlüsselter_text:authTag zurück
|
||||
return Buffer.concat([
|
||||
Uint8Array.from(iv),
|
||||
Uint8Array.from(Buffer.from(encrypted, "hex")),
|
||||
Uint8Array.from(authTag),
|
||||
]).toString("base64");
|
||||
// Gib das Format: iv:verschlüsselter_text:authTag zurück
|
||||
return Buffer.concat([
|
||||
Uint8Array.from(iv),
|
||||
Uint8Array.from(Buffer.from(encrypted, "hex")),
|
||||
Uint8Array.from(authTag),
|
||||
]).toString("base64");
|
||||
} catch (error) {
|
||||
if (passError) throw error;
|
||||
console.error("Encryption failed:", error);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public static decrypt(phrase: string, content: string): string {
|
||||
public static decrypt(phrase: string, content: string, passError = false): string {
|
||||
if (!content) return "";
|
||||
|
||||
try {
|
||||
|
@ -79,6 +87,7 @@ export abstract class CodingHelper {
|
|||
|
||||
return decrypted;
|
||||
} catch (error) {
|
||||
if (passError) throw error;
|
||||
console.error("Decryption failed:", error);
|
||||
return "";
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue