@privateaim/kit 0.8.4 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/crypto/asymmetric/constants.d.ts +5 -0
  3. package/dist/crypto/asymmetric/constants.d.ts.map +1 -0
  4. package/dist/crypto/asymmetric/helpers.d.ts +5 -0
  5. package/dist/crypto/asymmetric/helpers.d.ts.map +1 -0
  6. package/dist/crypto/asymmetric/index.d.ts +5 -0
  7. package/dist/crypto/asymmetric/index.d.ts.map +1 -0
  8. package/dist/crypto/asymmetric/module.d.ts +14 -0
  9. package/dist/crypto/asymmetric/module.d.ts.map +1 -0
  10. package/dist/crypto/asymmetric/types.d.ts +3 -0
  11. package/dist/crypto/asymmetric/types.d.ts.map +1 -0
  12. package/dist/crypto/index.d.ts +3 -0
  13. package/dist/crypto/index.d.ts.map +1 -0
  14. package/dist/crypto/symmetric/index.d.ts +3 -0
  15. package/dist/crypto/symmetric/index.d.ts.map +1 -0
  16. package/dist/crypto/symmetric/module.d.ts +10 -0
  17. package/dist/crypto/symmetric/module.d.ts.map +1 -0
  18. package/dist/crypto/symmetric/types.d.ts +3 -0
  19. package/dist/crypto/symmetric/types.d.ts.map +1 -0
  20. package/dist/index.cjs +426 -0
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.ts +1 -0
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.mjs +421 -1
  25. package/dist/index.mjs.map +1 -1
  26. package/package.json +4 -4
  27. package/src/crypto/asymmetric/constants.ts +12 -0
  28. package/src/crypto/asymmetric/helpers.ts +58 -0
  29. package/src/crypto/asymmetric/index.ts +11 -0
  30. package/src/crypto/asymmetric/module.ts +190 -0
  31. package/src/crypto/asymmetric/types.ts +9 -0
  32. package/src/crypto/index.ts +9 -0
  33. package/src/crypto/symmetric/index.ts +9 -0
  34. package/src/crypto/symmetric/module.ts +71 -0
  35. package/src/crypto/symmetric/types.ts +10 -0
  36. package/src/index.ts +1 -0
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/domains/permission/constants.ts","../src/utils/boolean.ts","../src/utils/has-own-property.ts","../src/utils/error.ts","../src/utils/hex-checker.ts","../src/utils/hostname.ts","../src/utils/nanoid.ts","../src/utils/object-properties.ts","../src/utils/proxy-connection-string.ts","../src/utils/regex-patterns.ts","../src/constants.ts"],"sourcesContent":["/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum PermissionName {\n BUCKET_CREATE = 'bucket_create',\n BUCKET_UPDATE = 'bucket_update',\n BUCKET_DELETE = 'bucket_delete',\n\n PROJECT_CREATE = 'project_create',\n PROJECT_DELETE = 'project_delete',\n PROJECT_UPDATE = 'project_update',\n PROJECT_APPROVE = 'project_approve',\n\n REGISTRY_MANAGE = 'registry_manage',\n REGISTRY_PROJECT_MANAGE = 'registry_project_manage',\n\n NODE_CREATE = 'node_create',\n NODE_DELETE = 'node_delete',\n NODE_UPDATE = 'node_update',\n\n ANALYSIS_APPROVE = 'analysis_approve',\n ANALYSIS_UPDATE = 'analysis_update',\n ANALYSIS_CREATE = 'analysis_create',\n ANALYSIS_EXECUTION_START = 'analysis_execution_start',\n ANALYSIS_EXECUTION_STOP = 'analysis_execution_stop',\n ANALYSIS_DELETE = 'analysis_delete',\n ANALYSIS_RESULT_READ = 'analysis_result_read', // todo: this is maybe not required anymore\n\n MASTER_IMAGE_MANAGE = 'master_image_manage',\n MASTER_IMAGE_GROUP_MANAGE = 'master_image_group_manage',\n\n SERVICE_MANAGE = 'service_manage',\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isBoolTrue<T = any>(input: T | boolean) : input is true {\n return typeof input === 'boolean' && !!input;\n}\n\nexport function isBoolFalse<T = any>(input: T | boolean) : input is false {\n return typeof input === 'boolean' && !input;\n}\n\nexport function isBool<T = any>(input: T | boolean) : input is boolean {\n return typeof input === 'boolean';\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n\nexport function isObject(item: unknown) : item is Record<string, any> {\n return (\n !!item &&\n typeof item === 'object' &&\n !Array.isArray(item)\n );\n}\n","/*\n * Copyright (c) 2023-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { hasOwnProperty } from './has-own-property';\n\nexport function isError(e: unknown) {\n return typeof e === 'object' && e && hasOwnProperty(e, 'message');\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isHex(value: string) : boolean {\n return /^[A-Fa-f0-9]+$/i.test(value);\n}\n\nexport function hexToUTF8(value: string) {\n try {\n return decodeURIComponent(`%${value.match(/.{1,2}/g).join('%')}`);\n } catch (e) {\n if (e instanceof URIError) {\n return value;\n }\n\n throw e;\n }\n}\n","/*\n * Copyright (c) 2022-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function getHostNameFromString(value: string) : string {\n if (\n value.startsWith('http://') ||\n value.startsWith('https://')\n ) {\n const url = new URL(value);\n value = url.hostname;\n }\n\n return value;\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { customAlphabet } from 'nanoid';\n\nexport function createNanoID(alphabet?: string) : string;\nexport function createNanoID(len?: number) : string;\nexport function createNanoID(alphabet?: string, len?: number) : string;\nexport function createNanoID(alphabetOrLen?: string | number, len?: number) : string {\n if (typeof alphabetOrLen === 'string') {\n return customAlphabet(alphabetOrLen, len || 21)();\n }\n\n if (typeof alphabetOrLen === 'number') {\n return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', alphabetOrLen)();\n }\n\n return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', len || 21)();\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function nullifyEmptyObjectProperties<T extends Record<string, any>>(data: T) : T {\n const keys : (keyof T)[] = Object.keys(data);\n\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n if (data[key] === '') {\n data[key] = null as T[keyof T];\n }\n }\n\n return data as T;\n}\n\nexport function deleteUndefinedObjectProperties<T extends Record<string, any>>(data: T) : T {\n const keys : string[] = Object.keys(data);\n\n for (let i = 0; i < keys.length; i++) {\n if (typeof data[keys[i]] === 'undefined') {\n delete data[keys[i]];\n }\n }\n\n return data;\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport type ProxyConnectionConfig = {\n protocol: 'http' | 'https',\n host: string,\n port: number,\n auth: {\n username: string,\n password: string,\n }\n};\n\nexport function parseProxyConnectionString(connectionStr: string) : ProxyConnectionConfig | undefined {\n const match = connectionStr\n .match(/(?:(https|http):\\/\\/)(?:(\\w+)(?::(\\w+))?@)?(?:([^:]+))(?::(\\d{1,5}))?$/);\n\n if (!match) {\n return undefined;\n }\n\n return {\n protocol: match[1] as 'http' | 'https',\n host: match[4],\n port: parseInt(match[5], 10),\n auth: {\n username: match[2],\n password: match[3],\n },\n };\n}\n\nexport function detectProxyConnectionConfig() : ProxyConnectionConfig | undefined {\n const envKeys = [\n 'https_proxy',\n 'HTTPS_PROXY',\n 'http_proxy',\n 'HTTP_PROXY',\n ];\n\n let result : string | undefined;\n\n for (let i = 0; i < envKeys.length; i++) {\n const envKey = envKeys[i];\n const envVal = process.env[envKey];\n\n if (\n envVal !== undefined &&\n envVal !== null\n ) {\n result = result || envVal;\n }\n }\n\n if (!result) {\n return undefined;\n }\n\n return parseProxyConnectionString(result);\n}\n","/*\n * Copyright (c) 2023-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport const alphaNumHyphenUnderscoreRegex = /^[a-z0-9-_]*$/;\nexport const registryRobotSecretRegex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d).{8,}$/;\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum EnvironmentName {\n TEST = 'test',\n DEVELOPMENT = 'development',\n PRODUCTION = 'production',\n}\n"],"names":["PermissionName","isBoolTrue","input","isBoolFalse","isBool","hasOwnProperty","obj","prop","Object","prototype","call","isObject","item","Array","isArray","isError","e","isHex","value","test","hexToUTF8","decodeURIComponent","match","join","URIError","getHostNameFromString","startsWith","url","URL","hostname","createNanoID","alphabetOrLen","len","customAlphabet","nullifyEmptyObjectProperties","data","keys","i","length","key","deleteUndefinedObjectProperties","parseProxyConnectionString","connectionStr","undefined","protocol","host","port","parseInt","auth","username","password","detectProxyConnectionConfig","envKeys","result","envKey","envVal","process","env","alphaNumHyphenUnderscoreRegex","registryRobotSecretRegex","EnvironmentName"],"mappings":";;AAAA;;;;;IAOO,IAAKA,cAAAA,iBAAAA,SAAAA,cAAAA,EAAAA;;;;;;;;;;;;;;;;;;;;;;;AAAAA,IAAAA,OAAAA,cAAAA,CAAAA;AA6BX,CAAA,CAAA,EAAA;;ACpCD;;;;;IAOO,SAASC,UAAAA,CAAoBC,KAAkB,EAAA;AAClD,IAAA,OAAO,OAAOA,KAAAA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A,OAAAA,CAAQC,GAAG,CAACH,MAAO,CAAA,CAAA;QAElC,IACIC,MAAAA,KAAWZ,SACXY,IAAAA,MAAAA,KAAW,IACb,EAAA;AACEF,YAAAA,MAAAA,GAASA,MAAUE,IAAAA,MAAAA,CAAAA;AACvB,SAAA;AACJ,KAAA;AAEA,IAAA,IAAI,CAACF,MAAQ,EAAA;QACT,OAAOV,SAAAA,CAAAA;AACX,KAAA;AAEA,IAAA,OAAOF,0BAA2BY,CAAAA,MAAAA,CAAAA,CAAAA;AACtC;;AC/DA;;;;;IAOaK,MAAAA,6BAAAA,GAAgC,gBAAgB;AACtD,MAAMC,2BAA2B;;ACRxC;;;;;IAOO,IAAKC,eAAAA,iBAAAA,SAAAA,eAAAA,EAAAA;;;;AAAAA,IAAAA,OAAAA,eAAAA,CAAAA;AAIX,CAAA,CAAA,EAAA;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/crypto/asymmetric/constants.ts","../src/crypto/asymmetric/helpers.ts","../src/crypto/asymmetric/module.ts","../src/crypto/symmetric/module.ts","../src/domains/permission/constants.ts","../src/utils/boolean.ts","../src/utils/has-own-property.ts","../src/utils/error.ts","../src/utils/hex-checker.ts","../src/utils/hostname.ts","../src/utils/nanoid.ts","../src/utils/object-properties.ts","../src/utils/proxy-connection-string.ts","../src/utils/regex-patterns.ts","../src/constants.ts"],"sourcesContent":["/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum AsymmetricCryptoAlgorithmName {\n RSA_OAEP = 'RSA-OAEP',\n\n ECDH = 'ECDH',\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { AsymmetricAlgorithmImportParams } from './types';\n\nfunction arrayBufferToBase64(arrayBuffer: ArrayBuffer): string {\n return btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));\n}\n\nexport async function exportAsymmetricPublicKey(key: CryptoKey): Promise<string> {\n const exported = await crypto.subtle.exportKey(\n 'spki',\n key,\n );\n\n return `-----BEGIN PUBLIC KEY-----\\n${arrayBufferToBase64(exported)}\\n-----END PUBLIC KEY-----`;\n}\n\nexport async function exportAsymmetricPrivateKey(key: CryptoKey): Promise<string> {\n const exported = await crypto.subtle.exportKey(\n 'pkcs8',\n key,\n );\n\n return `-----BEGIN PRIVATE KEY-----\\n${arrayBufferToBase64(exported)}\\n-----END PRIVATE KEY-----`;\n}\n\nexport async function importAsymmetricPublicKey(\n pem: string,\n params: AsymmetricAlgorithmImportParams,\n): Promise<CryptoKey> {\n const pemHeader = '-----BEGIN PUBLIC KEY-----';\n const pemFooter = '-----END PUBLIC KEY-----';\n const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);\n const buffer = Buffer.from(pemContents, 'base64');\n\n if (params.name === 'ECDH') {\n return crypto.subtle.importKey(\n 'spki',\n buffer,\n params,\n true,\n ['deriveKey'],\n );\n }\n\n return crypto.subtle.importKey(\n 'spki',\n buffer,\n params,\n true,\n ['encrypt'],\n );\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { AsymmetricCryptoAlgorithmName } from './constants';\nimport type { AsymmetricAlgorithmImportParams, AsymmetricAlgorithmParams } from './types';\nimport { exportAsymmetricPrivateKey, exportAsymmetricPublicKey } from './helpers';\n\nexport class CryptoAsymmetricAlgorithm {\n public readonly algorithm: AsymmetricAlgorithmParams;\n\n protected keyPair : CryptoKeyPair | undefined;\n\n constructor(algorithm: AsymmetricAlgorithmParams) {\n if (algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {\n algorithm = {\n ...algorithm,\n publicExponent: new Uint8Array([1, 0, 1]),\n };\n }\n\n this.algorithm = algorithm;\n }\n\n buildImportParams() : AsymmetricAlgorithmImportParams {\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {\n return {\n name: 'RSA-OAEP',\n hash: 'SHA-256',\n };\n }\n\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {\n return {\n name: 'ECDH',\n namedCurve: (this.algorithm as EcKeyGenParams).namedCurve,\n hash: 'SHA-256',\n };\n }\n\n throw new Error('Import params could not be created.');\n }\n\n async generateKeyPair() : Promise<CryptoKeyPair> {\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {\n this.keyPair = await crypto.subtle.generateKey(\n this.algorithm,\n true,\n ['encrypt', 'decrypt'],\n );\n\n return this.keyPair;\n }\n\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {\n this.keyPair = await crypto.subtle.generateKey(\n this.algorithm,\n true,\n ['deriveKey'],\n );\n\n return this.keyPair;\n }\n\n throw new Error('The algorithm is not supported for key generation.');\n }\n\n async useKeyPair(): Promise<CryptoKeyPair> {\n if (typeof this.keyPair !== 'undefined') {\n return this.keyPair;\n }\n\n return this.generateKeyPair();\n }\n\n async exportPublicKey(): Promise<string> {\n const keyPair = await this.useKeyPair();\n\n return exportAsymmetricPublicKey(keyPair.publicKey);\n }\n\n async exportPrivateKey(): Promise<string> {\n const keyPair = await this.useKeyPair();\n\n return exportAsymmetricPrivateKey(keyPair.privateKey);\n }\n\n async encrypt(data: Buffer, remoteKey?: CryptoKey) {\n const keyPair = await this.useKeyPair();\n\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {\n return crypto.subtle.encrypt(\n {\n name: 'RSA-OAEP',\n },\n remoteKey || keyPair.publicKey,\n data,\n );\n }\n\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {\n if (typeof remoteKey === 'undefined') {\n throw new Error('Remote public key is required.');\n }\n\n const array = new Uint8Array(16);\n const iv = crypto.getRandomValues(array);\n const key = await crypto.subtle.deriveKey(\n {\n name: 'ECDH',\n public: remoteKey,\n },\n keyPair.privateKey,\n {\n name: 'AES-GCM',\n length: 256,\n },\n true,\n ['encrypt'],\n );\n\n const arrayBuffer = await crypto.subtle.encrypt(\n {\n name: 'AES-GCM',\n length: 256,\n iv,\n },\n key,\n data,\n );\n\n const buffer = Buffer.from(arrayBuffer);\n\n return Buffer.concat([iv, buffer]);\n }\n\n throw new Error('Unsupported algorithm for encryption.');\n }\n\n async decrypt(data: Buffer, remoteKey?: CryptoKey) {\n const keyPair = await this.useKeyPair();\n\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {\n return crypto.subtle.decrypt(\n {\n name: 'RSA-OAEP',\n },\n keyPair.privateKey,\n data,\n );\n }\n\n if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {\n if (typeof remoteKey === 'undefined') {\n throw new Error('Remote public key is required.');\n }\n\n const iv = data.slice(0, 16);\n\n const key = await crypto.subtle.deriveKey(\n {\n name: 'ECDH',\n public: remoteKey,\n },\n keyPair.privateKey,\n {\n name: 'AES-GCM',\n length: 256,\n },\n true,\n ['decrypt'],\n );\n\n return crypto.subtle.decrypt(\n {\n name: 'AES-GCM',\n length: 256,\n iv,\n },\n key,\n data.slice(16),\n );\n }\n\n throw new Error('Unsupported algorithm for decryption.');\n }\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport type { SymmetricAlgorithmParams } from './types';\n\nexport class CryptoSymmetricAlgorithm {\n protected algorithm : SymmetricAlgorithmParams;\n\n constructor(algorithm: SymmetricAlgorithmParams) {\n this.algorithm = algorithm;\n }\n\n async generateKey() : Promise<CryptoKey> {\n return crypto.subtle.generateKey(\n {\n name: this.algorithm.name,\n length: 256,\n },\n true,\n ['encrypt', 'decrypt'],\n );\n }\n\n async importKey(buffer: Buffer | ArrayBuffer) : Promise<CryptoKey> {\n return crypto.subtle.importKey(\n 'raw',\n buffer,\n {\n name: this.algorithm.name,\n length: 256,\n },\n true,\n ['encrypt', 'decrypt'],\n );\n }\n\n async encrypt(key: CryptoKey, iv: Buffer, data: Buffer) : Promise<Buffer> {\n const arrayBuffer = await crypto.subtle.encrypt(\n {\n name: this.algorithm.name,\n length: 256,\n iv,\n },\n key,\n data,\n );\n\n const buffer = Buffer.from(arrayBuffer);\n\n return Buffer.concat([iv, buffer]);\n }\n\n async decrypt(key: CryptoKey, data: Buffer) : Promise<Buffer> {\n const iv = data.slice(0, 16);\n const arrayBuffer = await crypto.subtle.decrypt(\n {\n name: this.algorithm.name,\n length: 256,\n iv,\n },\n key,\n data.slice(16),\n );\n\n return Buffer.from(arrayBuffer);\n }\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum PermissionName {\n BUCKET_CREATE = 'bucket_create',\n BUCKET_UPDATE = 'bucket_update',\n BUCKET_DELETE = 'bucket_delete',\n\n PROJECT_CREATE = 'project_create',\n PROJECT_DELETE = 'project_delete',\n PROJECT_UPDATE = 'project_update',\n PROJECT_APPROVE = 'project_approve',\n\n REGISTRY_MANAGE = 'registry_manage',\n REGISTRY_PROJECT_MANAGE = 'registry_project_manage',\n\n NODE_CREATE = 'node_create',\n NODE_DELETE = 'node_delete',\n NODE_UPDATE = 'node_update',\n\n ANALYSIS_APPROVE = 'analysis_approve',\n ANALYSIS_UPDATE = 'analysis_update',\n ANALYSIS_CREATE = 'analysis_create',\n ANALYSIS_EXECUTION_START = 'analysis_execution_start',\n ANALYSIS_EXECUTION_STOP = 'analysis_execution_stop',\n ANALYSIS_DELETE = 'analysis_delete',\n ANALYSIS_RESULT_READ = 'analysis_result_read', // todo: this is maybe not required anymore\n\n MASTER_IMAGE_MANAGE = 'master_image_manage',\n MASTER_IMAGE_GROUP_MANAGE = 'master_image_group_manage',\n\n SERVICE_MANAGE = 'service_manage',\n}\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isBoolTrue<T = any>(input: T | boolean) : input is true {\n return typeof input === 'boolean' && !!input;\n}\n\nexport function isBoolFalse<T = any>(input: T | boolean) : input is false {\n return typeof input === 'boolean' && !input;\n}\n\nexport function isBool<T = any>(input: T | boolean) : input is boolean {\n return typeof input === 'boolean';\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport function hasOwnProperty<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {\n return Object.prototype.hasOwnProperty.call(obj, prop);\n}\n\nexport function isObject(item: unknown) : item is Record<string, any> {\n return (\n !!item &&\n typeof item === 'object' &&\n !Array.isArray(item)\n );\n}\n","/*\n * Copyright (c) 2023-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { hasOwnProperty } from './has-own-property';\n\nexport function isError(e: unknown) {\n return typeof e === 'object' && e && hasOwnProperty(e, 'message');\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function isHex(value: string) : boolean {\n return /^[A-Fa-f0-9]+$/i.test(value);\n}\n\nexport function hexToUTF8(value: string) {\n try {\n return decodeURIComponent(`%${value.match(/.{1,2}/g).join('%')}`);\n } catch (e) {\n if (e instanceof URIError) {\n return value;\n }\n\n throw e;\n }\n}\n","/*\n * Copyright (c) 2022-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function getHostNameFromString(value: string) : string {\n if (\n value.startsWith('http://') ||\n value.startsWith('https://')\n ) {\n const url = new URL(value);\n value = url.hostname;\n }\n\n return value;\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nimport { customAlphabet } from 'nanoid';\n\nexport function createNanoID(alphabet?: string) : string;\nexport function createNanoID(len?: number) : string;\nexport function createNanoID(alphabet?: string, len?: number) : string;\nexport function createNanoID(alphabetOrLen?: string | number, len?: number) : string {\n if (typeof alphabetOrLen === 'string') {\n return customAlphabet(alphabetOrLen, len || 21)();\n }\n\n if (typeof alphabetOrLen === 'number') {\n return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', alphabetOrLen)();\n }\n\n return customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', len || 21)();\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport function nullifyEmptyObjectProperties<T extends Record<string, any>>(data: T) : T {\n const keys : (keyof T)[] = Object.keys(data);\n\n for (let i = 0; i < keys.length; i++) {\n const key = keys[i];\n if (data[key] === '') {\n data[key] = null as T[keyof T];\n }\n }\n\n return data as T;\n}\n\nexport function deleteUndefinedObjectProperties<T extends Record<string, any>>(data: T) : T {\n const keys : string[] = Object.keys(data);\n\n for (let i = 0; i < keys.length; i++) {\n if (typeof data[keys[i]] === 'undefined') {\n delete data[keys[i]];\n }\n }\n\n return data;\n}\n","/*\n * Copyright (c) 2021-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport type ProxyConnectionConfig = {\n protocol: 'http' | 'https',\n host: string,\n port: number,\n auth: {\n username: string,\n password: string,\n }\n};\n\nexport function parseProxyConnectionString(connectionStr: string) : ProxyConnectionConfig | undefined {\n const match = connectionStr\n .match(/(?:(https|http):\\/\\/)(?:(\\w+)(?::(\\w+))?@)?(?:([^:]+))(?::(\\d{1,5}))?$/);\n\n if (!match) {\n return undefined;\n }\n\n return {\n protocol: match[1] as 'http' | 'https',\n host: match[4],\n port: parseInt(match[5], 10),\n auth: {\n username: match[2],\n password: match[3],\n },\n };\n}\n\nexport function detectProxyConnectionConfig() : ProxyConnectionConfig | undefined {\n const envKeys = [\n 'https_proxy',\n 'HTTPS_PROXY',\n 'http_proxy',\n 'HTTP_PROXY',\n ];\n\n let result : string | undefined;\n\n for (let i = 0; i < envKeys.length; i++) {\n const envKey = envKeys[i];\n const envVal = process.env[envKey];\n\n if (\n envVal !== undefined &&\n envVal !== null\n ) {\n result = result || envVal;\n }\n }\n\n if (!result) {\n return undefined;\n }\n\n return parseProxyConnectionString(result);\n}\n","/*\n * Copyright (c) 2023-2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport const alphaNumHyphenUnderscoreRegex = /^[a-z0-9-_]*$/;\nexport const registryRobotSecretRegex = /^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d).{8,}$/;\n","/*\n * Copyright (c) 2024.\n * Author Peter Placzek (tada5hi)\n * For the full copyright and license information,\n * view the LICENSE file that was distributed with this source code.\n */\n\nexport enum EnvironmentName {\n TEST = 'test',\n DEVELOPMENT = 'development',\n PRODUCTION = 'production',\n}\n"],"names":["AsymmetricCryptoAlgorithmName","asyncGeneratorStep","arrayBufferToBase64","arrayBuffer","btoa","String","fromCharCode","apply","Uint8Array","exportAsymmetricPublicKey","key","_async_to_generator","exported","crypto","subtle","exportKey","exportAsymmetricPrivateKey","importAsymmetricPublicKey","pem","params","pemHeader","pemFooter","pemContents","substring","length","buffer","Buffer","from","name","importKey","CryptoAsymmetricAlgorithm","buildImportParams","algorithm","RSA_OAEP","hash","ECDH","namedCurve","Error","generateKeyPair","keyPair","generateKey","useKeyPair","exportPublicKey","publicKey","exportPrivateKey","privateKey","encrypt","data","remoteKey","array","iv","getRandomValues","deriveKey","public","concat","decrypt","slice","constructor","_define_property","publicExponent","CryptoSymmetricAlgorithm","PermissionName","isBoolTrue","input","isBoolFalse","isBool","hasOwnProperty","obj","prop","Object","prototype","call","isObject","item","Array","isArray","isError","e","isHex","value","test","hexToUTF8","decodeURIComponent","match","join","URIError","getHostNameFromString","startsWith","url","URL","hostname","createNanoID","alphabetOrLen","len","customAlphabet","nullifyEmptyObjectProperties","keys","i","deleteUndefinedObjectProperties","parseProxyConnectionString","connectionStr","undefined","protocol","host","port","parseInt","auth","username","password","detectProxyConnectionConfig","envKeys","result","envKey","envVal","process","env","alphaNumHyphenUnderscoreRegex","registryRobotSecretRegex","EnvironmentName"],"mappings":";;AAAA;;;;;IAOO,IAAKA,6BAAAA,iBAAAA,SAAAA,6BAAAA,EAAAA;;;AAAAA,IAAAA,OAAAA,6BAAAA;AAIX,CAAA,CAAA,EAAA;;ACXD;;;;;AAKC,IAAA,SAAAC,oBAAA,CAAA,GAAA,EAAA,OAAA,EAAA,MAAA,EAAA,KAAA,EAAA,MAAA,EAAA,GAAA,EAAA,GAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAID,SAASC,oBAAoBC,WAAwB,EAAA;IACjD,OAAOC,IAAAA,CAAKC,OAAOC,YAAY,CAACC,KAAK,CAAC,IAAA,EAAM,IAAIC,UAAWL,CAAAA,WAAAA,CAAAA,CAAAA,CAAAA;AAC/D;AAEA,SAAsBM,0BAA0BC,GAAc,EAAA;AAAxCD,IAAAA,OAAAA,0BAAAA,CAAAA,KAAAA,CAAAA,IAAAA,EAAAA,SAAAA,CAAAA;;AAAAA,SAAAA,0BAAAA,GAAAA;IAAAA,0BAAf,GAAAE,qBAAA,CAAA,UAAyCD,GAAc,EAAA;AAC1D,QAAA,MAAME,WAAW,MAAMC,MAAAA,CAAOC,MAAM,CAACC,SAAS,CAC1C,MACAL,EAAAA,GAAAA,CAAAA;AAGJ,QAAA,OAAO,CAAC,4BAA4B,EAAER,mBAAoBU,CAAAA,QAAAA,CAAAA,CAAU,0BAA0B,CAAC;AACnG,KAAA,CAAA;AAPsBH,IAAAA,OAAAA,0BAAAA,CAAAA,KAAAA,CAAAA,I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AA;QAElC,IACIC,MAAAA,KAAWZ,SACXY,IAAAA,MAAAA,KAAW,IACb,EAAA;AACEF,YAAAA,MAAAA,GAASA,MAAUE,IAAAA,MAAAA;AACvB;AACJ;AAEA,IAAA,IAAI,CAACF,MAAQ,EAAA;QACT,OAAOV,SAAAA;AACX;AAEA,IAAA,OAAOF,0BAA2BY,CAAAA,MAAAA,CAAAA;AACtC;;AC/DA;;;;;IAOaK,MAAAA,6BAAAA,GAAgC;AACtC,MAAMC,2BAA2B;;ACRxC;;;;;IAOO,IAAKC,eAAAA,iBAAAA,SAAAA,eAAAA,EAAAA;;;;AAAAA,IAAAA,OAAAA,eAAAA;AAIX,CAAA,CAAA,EAAA;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@privateaim/kit",
3
- "version": "0.8.4",
3
+ "version": "0.8.6",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "exports": {
@@ -26,13 +26,13 @@
26
26
  "build-watch": "rimraf ./dist && tsc -p tsconfig.build.json --watch"
27
27
  },
28
28
  "devDependencies": {
29
- "@authup/core-kit": "^1.0.0-beta.22"
29
+ "@authup/core-kit": "^1.0.0-beta.24"
30
30
  },
31
31
  "peerDependencies": {
32
- "@authup/core-kit": "^1.0.0-beta.22"
32
+ "@authup/core-kit": "^1.0.0-beta.24"
33
33
  },
34
34
  "dependencies": {
35
- "nanoid": "^3.3.4"
35
+ "nanoid": "^3.3.8"
36
36
  },
37
37
  "gitHead": "5d3b6f4ce1edf2383bdfbf66e913a08c8a3a2e40",
38
38
  "publishConfig": {
@@ -0,0 +1,12 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export enum AsymmetricCryptoAlgorithmName {
9
+ RSA_OAEP = 'RSA-OAEP',
10
+
11
+ ECDH = 'ECDH',
12
+ }
@@ -0,0 +1,58 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { AsymmetricAlgorithmImportParams } from './types';
9
+
10
+ function arrayBufferToBase64(arrayBuffer: ArrayBuffer): string {
11
+ return btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
12
+ }
13
+
14
+ export async function exportAsymmetricPublicKey(key: CryptoKey): Promise<string> {
15
+ const exported = await crypto.subtle.exportKey(
16
+ 'spki',
17
+ key,
18
+ );
19
+
20
+ return `-----BEGIN PUBLIC KEY-----\n${arrayBufferToBase64(exported)}\n-----END PUBLIC KEY-----`;
21
+ }
22
+
23
+ export async function exportAsymmetricPrivateKey(key: CryptoKey): Promise<string> {
24
+ const exported = await crypto.subtle.exportKey(
25
+ 'pkcs8',
26
+ key,
27
+ );
28
+
29
+ return `-----BEGIN PRIVATE KEY-----\n${arrayBufferToBase64(exported)}\n-----END PRIVATE KEY-----`;
30
+ }
31
+
32
+ export async function importAsymmetricPublicKey(
33
+ pem: string,
34
+ params: AsymmetricAlgorithmImportParams,
35
+ ): Promise<CryptoKey> {
36
+ const pemHeader = '-----BEGIN PUBLIC KEY-----';
37
+ const pemFooter = '-----END PUBLIC KEY-----';
38
+ const pemContents = pem.substring(pemHeader.length, pem.length - pemFooter.length);
39
+ const buffer = Buffer.from(pemContents, 'base64');
40
+
41
+ if (params.name === 'ECDH') {
42
+ return crypto.subtle.importKey(
43
+ 'spki',
44
+ buffer,
45
+ params,
46
+ true,
47
+ ['deriveKey'],
48
+ );
49
+ }
50
+
51
+ return crypto.subtle.importKey(
52
+ 'spki',
53
+ buffer,
54
+ params,
55
+ true,
56
+ ['encrypt'],
57
+ );
58
+ }
@@ -0,0 +1,11 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './constants';
9
+ export * from './helpers';
10
+ export * from './module';
11
+ export * from './types';
@@ -0,0 +1,190 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import { AsymmetricCryptoAlgorithmName } from './constants';
9
+ import type { AsymmetricAlgorithmImportParams, AsymmetricAlgorithmParams } from './types';
10
+ import { exportAsymmetricPrivateKey, exportAsymmetricPublicKey } from './helpers';
11
+
12
+ export class CryptoAsymmetricAlgorithm {
13
+ public readonly algorithm: AsymmetricAlgorithmParams;
14
+
15
+ protected keyPair : CryptoKeyPair | undefined;
16
+
17
+ constructor(algorithm: AsymmetricAlgorithmParams) {
18
+ if (algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {
19
+ algorithm = {
20
+ ...algorithm,
21
+ publicExponent: new Uint8Array([1, 0, 1]),
22
+ };
23
+ }
24
+
25
+ this.algorithm = algorithm;
26
+ }
27
+
28
+ buildImportParams() : AsymmetricAlgorithmImportParams {
29
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {
30
+ return {
31
+ name: 'RSA-OAEP',
32
+ hash: 'SHA-256',
33
+ };
34
+ }
35
+
36
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {
37
+ return {
38
+ name: 'ECDH',
39
+ namedCurve: (this.algorithm as EcKeyGenParams).namedCurve,
40
+ hash: 'SHA-256',
41
+ };
42
+ }
43
+
44
+ throw new Error('Import params could not be created.');
45
+ }
46
+
47
+ async generateKeyPair() : Promise<CryptoKeyPair> {
48
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {
49
+ this.keyPair = await crypto.subtle.generateKey(
50
+ this.algorithm,
51
+ true,
52
+ ['encrypt', 'decrypt'],
53
+ );
54
+
55
+ return this.keyPair;
56
+ }
57
+
58
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {
59
+ this.keyPair = await crypto.subtle.generateKey(
60
+ this.algorithm,
61
+ true,
62
+ ['deriveKey'],
63
+ );
64
+
65
+ return this.keyPair;
66
+ }
67
+
68
+ throw new Error('The algorithm is not supported for key generation.');
69
+ }
70
+
71
+ async useKeyPair(): Promise<CryptoKeyPair> {
72
+ if (typeof this.keyPair !== 'undefined') {
73
+ return this.keyPair;
74
+ }
75
+
76
+ return this.generateKeyPair();
77
+ }
78
+
79
+ async exportPublicKey(): Promise<string> {
80
+ const keyPair = await this.useKeyPair();
81
+
82
+ return exportAsymmetricPublicKey(keyPair.publicKey);
83
+ }
84
+
85
+ async exportPrivateKey(): Promise<string> {
86
+ const keyPair = await this.useKeyPair();
87
+
88
+ return exportAsymmetricPrivateKey(keyPair.privateKey);
89
+ }
90
+
91
+ async encrypt(data: Buffer, remoteKey?: CryptoKey) {
92
+ const keyPair = await this.useKeyPair();
93
+
94
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {
95
+ return crypto.subtle.encrypt(
96
+ {
97
+ name: 'RSA-OAEP',
98
+ },
99
+ remoteKey || keyPair.publicKey,
100
+ data,
101
+ );
102
+ }
103
+
104
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {
105
+ if (typeof remoteKey === 'undefined') {
106
+ throw new Error('Remote public key is required.');
107
+ }
108
+
109
+ const array = new Uint8Array(16);
110
+ const iv = crypto.getRandomValues(array);
111
+ const key = await crypto.subtle.deriveKey(
112
+ {
113
+ name: 'ECDH',
114
+ public: remoteKey,
115
+ },
116
+ keyPair.privateKey,
117
+ {
118
+ name: 'AES-GCM',
119
+ length: 256,
120
+ },
121
+ true,
122
+ ['encrypt'],
123
+ );
124
+
125
+ const arrayBuffer = await crypto.subtle.encrypt(
126
+ {
127
+ name: 'AES-GCM',
128
+ length: 256,
129
+ iv,
130
+ },
131
+ key,
132
+ data,
133
+ );
134
+
135
+ const buffer = Buffer.from(arrayBuffer);
136
+
137
+ return Buffer.concat([iv, buffer]);
138
+ }
139
+
140
+ throw new Error('Unsupported algorithm for encryption.');
141
+ }
142
+
143
+ async decrypt(data: Buffer, remoteKey?: CryptoKey) {
144
+ const keyPair = await this.useKeyPair();
145
+
146
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.RSA_OAEP) {
147
+ return crypto.subtle.decrypt(
148
+ {
149
+ name: 'RSA-OAEP',
150
+ },
151
+ keyPair.privateKey,
152
+ data,
153
+ );
154
+ }
155
+
156
+ if (this.algorithm.name === AsymmetricCryptoAlgorithmName.ECDH) {
157
+ if (typeof remoteKey === 'undefined') {
158
+ throw new Error('Remote public key is required.');
159
+ }
160
+
161
+ const iv = data.slice(0, 16);
162
+
163
+ const key = await crypto.subtle.deriveKey(
164
+ {
165
+ name: 'ECDH',
166
+ public: remoteKey,
167
+ },
168
+ keyPair.privateKey,
169
+ {
170
+ name: 'AES-GCM',
171
+ length: 256,
172
+ },
173
+ true,
174
+ ['decrypt'],
175
+ );
176
+
177
+ return crypto.subtle.decrypt(
178
+ {
179
+ name: 'AES-GCM',
180
+ length: 256,
181
+ iv,
182
+ },
183
+ key,
184
+ data.slice(16),
185
+ );
186
+ }
187
+
188
+ throw new Error('Unsupported algorithm for decryption.');
189
+ }
190
+ }
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export type AsymmetricAlgorithmParams = RsaHashedKeyGenParams | EcKeyGenParams;
9
+ export type AsymmetricAlgorithmImportParams = RsaHashedImportParams | EcKeyImportParams;
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './asymmetric';
9
+ export * from './symmetric';
@@ -0,0 +1,9 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ export * from './module';
9
+ export * from './types';
@@ -0,0 +1,71 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { SymmetricAlgorithmParams } from './types';
9
+
10
+ export class CryptoSymmetricAlgorithm {
11
+ protected algorithm : SymmetricAlgorithmParams;
12
+
13
+ constructor(algorithm: SymmetricAlgorithmParams) {
14
+ this.algorithm = algorithm;
15
+ }
16
+
17
+ async generateKey() : Promise<CryptoKey> {
18
+ return crypto.subtle.generateKey(
19
+ {
20
+ name: this.algorithm.name,
21
+ length: 256,
22
+ },
23
+ true,
24
+ ['encrypt', 'decrypt'],
25
+ );
26
+ }
27
+
28
+ async importKey(buffer: Buffer | ArrayBuffer) : Promise<CryptoKey> {
29
+ return crypto.subtle.importKey(
30
+ 'raw',
31
+ buffer,
32
+ {
33
+ name: this.algorithm.name,
34
+ length: 256,
35
+ },
36
+ true,
37
+ ['encrypt', 'decrypt'],
38
+ );
39
+ }
40
+
41
+ async encrypt(key: CryptoKey, iv: Buffer, data: Buffer) : Promise<Buffer> {
42
+ const arrayBuffer = await crypto.subtle.encrypt(
43
+ {
44
+ name: this.algorithm.name,
45
+ length: 256,
46
+ iv,
47
+ },
48
+ key,
49
+ data,
50
+ );
51
+
52
+ const buffer = Buffer.from(arrayBuffer);
53
+
54
+ return Buffer.concat([iv, buffer]);
55
+ }
56
+
57
+ async decrypt(key: CryptoKey, data: Buffer) : Promise<Buffer> {
58
+ const iv = data.slice(0, 16);
59
+ const arrayBuffer = await crypto.subtle.decrypt(
60
+ {
61
+ name: this.algorithm.name,
62
+ length: 256,
63
+ iv,
64
+ },
65
+ key,
66
+ data.slice(16),
67
+ );
68
+
69
+ return Buffer.from(arrayBuffer);
70
+ }
71
+ }
@@ -0,0 +1,10 @@
1
+ /*
2
+ * Copyright (c) 2024.
3
+ * Author Peter Placzek (tada5hi)
4
+ * For the full copyright and license information,
5
+ * view the LICENSE file that was distributed with this source code.
6
+ */
7
+
8
+ import type { webcrypto } from 'crypto';
9
+
10
+ export type SymmetricAlgorithmParams = webcrypto.AesKeyGenParams;
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  * view the LICENSE file that was distributed with this source code.
6
6
  */
7
7
 
8
+ export * from './crypto';
8
9
  export * from './domains';
9
10
  export * from './utils';
10
11
  export * from './constants';