Skip to content

Commit

Permalink
Update pgp method params
Browse files Browse the repository at this point in the history
  • Loading branch information
dinukaamarasinghe817 committed May 9, 2024
1 parent 9110389 commit 04f59c4
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 25 deletions.
14 changes: 7 additions & 7 deletions ballerina/encrypt_decrypt.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/pgp_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ public enum SymmetricKeyAlgorithmTags {
CAMELLIA_128 = "11",
CAMELLIA_192 = "12",
CAMELLIA_256 = "13"
}
}
4 changes: 2 additions & 2 deletions ballerina/tests/encrypt_decrypt_pgp_test.bal
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,5 +448,4 @@ private static Digest selectHash(String algorithm) {
}
throw CryptoUtils.createError("Unsupported algorithm: " + algorithm);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()),
Expand All @@ -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());
}
Expand Down

0 comments on commit 04f59c4

Please sign in to comment.