From 020ce44259c3fc747288ce1a0c465bb24ad9210a Mon Sep 17 00:00:00 2001 From: Simya Jose Date: Tue, 23 Aug 2022 16:38:41 +0100 Subject: [PATCH] ISSUE-4-add-original-file-name-to-encrypted-message (#1) By default, name parameter in the PGP Encrypted message is set and coming as '_CONSUL' Include original filename(from which the message originates from) as an additional argument to set 'name' paramter --- src/main/kotlin/KotlinPGP.kt | 3 ++- src/main/kotlin/data/EncryptParameter.kt | 5 +++-- src/test/kotlin/KotlinPGPTest.kt | 12 +++++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/KotlinPGP.kt b/src/main/kotlin/KotlinPGP.kt index 90099bc..d75906e 100644 --- a/src/main/kotlin/KotlinPGP.kt +++ b/src/main/kotlin/KotlinPGP.kt @@ -365,6 +365,7 @@ object KotlinPGP { armoredOutputStream.setHeader(head.key, head.value) } val messageBytes = encryptParameter.message.toByteArray() + val messageName = encryptParameter.messageOriginatingFileName.ifEmpty { PGPLiteralData.CONSOLE } val signatureGenerator: PGPSignatureGenerator? var signatureHashAlgorithm = 0 if (encryptParameter.enableSignature) { @@ -428,7 +429,7 @@ object KotlinPGP { val literalDataGeneratorOutput = literalDataGenerator.open( bcpgOutputStream, PGPLiteralData.UTF8, - PGPLiteralData.CONSOLE, + messageName, Date(), ByteArray(1 shl 16) ) diff --git a/src/main/kotlin/data/EncryptParameter.kt b/src/main/kotlin/data/EncryptParameter.kt index 107ae15..9bb097c 100644 --- a/src/main/kotlin/data/EncryptParameter.kt +++ b/src/main/kotlin/data/EncryptParameter.kt @@ -8,7 +8,8 @@ data class EncryptParameter( val enableSignature: Boolean = false, val privateKey: String = "", val password: String = "", - val compressionAlgorithm: Int = CompressionAlgorithmTags.ZIP + val compressionAlgorithm: Int = CompressionAlgorithmTags.ZIP, + val messageOriginatingFileName: String = "" ) data class PublicKeyData( @@ -19,4 +20,4 @@ data class PublicKeyData( data class PrivateKeyData( val key: String, val password: String -) \ No newline at end of file +) diff --git a/src/test/kotlin/KotlinPGPTest.kt b/src/test/kotlin/KotlinPGPTest.kt index 71c6c19..f3508e0 100644 --- a/src/test/kotlin/KotlinPGPTest.kt +++ b/src/test/kotlin/KotlinPGPTest.kt @@ -190,6 +190,16 @@ class KotlinPGPTest : FreeSpec({ it.isPGPMessage } } + "encrypt data with originating filename" { + val encryptResult = KotlinPGP.encrypt(EncryptParameter( + message = contentToEncrypt, + publicKey = publicKeys, + messageOriginatingFileName = "test" + )) + encryptResult should { + it.isPGPMessage + } + } "decrypt" - { val encryptedData = KotlinPGP.encrypt(EncryptParameter( message = contentToEncrypt, @@ -315,4 +325,4 @@ private fun testKeyPair(keypair: PGPKeyPairData) { keypair.secretKey should { it.isPGPPrivateKey } -} \ No newline at end of file +}