Files
web/packages/trpc/lib/utils/encryption.ts
Anton Gunnarsson e572d9e7e9 Merged in feat/sw-2862-move-booking-router-to-trpc-package (pull request #2421)
feat(SW-2861): Move booking router to trpc package

* Use direct imports from trpc package

* Add lint-staged config to trpc

* Move lang enum to common

* Restructure trpc package folder structure

* WIP first step

* update internal imports in trpc

* Fix most errors in scandic-web

Just 100 left...

* Move Props type out of trpc

* Fix CategorizedFilters types

* Move more schemas in hotel router

* Fix deps

* fix getNonContentstackUrls

* Fix import error

* Fix entry error handling

* Fix generateMetadata metrics

* Fix alertType enum

* Fix duplicated types

* lint:fix

* Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package

* Fix broken imports

* Move booking router to trpc package

* Move partners router to trpc package

* Move autocomplete router to trpc package

* Move booking router to trpc package

* Merge branch 'master' into feat/sw-2862-move-booking-router-to-trpc-package


Approved-by: Linus Flood
2025-06-26 13:21:16 +00:00

53 lines
1.5 KiB
TypeScript

import "server-only"
import crypto from "crypto"
import { env } from "../../env/server"
const algorithm = "DES-ECB"
const encryptionKey = env.BOOKING_ENCRYPTION_KEY
const bufferKey = Buffer.from(encryptionKey, "utf8")
export function encrypt(originalString: string) {
try {
const cipher = crypto.createCipheriv(algorithm, bufferKey, null)
cipher.setAutoPadding(false)
const bufferString = Buffer.from(originalString, "utf8")
const paddingSize =
bufferKey.length - (bufferString.length % bufferKey.length)
const paddedStr = Buffer.concat([
bufferString,
Buffer.alloc(paddingSize, 0),
])
const buffers: Buffer[] = []
buffers.push(cipher.update(paddedStr))
buffers.push(cipher.final())
const result = Buffer.concat(buffers).toString("base64")
return result
} catch (e) {
console.log(e)
return ""
}
}
export function decrypt(encryptedString: string) {
try {
const decipher = crypto.createDecipheriv(algorithm, bufferKey, null)
decipher.setAutoPadding(false)
const buffers: Buffer[] = []
buffers.push(decipher.update(encryptedString, "base64"))
buffers.push(decipher.final())
const result = Buffer.concat(buffers)
.toString("utf8")
/*
* Hexadecimal byte (null byte) replace. These occur when decrypting because
* we're disabling the auto padding for historical/compatibility reasons.
*/
.replace(/(\x00)*/g, "")
return result
} catch (e) {
console.log(e)
return ""
}
}