reformat with prettier with semicolons and tabs
This commit is contained in:
@ -1,37 +1,37 @@
|
||||
import crypto from "crypto"
|
||||
import { getConfig } from "blitz"
|
||||
import crypto from "crypto";
|
||||
import { getConfig } from "blitz";
|
||||
|
||||
const { serverRuntimeConfig } = getConfig()
|
||||
const { serverRuntimeConfig } = getConfig();
|
||||
|
||||
const IV_LENGTH = 16
|
||||
const ALGORITHM = "aes-256-cbc"
|
||||
const IV_LENGTH = 16;
|
||||
const ALGORITHM = "aes-256-cbc";
|
||||
|
||||
export function encrypt(text: string, encryptionKey: Buffer | string) {
|
||||
const encryptionKeyAsBuffer = Buffer.isBuffer(encryptionKey)
|
||||
? encryptionKey
|
||||
: Buffer.from(encryptionKey, "hex")
|
||||
const iv = crypto.randomBytes(IV_LENGTH)
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, encryptionKeyAsBuffer, iv)
|
||||
const encrypted = cipher.update(text)
|
||||
const encryptedBuffer = Buffer.concat([encrypted, cipher.final()])
|
||||
: Buffer.from(encryptionKey, "hex");
|
||||
const iv = crypto.randomBytes(IV_LENGTH);
|
||||
const cipher = crypto.createCipheriv(ALGORITHM, encryptionKeyAsBuffer, iv);
|
||||
const encrypted = cipher.update(text);
|
||||
const encryptedBuffer = Buffer.concat([encrypted, cipher.final()]);
|
||||
|
||||
return `${iv.toString("hex")}:${encryptedBuffer.toString("hex")}`
|
||||
return `${iv.toString("hex")}:${encryptedBuffer.toString("hex")}`;
|
||||
}
|
||||
|
||||
export function decrypt(encryptedHexText: string, encryptionKey: Buffer | string) {
|
||||
const encryptionKeyAsBuffer = Buffer.isBuffer(encryptionKey)
|
||||
? encryptionKey
|
||||
: Buffer.from(encryptionKey, "hex")
|
||||
const [hexIv, hexText] = encryptedHexText.split(":")
|
||||
const iv = Buffer.from(hexIv!, "hex")
|
||||
const encryptedText = Buffer.from(hexText!, "hex")
|
||||
const decipher = crypto.createDecipheriv(ALGORITHM, encryptionKeyAsBuffer, iv)
|
||||
const decrypted = decipher.update(encryptedText)
|
||||
const decryptedBuffer = Buffer.concat([decrypted, decipher.final()])
|
||||
: Buffer.from(encryptionKey, "hex");
|
||||
const [hexIv, hexText] = encryptedHexText.split(":");
|
||||
const iv = Buffer.from(hexIv!, "hex");
|
||||
const encryptedText = Buffer.from(hexText!, "hex");
|
||||
const decipher = crypto.createDecipheriv(ALGORITHM, encryptionKeyAsBuffer, iv);
|
||||
const decrypted = decipher.update(encryptedText);
|
||||
const decryptedBuffer = Buffer.concat([decrypted, decipher.final()]);
|
||||
|
||||
return decryptedBuffer.toString()
|
||||
return decryptedBuffer.toString();
|
||||
}
|
||||
|
||||
export function computeEncryptionKey(userIdentifier: string) {
|
||||
return crypto.scryptSync(userIdentifier, serverRuntimeConfig.masterEncryptionKey, 32)
|
||||
return crypto.scryptSync(userIdentifier, serverRuntimeConfig.masterEncryptionKey, 32);
|
||||
}
|
||||
|
10
db/index.ts
10
db/index.ts
@ -1,7 +1,7 @@
|
||||
import { enhancePrisma } from "blitz"
|
||||
import { PrismaClient } from "@prisma/client"
|
||||
import { enhancePrisma } from "blitz";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const EnhancedPrisma = enhancePrisma(PrismaClient)
|
||||
const EnhancedPrisma = enhancePrisma(PrismaClient);
|
||||
|
||||
export * from "@prisma/client"
|
||||
export default new EnhancedPrisma()
|
||||
export * from "@prisma/client";
|
||||
export default new EnhancedPrisma();
|
||||
|
@ -11,6 +11,6 @@ const seed = async () => {
|
||||
// for (let i = 0; i < 5; i++) {
|
||||
// await db.project.create({ data: { name: "Project " + i } })
|
||||
// }
|
||||
}
|
||||
};
|
||||
|
||||
export default seed
|
||||
export default seed;
|
||||
|
Reference in New Issue
Block a user