mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 04:57:56 +01:00
Kotlin conversion: InlineSignExternal
This commit is contained in:
parent
f181453004
commit
7be71494cf
2 changed files with 40 additions and 74 deletions
|
@ -1,74 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation;
|
||||
|
||||
import sop.Ready;
|
||||
import sop.enums.InlineSignAs;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.external.ExternalSOP;
|
||||
import sop.operation.InlineSign;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link InlineSign} operation using an external SOP binary.
|
||||
*/
|
||||
public class InlineSignExternal implements InlineSign {
|
||||
|
||||
private final List<String> commandList = new ArrayList<>();
|
||||
private final List<String> envList;
|
||||
|
||||
private int keyCounter = 0;
|
||||
private int withKeyPasswordCounter = 0;
|
||||
|
||||
public InlineSignExternal(String binary, Properties environment) {
|
||||
commandList.add(binary);
|
||||
commandList.add("inline-sign");
|
||||
envList = ExternalSOP.propertiesToEnv(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineSign noArmor() {
|
||||
commandList.add("--no-armor");
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineSign key(@Nonnull InputStream key) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
|
||||
String envVar = "KEY_" + keyCounter++;
|
||||
commandList.add("@ENV:" + envVar);
|
||||
envList.add(envVar + "=" + ExternalSOP.readString(key));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineSign withKeyPassword(@Nonnull byte[] password) throws SOPGPException.UnsupportedOption, SOPGPException.PasswordNotHumanReadable {
|
||||
String envVar = "WITH_KEY_PASSWORD_" + withKeyPasswordCounter++;
|
||||
commandList.add("--with-key-password=@ENV:" + envVar);
|
||||
envList.add(envVar + "=" + new String(password));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineSign mode(@Nonnull InlineSignAs mode) throws SOPGPException.UnsupportedOption {
|
||||
commandList.add("--as=" + mode);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public Ready data(@Nonnull InputStream data) throws SOPGPException.KeyIsProtected, SOPGPException.ExpectedText {
|
||||
return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, data);
|
||||
}
|
||||
}
|
40
external-sop/src/main/kotlin/sop/external/operation/InlineSignExternal.kt
vendored
Normal file
40
external-sop/src/main/kotlin/sop/external/operation/InlineSignExternal.kt
vendored
Normal file
|
@ -0,0 +1,40 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation
|
||||
|
||||
import java.io.InputStream
|
||||
import java.util.Properties
|
||||
import sop.Ready
|
||||
import sop.enums.InlineSignAs
|
||||
import sop.external.ExternalSOP
|
||||
import sop.operation.InlineSign
|
||||
|
||||
/** Implementation of the [InlineSign] operation using an external SOP binary. */
|
||||
class InlineSignExternal(binary: String, environment: Properties) : InlineSign {
|
||||
|
||||
private val commandList = mutableListOf(binary, "inline-sign")
|
||||
private val envList = ExternalSOP.propertiesToEnv(environment).toMutableList()
|
||||
|
||||
private var argCounter = 0
|
||||
|
||||
override fun mode(mode: InlineSignAs): InlineSign = apply { commandList.add("--as=$mode") }
|
||||
|
||||
override fun data(data: InputStream): Ready =
|
||||
ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, data)
|
||||
|
||||
override fun noArmor(): InlineSign = apply { commandList.add("--no-armor") }
|
||||
|
||||
override fun key(key: InputStream): InlineSign = apply {
|
||||
commandList.add("@ENV:KEY_$argCounter")
|
||||
envList.add("KEY_$argCounter=${ExternalSOP.readString(key)}")
|
||||
argCounter += 1
|
||||
}
|
||||
|
||||
override fun withKeyPassword(password: ByteArray): InlineSign = apply {
|
||||
commandList.add("--with-key-password=@ENV:WITH_KEY_PASSWORD_$argCounter")
|
||||
envList.add("WITH_KEY_PASSWORD_$argCounter=${String(password)}")
|
||||
argCounter += 1
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue