From 20a7add862f6645f20fc8a010e84aaa9c740978f Mon Sep 17 00:00:00 2001 From: SayMoon Date: Tue, 27 May 2025 12:52:22 +0200 Subject: [PATCH 1/5] Add examples --- pgp-decrypt/index.ts | 59 +++++++++++++++++++++++++++++++++ pgp-decrypt/package.json | 8 +++++ pgp-encrypt/index.ts | 70 ++++++++++++++++++++++++++++++++++++++++ pgp-encrypt/package.json | 8 +++++ rsa-decrypt/index.ts | 69 +++++++++++++++++++++++++++++++++++++++ rsa-decrypt/package.json | 8 +++++ rsa-encrypt/index.ts | 61 ++++++++++++++++++++++++++++++++++ rsa-encrypt/package.json | 8 +++++ 8 files changed, 291 insertions(+) create mode 100644 pgp-decrypt/index.ts create mode 100644 pgp-decrypt/package.json create mode 100644 pgp-encrypt/index.ts create mode 100644 pgp-encrypt/package.json create mode 100644 rsa-decrypt/index.ts create mode 100644 rsa-decrypt/package.json create mode 100644 rsa-encrypt/index.ts create mode 100644 rsa-encrypt/package.json diff --git a/pgp-decrypt/index.ts b/pgp-decrypt/index.ts new file mode 100644 index 0000000..cb8e369 --- /dev/null +++ b/pgp-decrypt/index.ts @@ -0,0 +1,59 @@ +export function getDescription() { + return { + description: "PGP Decrypt.", + input: [ + { + id: "fileToDecrypt", + displayName: "File to be decrypted", + description: "File to be decrypted.", + type: "InputResource", + required: true, + defaultValue: "blob://file.encrypted", + }, + { + id: "privateCertificate", + displayName: "Private Certificate", + description: "Private certificate used for decryption.", + type: "Certificate", + required: true, + }, + { + id: "decryptedFile", + displayName: "Decrypted file", + description: "Contains the decrypted data (type Connector is used because this files is written and read in this example).", + type: "Connector", + required: true, + defaultValue: "blob://decryptedFile.txt", + }, + ], + output: [], + } as const satisfies ScriptDescription; +} + +export async function execute(context: Context): Promise { + const fileToDecrypt = context.getFile(context.parameters.fileToDecrypt); + const decryptedFile = context.getFile(context.parameters.decryptedFile); + + const privateKey = await importPrivateKey(context); + console.log("Private key imported"); + + await crypto.subtle.decrypt("PGP", privateKey, fileToDecrypt, decryptedFile); + console.log("File decrypted"); + + // Only for an example purpose + await checkDecrypted(decryptedFile); +} + +async function importPrivateKey(context: Context): Promise { + const algorithmParams: PgpParams = { name: "PGP" }; + const keyUse = ["decrypt"]; + + return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.certificate, algorithmParams, false, keyUse); +} + +async function checkDecrypted(decryptedFile: File): Promise { + const decryptedData = await decryptedFile.read(); + const expectedData = "this will be encrypted"; // This is the original data used in the encryption example + + console.log(`Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'`); +} diff --git a/pgp-decrypt/package.json b/pgp-decrypt/package.json new file mode 100644 index 0000000..a97b3e0 --- /dev/null +++ b/pgp-decrypt/package.json @@ -0,0 +1,8 @@ +{ + "name": "pgp-decrypt", + "displayName": "PGP Decrypt operation", + "description": "Decrypts a file using PGP.", + "fromVersion": "25.06.1.1", + "toVersion": null, + "private": true +} diff --git a/pgp-encrypt/index.ts b/pgp-encrypt/index.ts new file mode 100644 index 0000000..b115fff --- /dev/null +++ b/pgp-encrypt/index.ts @@ -0,0 +1,70 @@ +export function getDescription() { + return { + description: "PGP Encrypt.", + input: [ + { + id: "fileToEncrypt", + displayName: "File to be encrypted", + description: "File to be encrypted (type Connector used because this files is written and read in this example).", + type: "Connector", + required: true, + defaultValue: "blob://fileToEncrypt.txt", + }, + { + id: "publicCertificate", + displayName: "Public Certificate", + description: "Public certificate used for encryption.", + type: "Certificate", + required: true, + }, + { + id: "symmetricAlgorithmName", + displayName: + "Symmetric algorithm name (Idea, TripleDes, Cast5, Blowfish, Des, Aes128, Aes192, Aes256, Twofish, Camellia128, Camellia192, Camellia256)", + description: + "Name of the RSA hash algorithm (Idea, TripleDes, Cast5, Blowfish, Des, Aes128, Aes192, Aes256, Twofish, Camellia128, Camellia192, Camellia256).", + type: "String", + defaultValue: "Aes256", + required: true, + }, + { + id: "encryptedFile", + displayName: "Encrypted file", + description: "Contains the encrypted data (type Connector is used because this files is written and read in this example).", + type: "Connector", + required: true, + defaultValue: "blob://file.encrypted", + }, + ], + output: [], + } as const satisfies ScriptDescription; +} + +export async function execute(context: Context): Promise { + const fileToEncrypt = context.getFile(context.parameters.fileToEncrypt); + const encryptedFile = context.getFile(context.parameters.encryptedFile); + + // Example data to encrypt; only for an example purpose + await fileToEncrypt.write("this will be encrypted"); + console.log("Example data written to the file for the encryption"); + + const publicKey = await importPublicKey(context); + console.log("Public key imported"); + + await crypto.subtle.encrypt("PGP", publicKey, fileToEncrypt, encryptedFile); + console.log("File encrypted"); +} + +async function importPublicKey(context: Context): Promise { + const algorithmParams: PgpParams = { + name: "PGP", + symmetricKeyAlgorithm: context.parameters.symmetricAlgorithmName, + enableArmor: false, + enableIntegrityCheck: true, + compressionAlgorithm: "zlib", + compressionLevel: 6, + }; + const keyUse: KeyUsage[] = ["encrypt"]; + + return await crypto.subtle.importKey("spkifromparameterinput", context.parameters.publicCertificate, algorithmParams, false, keyUse); +} diff --git a/pgp-encrypt/package.json b/pgp-encrypt/package.json new file mode 100644 index 0000000..c9bcdee --- /dev/null +++ b/pgp-encrypt/package.json @@ -0,0 +1,8 @@ +{ + "name": "pgp-encrypt", + "displayName": "PGP Encrypt operation", + "description": "Encrypts a data using PGP.", + "fromVersion": "25.06.1.1", + "toVersion": null, + "private": true +} diff --git a/rsa-decrypt/index.ts b/rsa-decrypt/index.ts new file mode 100644 index 0000000..a7e177e --- /dev/null +++ b/rsa-decrypt/index.ts @@ -0,0 +1,69 @@ +export function getDescription() { + return { + description: "RSA Decrypt.", + input: [ + { + id: "fileToDecrypt", + displayName: "File to be decrypted", + description: "File to be decrypted.", + type: "InputResource", + required: true, + defaultValue: "blob://file.encrypted", + }, + { + id: "privateCertificate", + displayName: "Private Certificate", + description: "Private certificate used for decryption.", + type: "Certificate", + required: true, + }, + { + id: "rsaHashAlgorithmName", + displayName: "RSA hash algorithm name (SHA-1, SHA-256, SHA-384, SHA-512)", + description: "Name of the RSA hash algorithm (SHA-1, SHA-256, SHA-384, SHA-512).", + type: "String", + defaultValue: "SHA-256", + required: true, + }, + { + id: "decryptedFile", + displayName: "Decrypted file", + description: "Contains the decrypted data (type Connector is used because this files is written and read in this example).", + type: "Connector", + required: true, + defaultValue: "blob://decryptedFile.txt", + }, + ], + output: [], + } as const satisfies ScriptDescription; +} + +export async function execute(context: Context): Promise { + const fileToDecrypt = context.getFile(context.parameters.fileToDecrypt); + const decryptedFile = context.getFile(context.parameters.decryptedFile); + + const privateKey = await importPrivateKey(context); + console.log("Private key imported"); + + await crypto.subtle.decrypt("RSA-OAEP", privateKey, fileToDecrypt, decryptedFile); + console.log("File decrypted"); + + // Only for an example purpose + await checkDecrypted(decryptedFile); +} + +async function importPrivateKey(context: Context): Promise { + const algorithmParams: RsaOaepParams = { name: "RSA-OAEP", hash: context.parameters.rsaHashAlgorithmName }; + const keyUse = ["decrypt"]; + + return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.certificate, algorithmParams, false, keyUse); +} + +async function checkDecrypted(decryptedFile: File): Promise { + const decryptedData = await decryptedFile.read(); + const expectedData = "this will be encrypted"; // This is the original data used in the encryption example + + console.log( + `Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'` + ); +} diff --git a/rsa-decrypt/package.json b/rsa-decrypt/package.json new file mode 100644 index 0000000..30adeef --- /dev/null +++ b/rsa-decrypt/package.json @@ -0,0 +1,8 @@ +{ + "name": "rsa-decrypt", + "displayName": "RSA Decrypt operation", + "description": "Decrypts a file using RSA.", + "fromVersion": "25.06.1.1", + "toVersion": null, + "private": true +} diff --git a/rsa-encrypt/index.ts b/rsa-encrypt/index.ts new file mode 100644 index 0000000..adefbc7 --- /dev/null +++ b/rsa-encrypt/index.ts @@ -0,0 +1,61 @@ +export function getDescription() { + return { + description: "RSA Encrypt.", + input: [ + { + id: "fileToEncrypt", + displayName: "File to be encrypted", + description: "File to be encrypted (type Connector used because this files is written and read in this example).", + type: "Connector", + required: true, + defaultValue: "blob://fileToEncrypt.txt", + }, + { + id: "publicCertificate", + displayName: "Public Certificate", + description: "Public certificate used for encryption.", + type: "Certificate", + required: true, + }, + { + id: "rsaHashAlgorithmName", + displayName: "RSA hash algorithm name (SHA-1, SHA-256, SHA-384, SHA-512)", + description: "Name of the RSA hash algorithm (SHA-1, SHA-256, SHA-384, SHA-512).", + type: "String", + defaultValue: "SHA-256", + required: true, + }, + { + id: "encryptedFile", + displayName: "Encrypted file", + description: "Contains the encrypted data (type Connector is used because this files is written and read in this example).", + type: "Connector", + required: true, + defaultValue: "blob://file.encrypted", + }, + ], + output: [], + } as const satisfies ScriptDescription; +} + +export async function execute(context: Context): Promise { + const fileToEncrypt = context.getFile(context.parameters.fileToEncrypt); + const encryptedFile = context.getFile(context.parameters.encryptedFile); + + // Example data to encrypt; only for an example purpose + await fileToEncrypt.write("this will be encrypted"); + console.log("Example data written to the file for the encryption"); + + const publicKey = await importPublicKey(context); + console.log("Public key imported"); + + await crypto.subtle.encrypt("RSA-OAEP", publicKey, fileToEncrypt, encryptedFile); + console.log("File encrypted"); +} + +async function importPublicKey(context: Context): Promise { + const algorithmParams: RsaOaepParams = { name: "RSA-OAEP", hash: context.parameters.rsaHashAlgorithmName }; + const keyUse: KeyUsage[] = ["encrypt"]; + + return await crypto.subtle.importKey("spkifromparameterinput", context.parameters.publicCertificate, algorithmParams, false, keyUse); +} diff --git a/rsa-encrypt/package.json b/rsa-encrypt/package.json new file mode 100644 index 0000000..9e56f98 --- /dev/null +++ b/rsa-encrypt/package.json @@ -0,0 +1,8 @@ +{ + "name": "rsa-encrypt", + "displayName": "RSA Encrypt operation", + "description": "Encrypts a data using RSA.", + "fromVersion": "25.06.1.1", + "toVersion": null, + "private": true +} From 34bb8ef508234d0194307dfd2d7595c5a8c6d168 Mon Sep 17 00:00:00 2001 From: SayMoon Date: Tue, 27 May 2025 13:39:46 +0200 Subject: [PATCH 2/5] Please prettier --- pgp-encrypt/index.ts | 6 ++---- rsa-decrypt/index.ts | 4 +--- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/pgp-encrypt/index.ts b/pgp-encrypt/index.ts index b115fff..2a57187 100644 --- a/pgp-encrypt/index.ts +++ b/pgp-encrypt/index.ts @@ -19,10 +19,8 @@ export function getDescription() { }, { id: "symmetricAlgorithmName", - displayName: - "Symmetric algorithm name (Idea, TripleDes, Cast5, Blowfish, Des, Aes128, Aes192, Aes256, Twofish, Camellia128, Camellia192, Camellia256)", - description: - "Name of the RSA hash algorithm (Idea, TripleDes, Cast5, Blowfish, Des, Aes128, Aes192, Aes256, Twofish, Camellia128, Camellia192, Camellia256).", + displayName: "Symmetric algorithm name (Idea, TripleDes, Cast5, Blowfish, Des, Aes128, Aes192, Aes256, Twofish, Camellia128, Camellia192, Camellia256)", + description: "Name of the RSA hash algorithm (Idea, TripleDes, Cast5, Blowfish, Des, Aes128, Aes192, Aes256, Twofish, Camellia128, Camellia192, Camellia256).", type: "String", defaultValue: "Aes256", required: true, diff --git a/rsa-decrypt/index.ts b/rsa-decrypt/index.ts index a7e177e..5d19af6 100644 --- a/rsa-decrypt/index.ts +++ b/rsa-decrypt/index.ts @@ -63,7 +63,5 @@ async function checkDecrypted(decryptedFile: File): Promise { const decryptedData = await decryptedFile.read(); const expectedData = "this will be encrypted"; // This is the original data used in the encryption example - console.log( - `Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'` - ); + console.log(`Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'`); } From 6f4fe4f18ecbc41875b9e2d8ec7640c930ddfc16 Mon Sep 17 00:00:00 2001 From: SayMoon Date: Thu, 29 May 2025 11:11:38 +0200 Subject: [PATCH 3/5] WIP --- pgp-decrypt/index.ts | 13 +++++++++---- rsa-decrypt/index.ts | 13 +++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pgp-decrypt/index.ts b/pgp-decrypt/index.ts index cb8e369..3c456c3 100644 --- a/pgp-decrypt/index.ts +++ b/pgp-decrypt/index.ts @@ -46,14 +46,19 @@ export async function execute(context: Context): Promise { async function importPrivateKey(context: Context): Promise { const algorithmParams: PgpParams = { name: "PGP" }; - const keyUse = ["decrypt"]; + const keyUse: KeyUsage = ["decrypt"]; - return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.certificate, algorithmParams, false, keyUse); + return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.privateCertificate, algorithmParams, false, keyUse); } -async function checkDecrypted(decryptedFile: File): Promise { +async function checkDecrypted(decryptedFile: IFile): Promise { const decryptedData = await decryptedFile.read(); const expectedData = "this will be encrypted"; // This is the original data used in the encryption example - console.log(`Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'`); + console.log( + `Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'` + ); + if (decryptedData !== expectedData) { + throw new Error("Decrypted data does not match the original data."); + } } diff --git a/rsa-decrypt/index.ts b/rsa-decrypt/index.ts index 5d19af6..fd27ff2 100644 --- a/rsa-decrypt/index.ts +++ b/rsa-decrypt/index.ts @@ -54,14 +54,19 @@ export async function execute(context: Context): Promise { async function importPrivateKey(context: Context): Promise { const algorithmParams: RsaOaepParams = { name: "RSA-OAEP", hash: context.parameters.rsaHashAlgorithmName }; - const keyUse = ["decrypt"]; + const keyUse: KeyUsage = ["decrypt"]; - return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.certificate, algorithmParams, false, keyUse); + return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.privateCertificate, algorithmParams, false, keyUse); } -async function checkDecrypted(decryptedFile: File): Promise { +async function checkDecrypted(decryptedFile: IFile): Promise { const decryptedData = await decryptedFile.read(); const expectedData = "this will be encrypted"; // This is the original data used in the encryption example - console.log(`Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'`); + console.log( + `Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'` + ); + if (decryptedData !== expectedData) { + throw new Error("Decrypted data does not match the original data."); + } } From dc636be75caf4d2b1958ffff042c4ae800102925 Mon Sep 17 00:00:00 2001 From: SayMoon Date: Mon, 2 Jun 2025 13:14:43 +0200 Subject: [PATCH 4/5] WIP --- pgp-encrypt/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgp-encrypt/index.ts b/pgp-encrypt/index.ts index 2a57187..ccefea8 100644 --- a/pgp-encrypt/index.ts +++ b/pgp-encrypt/index.ts @@ -56,7 +56,7 @@ export async function execute(context: Context): Promise { async function importPublicKey(context: Context): Promise { const algorithmParams: PgpParams = { name: "PGP", - symmetricKeyAlgorithm: context.parameters.symmetricAlgorithmName, + symmetricKeyAlgorithm: context.parameters.symmetricAlgorithmName as PgpSymmetricKeyAlgorithm, enableArmor: false, enableIntegrityCheck: true, compressionAlgorithm: "zlib", From f3c5f6f1363bc66d0d526ad9158f79bd81e9748d Mon Sep 17 00:00:00 2001 From: SayMoon Date: Mon, 2 Jun 2025 14:18:46 +0200 Subject: [PATCH 5/5] WIP --- pgp-decrypt/index.ts | 4 +--- rsa-decrypt/index.ts | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/pgp-decrypt/index.ts b/pgp-decrypt/index.ts index 3c456c3..6507c07 100644 --- a/pgp-decrypt/index.ts +++ b/pgp-decrypt/index.ts @@ -55,9 +55,7 @@ async function checkDecrypted(decryptedFile: IFile): Promise { const decryptedData = await decryptedFile.read(); const expectedData = "this will be encrypted"; // This is the original data used in the encryption example - console.log( - `Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'` - ); + console.log(`Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'`); if (decryptedData !== expectedData) { throw new Error("Decrypted data does not match the original data."); } diff --git a/rsa-decrypt/index.ts b/rsa-decrypt/index.ts index fd27ff2..4adaf40 100644 --- a/rsa-decrypt/index.ts +++ b/rsa-decrypt/index.ts @@ -63,9 +63,7 @@ async function checkDecrypted(decryptedFile: IFile): Promise { const decryptedData = await decryptedFile.read(); const expectedData = "this will be encrypted"; // This is the original data used in the encryption example - console.log( - `Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'` - ); + console.log(`Decrypted data: '${decryptedData}' is the same as the original data: '${expectedData}' -> '${decryptedData === expectedData}'`); if (decryptedData !== expectedData) { throw new Error("Decrypted data does not match the original data."); }