Skip to content

Commit

Permalink
NIFI-12265 Fixed OpenPGP Hexadecimal Key Formatting with leading 0
Browse files Browse the repository at this point in the history
- Replaced Long.toHexString() with String.format() using pattern with leading 0

This closes apache#7926

Signed-off-by: David Handermann <exceptionfactory@apache.org>
(cherry picked from commit 8e301cf)
  • Loading branch information
nathluu authored and exceptionfactory committed Oct 28, 2023
1 parent 0bdc392 commit ead1c23
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import org.apache.nifi.processors.pgp.attributes.DecryptionStrategy;
import org.apache.nifi.processors.pgp.exception.PGPDecryptionException;
import org.apache.nifi.processors.pgp.exception.PGPProcessException;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.stream.io.StreamUtils;

import org.apache.nifi.util.StringUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.apache.nifi.processors.pgp.attributes.SigningStrategy;
import org.apache.nifi.processors.pgp.exception.PGPProcessException;
import org.apache.nifi.processors.pgp.io.EncodingStreamCallback;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
import org.bouncycastle.openpgp.PGPOnePassSignature;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.nifi.processor.Relationship;
import org.apache.nifi.processor.io.StreamCallback;
import org.apache.nifi.processors.pgp.exception.PGPProcessException;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.stream.io.StreamUtils;
import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.nifi.processors.pgp.attributes.DecryptionStrategy;
import org.apache.nifi.processors.pgp.attributes.FileEncoding;
import org.apache.nifi.processors.pgp.attributes.SymmetricKeyAlgorithm;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.util.MockFlowFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.nifi.processors.pgp.attributes.FileEncoding;
import org.apache.nifi.processors.pgp.attributes.HashAlgorithm;
import org.apache.nifi.processors.pgp.attributes.SigningStrategy;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.LogMessage;
import org.apache.nifi.util.MockFlowFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.nifi.pgp.util.PGPFileUtils;
import org.apache.nifi.pgp.util.PGPSecretKeyGenerator;
import org.apache.nifi.pgp.util.PGPOperationUtils;
import org.apache.nifi.processors.pgp.io.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.reporting.InitializationException;
import org.apache.nifi.util.LogMessage;
import org.apache.nifi.util.MockFlowFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.processors.pgp.io;
package org.apache.nifi.pgp.service.api;

import java.math.BigInteger;

Expand All @@ -24,14 +24,16 @@
public class KeyIdentifierConverter {
private static final int HEXADECIMAL_RADIX = 16;

private static final String KEY_ID_FORMAT = "%016X";

/**
* Format numeric key identifier as uppercase hexadecimal string
*
* @param keyId Key Identifier
* @return Uppercase hexadecimal string
*/
public static String format(final long keyId) {
return Long.toHexString(keyId).toUpperCase();
return String.format(KEY_ID_FORMAT, keyId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.processors.pgp.io;
package org.apache.nifi.pgp.service.api;

import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ConfigurationContext;
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.PGPPrivateKeyService;
import org.apache.nifi.pgp.service.standard.exception.PGPConfigurationException;
import org.apache.nifi.processor.util.StandardValidators;
Expand Down Expand Up @@ -142,7 +143,7 @@ public void onDisabled() {
*/
@Override
public Optional<PGPPrivateKey> findPrivateKey(final long keyIdentifier) {
getLogger().debug("Find Private Key [{}]", Long.toHexString(keyIdentifier).toUpperCase());
getLogger().debug("Find Private Key [{}]", KeyIdentifierConverter.format(keyIdentifier));
return Optional.ofNullable(privateKeys.get(keyIdentifier));
}

Expand Down Expand Up @@ -256,7 +257,7 @@ private List<PGPPrivateKey> extractPrivateKeys(final PGPSecretKeyRingCollection
for (final PGPSecretKeyRing keyRing : keyRings) {
for (final PGPSecretKey secretKey : keyRing) {
final long keyId = secretKey.getKeyID();
final String keyIdentifier = Long.toHexString(keyId).toUpperCase();
final String keyIdentifier = KeyIdentifierConverter.format(keyId);
try {
final PGPPrivateKey privateKey = secretKey.extractPrivateKey(keyDecryptor);
extractedPrivateKeys.add(privateKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.nifi.controller.AbstractControllerService;
import org.apache.nifi.controller.ConfigurationContext;
import org.apache.nifi.expression.ExpressionLanguageScope;
import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.api.PGPPublicKeyService;
import org.apache.nifi.pgp.service.standard.exception.PGPConfigurationException;
import org.apache.nifi.processor.util.StandardValidators;
Expand Down Expand Up @@ -186,7 +187,7 @@ protected Collection<ValidationResult> customValidate(final ValidationContext co

private boolean isPublicKeyMatched(final PGPPublicKey publicKey, final String search) {
boolean matched = false;
final String keyId = Long.toHexString(publicKey.getKeyID()).toUpperCase();
final String keyId = KeyIdentifierConverter.format(publicKey.getKeyID());
if (keyId.equals(search)) {
matched = true;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apche.nifi.pgp.service.standard;

import org.apache.nifi.pgp.service.api.KeyIdentifierConverter;
import org.apache.nifi.pgp.service.standard.StandardPGPPublicKeyService;
import org.apache.nifi.pgp.util.PGPFileUtils;
import org.apache.nifi.pgp.util.PGPSecretKeyGenerator;
Expand Down Expand Up @@ -129,7 +130,7 @@ public void testFindPublicKeyBinaryKeyringFileAndAsciiKeyring() throws Exception

private void assertPublicKeyFound(final PGPSecretKey secretKey) {
final long keyIdentifier = secretKey.getKeyID();
final String publicKeySearch = Long.toHexString(keyIdentifier).toUpperCase();
final String publicKeySearch = KeyIdentifierConverter.format(keyIdentifier);
final Optional<PGPPublicKey> optionalPublicKey = service.findPublicKey(publicKeySearch);
assertTrue(optionalPublicKey.isPresent());
final PGPPublicKey publicKey = optionalPublicKey.get();
Expand Down

0 comments on commit ead1c23

Please sign in to comment.