diff --git a/pgp-decrypt/index.ts b/pgp-decrypt/index.ts new file mode 100644 index 0000000..6507c07 --- /dev/null +++ b/pgp-decrypt/index.ts @@ -0,0 +1,62 @@ +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: KeyUsage = ["decrypt"]; + + return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.privateCertificate, algorithmParams, false, keyUse); +} + +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}'`); + if (decryptedData !== expectedData) { + throw new Error("Decrypted data does not match the original data."); + } +} 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..ccefea8 --- /dev/null +++ b/pgp-encrypt/index.ts @@ -0,0 +1,68 @@ +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 as PgpSymmetricKeyAlgorithm, + 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..4adaf40 --- /dev/null +++ b/rsa-decrypt/index.ts @@ -0,0 +1,70 @@ +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: KeyUsage = ["decrypt"]; + + return await crypto.subtle.importKey("pkcs8fromparameterinput", context.parameters.privateCertificate, algorithmParams, false, keyUse); +} + +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}'`); + if (decryptedData !== expectedData) { + throw new Error("Decrypted data does not match the original data."); + } +} 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 +}