1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

OpenPGPFingerprint(s): Use FingerprintUtil to calculate key-ids

This commit is contained in:
Paul Schaub 2025-02-18 15:14:48 +01:00
parent 6e83bfefd4
commit 5265dcdfeb
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 7 additions and 31 deletions

View file

@ -5,14 +5,11 @@
package org.pgpainless.key
import java.net.URI
import java.nio.Buffer
import java.nio.ByteBuffer
import java.nio.charset.Charset
import org.bouncycastle.bcpg.FingerprintUtil
import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSecretKey
import org.bouncycastle.util.encoders.Hex
class OpenPgpV4Fingerprint : OpenPgpFingerprint {
@ -28,17 +25,7 @@ class OpenPgpV4Fingerprint : OpenPgpFingerprint {
override fun getVersion() = 4
override val keyId: Long
get() {
val bytes = Hex.decode(toString().toByteArray(Charset.forName("UTF-8")))
val buf = ByteBuffer.wrap(bytes)
// The key id is the right-most 8 bytes (conveniently a long)
// We have to cast here in order to be compatible with java 8
// https://github.com/eclipse/jetty.project/issues/3244
(buf as Buffer).position(12) // 20 - 8 bytes = offset 12
return buf.getLong()
}
override val keyId: Long = FingerprintUtil.keyIdFromV4Fingerprint(bytes)
override val keyIdentifier: KeyIdentifier = KeyIdentifier(bytes)

View file

@ -4,6 +4,7 @@
package org.pgpainless.key
import org.bouncycastle.bcpg.FingerprintUtil
import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey
@ -22,6 +23,8 @@ class OpenPgpV5Fingerprint : _64DigitFingerprint {
constructor(bytes: ByteArray) : super(bytes)
override val keyId: Long = FingerprintUtil.keyIdFromLibrePgpFingerprint(bytes)
override fun getVersion(): Int {
return 5
}

View file

@ -4,14 +4,11 @@
package org.pgpainless.key
import java.nio.Buffer
import java.nio.ByteBuffer
import java.nio.charset.Charset
import org.bouncycastle.bcpg.FingerprintUtil
import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSecretKey
import org.bouncycastle.util.encoders.Hex
/**
* This class represents a hex encoded, upper case OpenPGP v5 or v6 fingerprint. Since both
@ -35,18 +32,7 @@ open class _64DigitFingerprint : OpenPgpFingerprint {
constructor(keys: PGPKeyRing) : super(keys)
override val keyId: Long
get() {
val bytes = Hex.decode(fingerprint.toByteArray(Charset.forName("UTF-8")))
val buf = ByteBuffer.wrap(bytes)
// The key id is the left-most 8 bytes (conveniently a long).
// We have to cast here in order to be compatible with java 8
// https://github.com/eclipse/jetty.project/issues/3244
(buf as Buffer).position(0)
return buf.getLong()
}
override val keyId: Long = FingerprintUtil.keyIdFromV6Fingerprint(bytes)
override fun getVersion(): Int {
return -1 // might be v5 or v6