mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-15 13:19:45 +02:00
Rename module to external-sop and make backend in tests configurable
This commit is contained in:
parent
28912618ea
commit
e602cc16cc
12 changed files with 80 additions and 30 deletions
89
external-sop/src/main/java/sop/external/operation/GenerateKeyExternal.java
vendored
Normal file
89
external-sop/src/main/java/sop/external/operation/GenerateKeyExternal.java
vendored
Normal file
|
@ -0,0 +1,89 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation;
|
||||
|
||||
import sop.Ready;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.GenerateKey;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenerateKeyExternal implements GenerateKey {
|
||||
|
||||
private final String binary;
|
||||
private boolean noArmor = false;
|
||||
private List<String> userIds = new ArrayList<>();
|
||||
private String keyPassword;
|
||||
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
public GenerateKeyExternal(String binary) {
|
||||
this.binary = binary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey noArmor() {
|
||||
this.noArmor = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey userId(String userId) {
|
||||
this.userIds.add(userId);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey withKeyPassword(String password)
|
||||
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
this.keyPassword = password;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ready generate()
|
||||
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
|
||||
commandList.add(binary);
|
||||
commandList.add("generate-key");
|
||||
|
||||
if (noArmor) {
|
||||
commandList.add("--no-armor");
|
||||
}
|
||||
|
||||
if (keyPassword != null) {
|
||||
commandList.add("--with-key-password");
|
||||
commandList.add(keyPassword);
|
||||
}
|
||||
|
||||
for (String userId : userIds) {
|
||||
commandList.add(userId);
|
||||
}
|
||||
|
||||
String[] command = commandList.toArray(new String[0]);
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
InputStream stdIn = process.getInputStream();
|
||||
|
||||
return new Ready() {
|
||||
@Override
|
||||
public void writeTo(OutputStream outputStream) throws IOException {
|
||||
byte[] buf = new byte[4096];
|
||||
int r;
|
||||
while ((r = stdIn.read(buf)) >= 0) {
|
||||
outputStream.write(buf, 0, r);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue