mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-17 20:50:44 +00:00
Add method pemStringToDecodedBytes
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user