Kotlin conversion: ChangeKeyPassword

This commit is contained in:
Paul Schaub 2023-10-31 14:00:49 +01:00
parent 4a123a1980
commit 34e1d8992f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 95 additions and 83 deletions

View file

@ -1,83 +0,0 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.operation;
import sop.Ready;
import sop.exception.SOPGPException;
import sop.util.UTF8Util;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.CharacterCodingException;
public interface ChangeKeyPassword {
/**
* Disable ASCII armoring of the output.
*
* @return builder instance
*/
ChangeKeyPassword noArmor();
default ChangeKeyPassword oldKeyPassphrase(byte[] password) {
try {
return oldKeyPassphrase(UTF8Util.decodeUTF8(password));
} catch (CharacterCodingException e) {
throw new SOPGPException.PasswordNotHumanReadable("Password MUST be a valid UTF8 string.");
}
}
/**
* Provide a passphrase to unlock the secret key.
* This method can be provided multiple times to provide separate passphrases that are tried as a
* means to unlock any secret key material encountered.
*
* @param oldPassphrase old passphrase
* @return builder instance
*/
ChangeKeyPassword oldKeyPassphrase(String oldPassphrase);
/**
* Provide a passphrase to re-lock the secret key with.
* This method can only be used once, and all key material encountered will be encrypted with the given passphrase.
* If this method is not called, the key material will not be protected.
*
* @param newPassphrase new passphrase
* @return builder instance
*/
default ChangeKeyPassword newKeyPassphrase(byte[] newPassphrase) {
try {
return newKeyPassphrase(UTF8Util.decodeUTF8(newPassphrase));
} catch (CharacterCodingException e) {
throw new SOPGPException.PasswordNotHumanReadable("Password MUST be a valid UTF8 string.");
}
}
/**
* Provide a passphrase to re-lock the secret key with.
* This method can only be used once, and all key material encountered will be encrypted with the given passphrase.
* If this method is not called, the key material will not be protected.
*
* @param newPassphrase new passphrase
* @return builder instance
*/
ChangeKeyPassword newKeyPassphrase(String newPassphrase);
default Ready keys(byte[] keys) throws SOPGPException.KeyIsProtected, SOPGPException.BadData {
return keys(new ByteArrayInputStream(keys));
}
/**
* Provide the key material.
*
* @param inputStream input stream of secret key material
* @return ready
*
* @throws sop.exception.SOPGPException.KeyIsProtected if any (sub-) key encountered cannot be unlocked.
* @throws sop.exception.SOPGPException.BadData if the key material is malformed
*/
Ready keys(InputStream inputStream) throws SOPGPException.KeyIsProtected, SOPGPException.BadData;
}

View file

@ -0,0 +1,95 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.operation
import java.io.InputStream
import sop.Ready
import sop.exception.SOPGPException.BadData
import sop.exception.SOPGPException.KeyIsProtected
import sop.exception.SOPGPException.PasswordNotHumanReadable
import sop.util.UTF8Util
interface ChangeKeyPassword {
/**
* Disable ASCII armoring of the output.
*
* @return builder instance
*/
fun noArmor(): ChangeKeyPassword
/**
* Provide a passphrase to unlock the secret key. This method can be provided multiple times to
* provide separate passphrases that are tried as a means to unlock any secret key material
* encountered.
*
* @param oldPassphrase old passphrase
* @return builder instance
*/
@Throws(PasswordNotHumanReadable::class)
fun oldKeyPassphrase(oldPassphrase: ByteArray): ChangeKeyPassword =
try {
oldKeyPassphrase(UTF8Util.decodeUTF8(oldPassphrase))
} catch (e: CharacterCodingException) {
throw PasswordNotHumanReadable("Password MUST be a valid UTF8 string.")
}
/**
* Provide a passphrase to unlock the secret key. This method can be provided multiple times to
* provide separate passphrases that are tried as a means to unlock any secret key material
* encountered.
*
* @param oldPassphrase old passphrase
* @return builder instance
*/
fun oldKeyPassphrase(oldPassphrase: String): ChangeKeyPassword
/**
* Provide a passphrase to re-lock the secret key with. This method can only be used once, and
* all key material encountered will be encrypted with the given passphrase. If this method is
* not called, the key material will not be protected.
*
* @param newPassphrase new passphrase
* @return builder instance
*/
@Throws(PasswordNotHumanReadable::class)
fun newKeyPassphrase(newPassphrase: ByteArray): ChangeKeyPassword =
try {
newKeyPassphrase(UTF8Util.decodeUTF8(newPassphrase))
} catch (e: CharacterCodingException) {
throw PasswordNotHumanReadable("Password MUST be a valid UTF8 string.")
}
/**
* Provide a passphrase to re-lock the secret key with. This method can only be used once, and
* all key material encountered will be encrypted with the given passphrase. If this method is
* not called, the key material will not be protected.
*
* @param newPassphrase new passphrase
* @return builder instance
*/
fun newKeyPassphrase(newPassphrase: String): ChangeKeyPassword
/**
* Provide the key material.
*
* @param keys input stream of secret key material
* @return ready
* @throws KeyIsProtected if any (sub-) key encountered cannot be unlocked.
* @throws BadData if the key material is malformed
*/
@Throws(KeyIsProtected::class, BadData::class)
fun keys(keys: ByteArray): Ready = keys(keys.inputStream())
/**
* Provide the key material.
*
* @param keys input stream of secret key material
* @return ready
* @throws KeyIsProtected if any (sub-) key encountered cannot be unlocked.
* @throws BadData if the key material is malformed
*/
@Throws(KeyIsProtected::class, BadData::class) fun keys(keys: InputStream): Ready
}