feat: add homeassistant integration (#578)

This commit is contained in:
Manuel
2024-06-10 21:16:39 +02:00
committed by GitHub
parent 19498854fc
commit 2e782ae442
28 changed files with 468 additions and 31 deletions

View File

@@ -0,0 +1,22 @@
import crypto from "crypto";
const algorithm = "aes-256-cbc"; //Using AES encryption
const key = Buffer.from("1d71cceced68159ba59a277d056a66173613052cbeeccbfbd15ab1c909455a4d", "hex"); // TODO: generate with const data = crypto.randomBytes(32).toString('hex')
export function encryptSecret(text: string): `${string}.${string}` {
const initializationVector = crypto.randomBytes(16);
const cipher = crypto.createCipheriv(algorithm, Buffer.from(key), initializationVector);
let encrypted = cipher.update(text);
encrypted = Buffer.concat([encrypted, cipher.final()]);
return `${encrypted.toString("hex")}.${initializationVector.toString("hex")}`;
}
export function decryptSecret(value: `${string}.${string}`) {
const [data, dataIv] = value.split(".") as [string, string];
const initializationVector = Buffer.from(dataIv, "hex");
const encryptedText = Buffer.from(data, "hex");
const decipher = crypto.createDecipheriv(algorithm, Buffer.from(key), initializationVector);
let decrypted = decipher.update(encryptedText);
decrypted = Buffer.concat([decrypted, decipher.final()]);
return decrypted.toString();
}

View File

@@ -4,5 +4,7 @@ export * from "./cookie";
export * from "./array";
export * from "./stopwatch";
export * from "./hooks";
export * from "./url";
export * from "./number";
export * from "./error";
export * from "./encryption";

View File

@@ -15,3 +15,7 @@ export const formatNumber = (value: number, decimalPlaces: number) => {
}
return value.toFixed(decimalPlaces);
};
export const randomInt = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1) + min);
};

View File

@@ -0,0 +1,5 @@
export const appendPath = (url: URL | string, path: string) => {
const newUrl = new URL(url);
newUrl.pathname += path;
return newUrl;
};