mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 12:57:57 +01:00
Kotlin conversion: ChangeKeyPasswordExternal
This commit is contained in:
parent
6771952618
commit
d149aac56c
2 changed files with 37 additions and 62 deletions
|
@ -1,62 +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.exception.SOPGPException;
|
|
||||||
import sop.external.ExternalSOP;
|
|
||||||
import sop.operation.ChangeKeyPassword;
|
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Properties;
|
|
||||||
|
|
||||||
public class ChangeKeyPasswordExternal implements ChangeKeyPassword {
|
|
||||||
private final List<String> commandList = new ArrayList<>();
|
|
||||||
private final List<String> envList;
|
|
||||||
|
|
||||||
private int keyPasswordCounter = 0;
|
|
||||||
|
|
||||||
public ChangeKeyPasswordExternal(String binary, Properties environment) {
|
|
||||||
this.commandList.add(binary);
|
|
||||||
this.commandList.add("decrypt");
|
|
||||||
this.envList = ExternalSOP.propertiesToEnv(environment);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nonnull
|
|
||||||
public ChangeKeyPassword noArmor() {
|
|
||||||
this.commandList.add("--no-armor");
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nonnull
|
|
||||||
public ChangeKeyPassword oldKeyPassphrase(@Nonnull String oldPassphrase) {
|
|
||||||
this.commandList.add("--old-key-password=@ENV:KEY_PASSWORD_" + keyPasswordCounter);
|
|
||||||
this.envList.add("KEY_PASSWORD_" + keyPasswordCounter + "=" + oldPassphrase);
|
|
||||||
keyPasswordCounter++;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nonnull
|
|
||||||
public ChangeKeyPassword newKeyPassphrase(@Nonnull String newPassphrase) {
|
|
||||||
this.commandList.add("--new-key-password=@ENV:KEY_PASSWORD_" + keyPasswordCounter);
|
|
||||||
this.envList.add("KEY_PASSWORD_" + keyPasswordCounter + "=" + newPassphrase);
|
|
||||||
keyPasswordCounter++;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
@Nonnull
|
|
||||||
public Ready keys(@Nonnull InputStream inputStream) throws SOPGPException.KeyIsProtected, SOPGPException.BadData {
|
|
||||||
return ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, inputStream);
|
|
||||||
}
|
|
||||||
}
|
|
37
external-sop/src/main/kotlin/sop/external/operation/ChangeKeyPasswordExternal.kt
vendored
Normal file
37
external-sop/src/main/kotlin/sop/external/operation/ChangeKeyPasswordExternal.kt
vendored
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// 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.external.ExternalSOP
|
||||||
|
import sop.operation.ChangeKeyPassword
|
||||||
|
|
||||||
|
/** Implementation of the [ChangeKeyPassword] operation using an external SOP binary. */
|
||||||
|
class ChangeKeyPasswordExternal(binary: String, environment: Properties) : ChangeKeyPassword {
|
||||||
|
|
||||||
|
private val commandList: MutableList<String> = mutableListOf(binary, "change-key-password")
|
||||||
|
private val envList = ExternalSOP.propertiesToEnv(environment).toMutableList()
|
||||||
|
|
||||||
|
private var keyPasswordCounter = 0
|
||||||
|
|
||||||
|
override fun noArmor(): ChangeKeyPassword = apply { commandList.add("--no-armor") }
|
||||||
|
|
||||||
|
override fun oldKeyPassphrase(oldPassphrase: String): ChangeKeyPassword = apply {
|
||||||
|
commandList.add("--old-key-password=@ENV:KEY_PASSWORD_$keyPasswordCounter")
|
||||||
|
envList.add("KEY_PASSWORD_$keyPasswordCounter=$oldPassphrase")
|
||||||
|
keyPasswordCounter += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun newKeyPassphrase(newPassphrase: String): ChangeKeyPassword = apply {
|
||||||
|
commandList.add("--new-key-password=@ENV:KEY_PASSWORD_$keyPasswordCounter")
|
||||||
|
envList.add("KEY_PASSWORD_$keyPasswordCounter=$newPassphrase")
|
||||||
|
keyPasswordCounter += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun keys(keys: InputStream): Ready =
|
||||||
|
ExternalSOP.executeTransformingOperation(Runtime.getRuntime(), commandList, envList, keys)
|
||||||
|
}
|
Loading…
Reference in a new issue