fix: requested changes

This commit is contained in:
Daniel Hougaard
2025-04-04 04:21:00 +04:00
parent c7416c825c
commit 53b5497271
23 changed files with 222 additions and 202 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
import crypto from "crypto";
import { SymmetricKeyEncryptDecrypt, TSymmetricEncryptionFns } from "./types";
import { SymmetricKeyAlgorithm, TSymmetricEncryptionFns } from "./types";
const getIvLength = () => {
return 12;
@@ -11,7 +11,7 @@ const getTagLength = () => {
};
export const symmetricCipherService = (
type: SymmetricKeyEncryptDecrypt.AES_GCM_128 | SymmetricKeyEncryptDecrypt.AES_GCM_256
type: SymmetricKeyAlgorithm.AES_GCM_128 | SymmetricKeyAlgorithm.AES_GCM_256
): TSymmetricEncryptionFns => {
const IV_LENGTH = getIvLength();
const TAG_LENGTH = getTagLength();
+1 -1
View File
@@ -1,2 +1,2 @@
export { symmetricCipherService } from "./cipher";
export { AllowedEncryptionKeyAlgorithms, SymmetricKeyEncryptDecrypt } from "./types";
export { AllowedEncryptionKeyAlgorithms, SymmetricKeyAlgorithm } from "./types";
+5 -7
View File
@@ -1,19 +1,17 @@
import { z } from "zod";
import { AsymmetricKeySignVerify } from "../sign/types";
import { AsymmetricKeyAlgorithm } from "../sign/types";
// Supported symmetric encrypt/decrypt algorithms
export enum SymmetricKeyEncryptDecrypt {
export enum SymmetricKeyAlgorithm {
AES_GCM_256 = "aes-256-gcm",
AES_GCM_128 = "aes-128-gcm"
}
export const SymmetricKeyEncryptDecryptEnum = z.enum(
Object.values(SymmetricKeyEncryptDecrypt) as [string, ...string[]]
).options;
export const SymmetricKeyAlgorithmEnum = z.enum(Object.values(SymmetricKeyAlgorithm) as [string, ...string[]]).options;
export const AllowedEncryptionKeyAlgorithms = z.enum([
...Object.values(SymmetricKeyEncryptDecrypt),
...Object.values(AsymmetricKeySignVerify)
...Object.values(SymmetricKeyAlgorithm),
...Object.values(AsymmetricKeyAlgorithm)
] as [string, ...string[]]).options;
export type TSymmetricEncryptionFns = {
+1 -1
View File
@@ -1,2 +1,2 @@
export { signingService } from "./signing";
export { AsymmetricKeySignVerify, SigningAlgorithm } from "./types";
export { AsymmetricKeyAlgorithm, SigningAlgorithm } from "./types";
+8 -7
View File
@@ -3,7 +3,7 @@ import crypto from "crypto";
import { BadRequestError } from "@app/lib/errors";
import { logger } from "@app/lib/logger";
import { AsymmetricKeySignVerify, SigningAlgorithm, TAsymmetricSignVerifyFns } from "./types";
import { AsymmetricKeyAlgorithm, SigningAlgorithm, TAsymmetricSignVerifyFns } from "./types";
// Map of signing algorithms to their parameters
interface SigningParams {
@@ -22,7 +22,7 @@ const SHA512_DIGEST_LENGTH = 64;
* @param algorithm The signing algorithm to use
* @returns Object with sign and verify functions
*/
export const signingService = (algorithm: AsymmetricKeySignVerify): TAsymmetricSignVerifyFns => {
export const signingService = (algorithm: AsymmetricKeyAlgorithm): TAsymmetricSignVerifyFns => {
const $getSigningParams = (signingAlgorithm: SigningAlgorithm): SigningParams => {
switch (signingAlgorithm) {
// RSA PSS
@@ -76,10 +76,10 @@ export const signingService = (algorithm: AsymmetricKeySignVerify): TAsymmetricS
};
// For ECC key generation, nodejs has some strange and hardly documented curve naming conventions
const $getEcCurveName = (keyAlgorithm: AsymmetricKeySignVerify): string => {
const $getEcCurveName = (keyAlgorithm: AsymmetricKeyAlgorithm): string => {
// We will support more in the future
switch (keyAlgorithm) {
case AsymmetricKeySignVerify.ECC_NIST_P256:
case AsymmetricKeyAlgorithm.ECC_NIST_P256:
return "prime256v1";
default:
throw new Error(`Unsupported EC curve: ${keyAlgorithm}`);
@@ -172,7 +172,6 @@ export const signingService = (algorithm: AsymmetricKeySignVerify): TAsymmetricS
type: "pkcs8"
});
// Return public key in PEM format for both RSA and EC
const publicKey = crypto.createPublicKey(privateKeyObj).export({
type: "spki",
format: "pem"
@@ -210,7 +209,8 @@ export const signingService = (algorithm: AsymmetricKeySignVerify): TAsymmetricS
}
// For PKCS1 v1.5 padding
return signer.sign({
key: privateKeyObject
key: privateKeyObject,
padding
});
}
if (signingAlgorithm.startsWith("ECDSA")) {
@@ -252,7 +252,8 @@ export const signingService = (algorithm: AsymmetricKeySignVerify): TAsymmetricS
// For PKCS1 v1.5 padding
return verifier.verify(
{
key: publicKey.toString()
key: publicKey.toString(),
padding
},
signature
);
+3 -3
View File
@@ -8,13 +8,13 @@ export type TAsymmetricSignVerifyFns = {
};
// Supported asymmetric key types
export enum AsymmetricKeySignVerify {
export enum AsymmetricKeyAlgorithm {
RSA_4096 = "rsa-4096",
ECC_NIST_P256 = "ecc-nist-p256"
}
export const AsymmetricKeySignVerifyEnum = z.enum(
Object.values(AsymmetricKeySignVerify) as [string, ...string[]]
export const AsymmetricKeyAlgorithmEnum = z.enum(
Object.values(AsymmetricKeyAlgorithm) as [string, ...string[]]
).options;
export enum SigningAlgorithm {