mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-05 12:05:58 +01:00
Kotlin conversion: SubkeyIdentifier
This commit is contained in:
parent
85bf15d8bd
commit
6ca44bddc1
2 changed files with 53 additions and 146 deletions
|
@ -1,146 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
|
||||
/**
|
||||
* Tuple class used to identify a subkey by fingerprints of the primary key of the subkeys key ring,
|
||||
* as well as the subkeys fingerprint.
|
||||
*/
|
||||
public class SubkeyIdentifier {
|
||||
|
||||
private final OpenPgpFingerprint primaryKeyFingerprint;
|
||||
private final OpenPgpFingerprint subkeyFingerprint;
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} from a {@link PGPKeyRing}.
|
||||
* The identifier will point to the primary key of the provided ring.
|
||||
*
|
||||
* @param keyRing key ring
|
||||
*/
|
||||
public SubkeyIdentifier(PGPKeyRing keyRing) {
|
||||
this(keyRing, keyRing.getPublicKey().getKeyID());
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} from a {@link PGPKeyRing} and the subkeys key id.
|
||||
* {@link #getPrimaryKeyFingerprint()} will return the {@link OpenPgpFingerprint} of the keyrings primary key,
|
||||
* while {@link #getSubkeyFingerprint()} will return the subkeys fingerprint.
|
||||
*
|
||||
* @param keyRing keyring the subkey belongs to
|
||||
* @param keyId keyid of the subkey
|
||||
*/
|
||||
public SubkeyIdentifier(@Nonnull PGPKeyRing keyRing, long keyId) {
|
||||
PGPPublicKey subkey = keyRing.getPublicKey(keyId);
|
||||
if (subkey == null) {
|
||||
throw new NoSuchElementException("Key ring does not contain subkey with id " + Long.toHexString(keyId));
|
||||
}
|
||||
this.primaryKeyFingerprint = OpenPgpFingerprint.of(keyRing);
|
||||
this.subkeyFingerprint = OpenPgpFingerprint.of(subkey);
|
||||
}
|
||||
|
||||
public SubkeyIdentifier(@Nonnull PGPKeyRing keyRing, @Nonnull OpenPgpFingerprint subkeyFingerprint) {
|
||||
this(OpenPgpFingerprint.of(keyRing), subkeyFingerprint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} that identifies the primary key with the given fingerprint.
|
||||
* This means, both {@link #getPrimaryKeyFingerprint()} and {@link #getSubkeyFingerprint()} return the same.
|
||||
*
|
||||
* @param primaryKeyFingerprint fingerprint of the identified key
|
||||
*/
|
||||
public SubkeyIdentifier(@Nonnull OpenPgpFingerprint primaryKeyFingerprint) {
|
||||
this(primaryKeyFingerprint, primaryKeyFingerprint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link SubkeyIdentifier} which points to the subkey with the given subkeyFingerprint,
|
||||
* which has a primary key with the given primaryKeyFingerprint.
|
||||
*
|
||||
* @param primaryKeyFingerprint fingerprint of the primary key
|
||||
* @param subkeyFingerprint fingerprint of the subkey
|
||||
*/
|
||||
public SubkeyIdentifier(@Nonnull OpenPgpFingerprint primaryKeyFingerprint, @Nonnull OpenPgpFingerprint subkeyFingerprint) {
|
||||
this.primaryKeyFingerprint = primaryKeyFingerprint;
|
||||
this.subkeyFingerprint = subkeyFingerprint;
|
||||
}
|
||||
|
||||
public @Nonnull OpenPgpFingerprint getFingerprint() {
|
||||
return getSubkeyFingerprint();
|
||||
}
|
||||
|
||||
public long getKeyId() {
|
||||
return getSubkeyId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link OpenPgpFingerprint} of the primary key of the identified key.
|
||||
* This might be the same as {@link #getSubkeyFingerprint()} if the identified subkey is the primary key.
|
||||
*
|
||||
* @return primary key fingerprint
|
||||
*/
|
||||
public @Nonnull OpenPgpFingerprint getPrimaryKeyFingerprint() {
|
||||
return primaryKeyFingerprint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key id of the primary key of the identified key.
|
||||
* This might be the same as {@link #getSubkeyId()} if the identified subkey is the primary key.
|
||||
*
|
||||
* @return primary key id
|
||||
*/
|
||||
public long getPrimaryKeyId() {
|
||||
return getPrimaryKeyFingerprint().getKeyId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link OpenPgpFingerprint} of the identified subkey.
|
||||
*
|
||||
* @return subkey fingerprint
|
||||
*/
|
||||
public @Nonnull OpenPgpFingerprint getSubkeyFingerprint() {
|
||||
return subkeyFingerprint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key id of the identified subkey.
|
||||
*
|
||||
* @return subkey id
|
||||
*/
|
||||
public long getSubkeyId() {
|
||||
return getSubkeyFingerprint().getKeyId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return primaryKeyFingerprint.hashCode() * 31 + subkeyFingerprint.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (!(obj instanceof SubkeyIdentifier)) {
|
||||
return false;
|
||||
}
|
||||
SubkeyIdentifier other = (SubkeyIdentifier) obj;
|
||||
return getPrimaryKeyFingerprint().equals(other.getPrimaryKeyFingerprint())
|
||||
&& getSubkeyFingerprint().equals(other.getSubkeyFingerprint());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getSubkeyFingerprint() + " " + getPrimaryKeyFingerprint();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.key
|
||||
|
||||
import org.bouncycastle.openpgp.PGPKeyRing
|
||||
import org.bouncycastle.openpgp.PGPPublicKey
|
||||
import org.pgpainless.key.util.KeyIdUtil
|
||||
|
||||
/**
|
||||
* Tuple class used to identify a subkey by fingerprints of the primary key of the subkeys key ring,
|
||||
* as well as the subkeys fingerprint.
|
||||
*/
|
||||
class SubkeyIdentifier(
|
||||
val primaryKeyFingerprint: OpenPgpFingerprint,
|
||||
val subkeyFingerprint: OpenPgpFingerprint) {
|
||||
|
||||
constructor(fingerprint: OpenPgpFingerprint): this(fingerprint, fingerprint)
|
||||
constructor(keys: PGPKeyRing): this(keys.publicKey)
|
||||
constructor(key: PGPPublicKey): this(OpenPgpFingerprint.of(key))
|
||||
constructor(keys: PGPKeyRing, keyId: Long): this(
|
||||
OpenPgpFingerprint.of(keys.publicKey),
|
||||
OpenPgpFingerprint.of(keys.getPublicKey(keyId) ?:
|
||||
throw NoSuchElementException("OpenPGP key does not contain subkey ${KeyIdUtil.formatKeyId(keyId)}")))
|
||||
constructor(keys: PGPKeyRing, subkeyFingerprint: OpenPgpFingerprint): this(OpenPgpFingerprint.of(keys), subkeyFingerprint)
|
||||
|
||||
val keyId = subkeyFingerprint.keyId
|
||||
val fingerprint = subkeyFingerprint
|
||||
|
||||
val subkeyId = subkeyFingerprint.keyId
|
||||
val primaryKeyId = primaryKeyFingerprint.keyId
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other == null) {
|
||||
return false
|
||||
}
|
||||
if (this === other) {
|
||||
return true
|
||||
}
|
||||
if (other !is SubkeyIdentifier) {
|
||||
return false
|
||||
}
|
||||
|
||||
return primaryKeyFingerprint.equals(other.primaryKeyFingerprint) && subkeyFingerprint.equals(other.subkeyFingerprint)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return primaryKeyFingerprint.hashCode() + 31 * subkeyFingerprint.hashCode()
|
||||
}
|
||||
|
||||
override fun toString(): String = "$subkeyFingerprint $primaryKeyFingerprint"
|
||||
}
|
Loading…
Reference in a new issue