Kotlin conversion: MicAlg

This commit is contained in:
Paul Schaub 2023-10-31 13:05:30 +01:00
parent bbe159e88c
commit 9dbb93e13d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 35 additions and 56 deletions

View file

@ -1,55 +0,0 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop;
import java.io.OutputStream;
import java.io.PrintWriter;
public class MicAlg {
private final String micAlg;
public MicAlg(String micAlg) {
if (micAlg == null) {
throw new IllegalArgumentException("MicAlg String cannot be null.");
}
this.micAlg = micAlg;
}
public static MicAlg empty() {
return new MicAlg("");
}
public static MicAlg fromHashAlgorithmId(int id) {
switch (id) {
case 1:
return new MicAlg("pgp-md5");
case 2:
return new MicAlg("pgp-sha1");
case 3:
return new MicAlg("pgp-ripemd160");
case 8:
return new MicAlg("pgp-sha256");
case 9:
return new MicAlg("pgp-sha384");
case 10:
return new MicAlg("pgp-sha512");
case 11:
return new MicAlg("pgp-sha224");
default:
throw new IllegalArgumentException("Unsupported hash algorithm ID: " + id);
}
}
public String getMicAlg() {
return micAlg;
}
public void writeTo(OutputStream outputStream) {
PrintWriter pw = new PrintWriter(outputStream);
pw.write(getMicAlg());
pw.close();
}
}

View file

@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop
import java.io.OutputStream
import java.io.PrintWriter
data class MicAlg(val micAlg: String) {
fun writeTo(outputStream: OutputStream) {
PrintWriter(outputStream).use { it.write(micAlg) }
}
companion object {
@JvmStatic fun empty() = MicAlg("")
@JvmStatic
fun fromHashAlgorithmId(id: Int) =
when (id) {
1 -> "pgp-md5"
2 -> "pgp-sha1"
3 -> "pgp-ripemd160"
8 -> "pgp-sha256"
9 -> "pgp-sha384"
10 -> "pgp-sha512"
11 -> "pgp-sha224"
12 -> "pgp-sha3-256"
14 -> "pgp-sha3-512"
else -> throw IllegalArgumentException("Unsupported hash algorithm ID: $id")
}.let { MicAlg(it) }
}
}

View file

@ -18,7 +18,7 @@ public class MicAlgTest {
@Test @Test
public void constructorNullArgThrows() { public void constructorNullArgThrows() {
assertThrows(IllegalArgumentException.class, () -> new MicAlg(null)); assertThrows(NullPointerException.class, () -> new MicAlg(null));
} }
@Test @Test