-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #555 from dinukaamarasinghe817/pgp
Add support for PGP Encryption/Decryption
- Loading branch information
Showing
17 changed files
with
830 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2024 WSO2 LLC. (https://www.wso2.com). | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
# Represents the PGP encryption options. | ||
# | ||
# + compressionAlgorithm - Specifies the compression algorithm used for PGP encryption | ||
# + symmetricKeyAlgorithm - Specifies the symmetric key algorithm used for encryption | ||
# + armor - Indicates whether ASCII armor is enabled for the encrypted output | ||
# + withIntegrityCheck - Indicates whether integrity check is included in the encryption | ||
public type Options record {| | ||
CompressionAlgorithmTags compressionAlgorithm = ZIP; | ||
SymmetricKeyAlgorithmTags symmetricKeyAlgorithm = AES_256; | ||
boolean armor = true; | ||
boolean withIntegrityCheck = true; | ||
|}; | ||
|
||
# Represents the compression algorithms available in PGP. | ||
# | ||
# + UNCOMPRESSED - No compression | ||
# + ZIP - Uses (RFC 1951) compression | ||
# + ZLIB - Uses (RFC 1950) compression | ||
# + BZIP2 - Uses Burrows–Wheeler algorithm | ||
public enum CompressionAlgorithmTags { | ||
UNCOMPRESSED = "0", | ||
ZIP = "1", | ||
ZLIB = "2", | ||
BZIP2= "3" | ||
} | ||
|
||
# Represent the symmetric key algorithms available in PGP. | ||
# | ||
# + NULL - No encryption | ||
# + IDEA - IDEA symmetric key algorithm | ||
# + TRIPLE_DES - Triple DES symmetric key algorithm | ||
# + CAST5 - CAST5 symmetric key algorithm | ||
# + BLOWFISH - Blowfish symmetric key algorithm | ||
# + SAFER - SAFER symmetric key algorithm | ||
# + DES - DES symmetric key algorithm | ||
# + AES_128 - AES 128-bit symmetric key algorithm | ||
# + AES_192 - AES 192-bit symmetric key algorithm | ||
# + AES_256 - AES 256-bit symmetric key algorithm | ||
# + TWOFISH - Twofish symmetric key algorithm | ||
# + CAMELLIA_128 - Camellia 128-bit symmetric key algorithm | ||
# + CAMELLIA_192 - Camellia 192-bit symmetric key algorithm | ||
# + CAMMELIA_256 - Camellia 256-bit symmetric key algorithm | ||
public enum SymmetricKeyAlgorithmTags { | ||
NULL = "0", | ||
IDEA = "1", | ||
TRIPLE_DES = "2", | ||
CAST5 = "3", | ||
BLOWFISH = "4", | ||
SAFER = "5", | ||
DES = "6", | ||
AES_128 = "7", | ||
AES_192 = "8", | ||
AES_256 = "9", | ||
TWOFISH = "10", | ||
CAMELLIA_128 = "11", | ||
CAMELLIA_192 = "12", | ||
CAMELLIA_256 = "13" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) 2024 WSO2 LLC. (https://www.wso2.com). | ||
// | ||
// 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 | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/test; | ||
|
||
@test:Config {} | ||
isolated function testEncryptAndDecryptWithPgp() returns error? { | ||
byte[] message = "Ballerina crypto test ".toBytes(); | ||
byte[] passphrase = "qCr3bv@5mj5n4eY".toBytes(); | ||
byte[] cipherText = check encryptPgp(message, PGP_PUBLIC_KEY_PATH); | ||
byte[] plainText = check decryptPgp(cipherText, PGP_PRIVATE_KEY_PATH, passphrase); | ||
test:assertEquals(plainText.toBase16(), message.toBase16()); | ||
} | ||
|
||
@test:Config {} | ||
isolated function testEncryptAndDecryptWithPgpWithOptions() returns error? { | ||
byte[] message = "Ballerina crypto test ".toBytes(); | ||
byte[] passphrase = "qCr3bv@5mj5n4eY".toBytes(); | ||
byte[] cipherText = check encryptPgp(message, PGP_PUBLIC_KEY_PATH, symmetricKeyAlgorithm = AES_128, armor = false); | ||
byte[] plainText = check decryptPgp(cipherText, PGP_PRIVATE_KEY_PATH, passphrase); | ||
test:assertEquals(plainText.toBase16(), message.toBase16()); | ||
} | ||
|
||
@test:Config {} | ||
isolated function testNegativeEncryptAndDecryptWithPgpInvalidPrivateKey() returns error? { | ||
byte[] message = "Ballerina crypto test ".toBytes(); | ||
byte[] passphrase = "p7S5@T2MRFD9TQb".toBytes(); | ||
byte[] cipherText = check encryptPgp(message, PGP_PUBLIC_KEY_PATH); | ||
byte[]|Error plainText = decryptPgp(cipherText, PGP_INVALID_PRIVATE_KEY_PATH, passphrase); | ||
if plainText is Error { | ||
test:assertEquals(plainText.message(), "Error occurred while PGP decrypt: Could Not Extract private key"); | ||
} else { | ||
test:assertFail("Should return a crypto Error"); | ||
} | ||
} | ||
|
||
@test:Config {} | ||
isolated function testNegativeEncryptAndDecryptWithPgpInvalidPassphrase() returns error? { | ||
byte[] message = "Ballerina crypto test ".toBytes(); | ||
byte[] passphrase = "p7S5@T2MRFD9TQb".toBytes(); | ||
byte[] cipherText = check encryptPgp(message, PGP_PUBLIC_KEY_PATH); | ||
byte[]|Error plainText = decryptPgp(cipherText, PGP_PRIVATE_KEY_PATH, passphrase); | ||
if plainText is Error { | ||
test:assertEquals(plainText.message(), | ||
"Error occurred while PGP decrypt: checksum mismatch at in checksum of 20 bytes"); | ||
} else { | ||
test:assertFail("Should return a crypto Error"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
-----BEGIN PGP PRIVATE KEY BLOCK----- | ||
Version: Keybase OpenPGP v2.0.76 | ||
Comment: https://keybase.io/crypto | ||
|
||
xcMGBGY8Rx0BCAC+lfjc0bvxHCaZY6txTJOvMygfDVYtOLx19KCP/+B5Qjl0AuJ9 | ||
Ky0JaJJdGpe4IZvKgB0Sr+elLBRRLIvapmuDD6feSbUHl+ckeaCY26j6qWmAXT0I | ||
9PI248rRCYzW3kyIa0c/d0pmwlVVICQ8DXxUaLBI9614q+v2lHRjKruGWAsdKIQ/ | ||
jssmTZI4b2pCqSlBe2PFtrKgLKNzSPXu8UFq7Ck8qoTkcaSBvKgDmf1su1PvEM+R | ||
iNHqUmKE/w6FOVfkRkSYWs9er5pO4k5k0/LtSa3K8Abgwg4WOX7PwzPrC+RvNg7X | ||
x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