diff --git a/ballerina/encrypt_decrypt.bal b/ballerina/encrypt_decrypt.bal index 895f694..c458e16 100644 --- a/ballerina/encrypt_decrypt.bal +++ b/ballerina/encrypt_decrypt.bal @@ -251,17 +251,17 @@ public isolated function decryptAesGcm(byte[] input, byte[] key, byte[] iv, AesP # byte[] cipherText = check crypto:encryptPgp(message, publicKey); # ``` # -# + input - The content to be encrypted -# + key - Public key +# + plainText - The content to be encrypted +# + publicKey - Public key # + options - PGP encryption options # + return - Encrypted data or else a `crypto:Error` if the key is invalid -public isolated function encryptPgp(byte[] input, byte[] key, *PgpEncryptionOptions options) +public isolated function encryptPgp(byte[] plainText, byte[] publicKey, *PgpEncryptionOptions options) returns byte[]|Error = @java:Method { name: "encryptPgp", 'class: "io.ballerina.stdlib.crypto.nativeimpl.Encrypt" } external; -# Returns the PGP-decrypted value for the given PGP-encrypted data. +# Returns the PGP-decrypted value of the given PGP-encrypted data. # ```ballerina # byte[] message = "Hello Ballerina!".toBytes(); # byte[] publicKey = check io:fileReadBytes("public_key.asc"); // provide the path to the public key @@ -272,11 +272,11 @@ public isolated function encryptPgp(byte[] input, byte[] key, *PgpEncryptionOpti # byte[] decryptedMessage = check crypto:decryptPgp(cipherText, privateKey, passphrase); # ``` # -# + input - The encrypted content to be decrypted -# + key - Private key +# + cipherText - The encrypted content to be decrypted +# + privateKey - Private key # + passphrase - passphrase of the private key # + return - Decrypted data or else a `crypto:Error` if the key or passphrase is invalid -public isolated function decryptPgp(byte[] input, byte[] key, byte[] passphrase) +public isolated function decryptPgp(byte[] cipherText, byte[] privateKey, byte[] passphrase) returns byte[]|Error = @java:Method { name: "decryptPgp", 'class: "io.ballerina.stdlib.crypto.nativeimpl.Decrypt" diff --git a/ballerina/pgp_utils.bal b/ballerina/pgp_utils.bal index b787806..161a2ef 100644 --- a/ballerina/pgp_utils.bal +++ b/ballerina/pgp_utils.bal @@ -43,4 +43,4 @@ public enum SymmetricKeyAlgorithmTags { CAMELLIA_128 = "11", CAMELLIA_192 = "12", CAMELLIA_256 = "13" -} \ No newline at end of file +} diff --git a/ballerina/tests/encrypt_decrypt_pgp_test.bal b/ballerina/tests/encrypt_decrypt_pgp_test.bal index 3644fb0..4c4096a 100644 --- a/ballerina/tests/encrypt_decrypt_pgp_test.bal +++ b/ballerina/tests/encrypt_decrypt_pgp_test.bal @@ -1,6 +1,6 @@ -// Copyright (c) 2020 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// Copyright (c) 2024 WSO2 LLC. (https://www.wso2.com). // -// WSO2 Inc. licenses this file to you under the Apache License, +// WSO2 LLC. licenses this file to you under the Apache License, // Version 2.0 (the "License"); you may not use this file except // in compliance with the License. // You may obtain a copy of the License at diff --git a/native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java b/native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java index d0376c3..f34e024 100644 --- a/native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java +++ b/native/src/main/java/io/ballerina/stdlib/crypto/CryptoUtils.java @@ -448,5 +448,4 @@ private static Digest selectHash(String algorithm) { } throw CryptoUtils.createError("Unsupported algorithm: " + algorithm); } - } diff --git a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decrypt.java b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decrypt.java index 521c671..36b4733 100644 --- a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decrypt.java +++ b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Decrypt.java @@ -80,18 +80,18 @@ public static Object decryptRsaEcb(BArray inputValue, Object keys, Object paddin input, null, -1); } - public static Object decryptPgp(BArray inputValue, BArray keyValue, BArray passphrase) { - byte[] input = inputValue.getBytes(); - byte[] key = keyValue.getBytes(); + public static Object decryptPgp(BArray cipherTextValue, BArray privateKeyValue, BArray passphrase) { + byte[] cipherText = cipherTextValue.getBytes(); + byte[] privateKey = privateKeyValue.getBytes(); byte[] passphraseInBytes = passphrase.getBytes(); - InputStream keyStream = new ByteArrayInputStream(key); + InputStream keyStream = new ByteArrayInputStream(privateKey); try { PgpDecryptionGenerator pgpDecryptionGenerator = new PgpDecryptionGenerator( keyStream, passphraseInBytes ); - return pgpDecryptionGenerator.decrypt(input); + return pgpDecryptionGenerator.decrypt(cipherText); } catch (IOException | PGPException e) { return CryptoUtils.createError("Error occurred while PGP decrypt: " + e.getMessage()); } diff --git a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Encrypt.java b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Encrypt.java index ff3b2aa..3930817 100644 --- a/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Encrypt.java +++ b/native/src/main/java/io/ballerina/stdlib/crypto/nativeimpl/Encrypt.java @@ -21,13 +21,9 @@ import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BArray; import io.ballerina.runtime.api.values.BMap; -import io.ballerina.runtime.api.values.BString; -import io.ballerina.runtime.api.values.BValue; import io.ballerina.stdlib.crypto.Constants; import io.ballerina.stdlib.crypto.CryptoUtils; import io.ballerina.stdlib.crypto.PgpEncryptionGenerator; -import org.bouncycastle.bcpg.CompressionAlgorithmTags; -import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags; import org.bouncycastle.openpgp.PGPException; import java.io.ByteArrayInputStream; @@ -96,10 +92,10 @@ public static Object encryptRsaEcb(BArray inputValue, Object keys, Object paddin input, null, -1); } - public static Object encryptPgp(BArray inputValue, BArray keyValue, BMap options) { - byte[] input = inputValue.getBytes(); - byte[] key = keyValue.getBytes(); - InputStream keyStream = new ByteArrayInputStream(key); + public static Object encryptPgp(BArray plainTextValue, BArray publicKeyValue, BMap options) { + byte[] plainText = plainTextValue.getBytes(); + byte[] publicKey = publicKeyValue.getBytes(); + InputStream publicKeyStream = new ByteArrayInputStream(publicKey); PgpEncryptionGenerator pgpEncryptionGenerator = new PgpEncryptionGenerator( Integer.parseInt(options.get(StringUtils.fromString(COMPRESSION_ALGORITHM)).toString()), @@ -109,7 +105,7 @@ public static Object encryptPgp(BArray inputValue, BArray keyValue, BMap options ); try { - return pgpEncryptionGenerator.encrypt(input, keyStream); + return pgpEncryptionGenerator.encrypt(plainText, publicKeyStream); } catch (PGPException | IOException e) { return CryptoUtils.createError("Error occurred while PGP encrypt: " + e.getMessage()); }