Add method pemStringToDecodedBytes

This commit is contained in:
2025-02-25 08:45:48 -06:00
parent cdc6df2140
commit eae24e3eba
2 changed files with 18 additions and 7 deletions

View File

@ -33,6 +33,7 @@ import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@ -409,4 +410,19 @@ public class AbstractSFTPAction extends AbstractBaseFilesystemAction<SFTPDirEntr
return (sftpClient);
}
/***************************************************************************
** take a string, which is the contents of a PEM file (like a private key)
** - and if it has the -----BEGIN...----- and -----END...---- lines, strip
** them away, and strip away any whitespace, and then base-64 decode it.
***************************************************************************/
public static byte[] pemStringToDecodedBytes(String pemString)
{
String base64 = pemString.replaceAll("-----BEGIN (.*?)-----", "")
.replaceAll("-----END (.*?)-----", "")
.replaceAll("\\s", "");
return Base64.getDecoder().decode(base64);
}
}

View File

@ -24,8 +24,7 @@ package com.kingsrook.qqq.backend.module.filesystem.sftp.actions;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.stream.Collectors;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qqq.backend.module.filesystem.sftp.BaseSFTPTest;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
@ -191,11 +190,7 @@ class SFTPTestConnectionActionTest extends BaseSFTPTest
{
try(InputStream resourceAsStream = getClass().getResourceAsStream("/test-only-key"))
{
String pem = IOUtils.readLines(resourceAsStream, StandardCharsets.UTF_8).stream()
.filter(s -> !s.startsWith("----"))
.collect(Collectors.joining(""));
byte[] privateKeyBytes = Base64.getDecoder().decode(pem);
byte[] privateKeyBytes = AbstractSFTPAction.pemStringToDecodedBytes(StringUtils.join("", IOUtils.readLines(resourceAsStream, StandardCharsets.UTF_8)));
SFTPTestConnectionAction.SFTPTestConnectionTestInput input = new SFTPTestConnectionAction.SFTPTestConnectionTestInput()
.withUsername(BaseSFTPTest.USERNAME)