1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-25 12:27:58 +01:00

Improve readability of primaryUserId adjustment

This commit is contained in:
Paul Schaub 2024-02-03 13:36:58 +01:00
parent 2f7266d771
commit 9785582bd2
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 23 additions and 17 deletions

View file

@ -147,24 +147,28 @@ open class GenerateOpenPgpKey(
userId: CharSequence, userId: CharSequence,
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply { ) = apply {
val hasPrimaryUID = primaryKey.userId(
primaryKey.pair.publicKey.userIDs.asSequence().any { uid -> userId,
primaryKey.pair.publicKey.getSignaturesForID(uid).asSequence().any { subpacketsCallback =
it.hashedSubPackets.isPrimaryUserID preferencesCallback.then(adjustPrimaryUserId()).then(subpacketsCallback))
} }
}
val setPrimaryUID = private fun adjustPrimaryUserId() =
SelfSignatureSubpackets.applyHashed { SelfSignatureSubpackets.applyHashed {
if (hasPrimaryUID) { if (primaryKeyHasPrimaryUserId()) {
setPrimaryUserId(null) setPrimaryUserId(null)
} else { } else {
setPrimaryUserId() setPrimaryUserId()
} }
} }
primaryKey.userId(
userId, private fun primaryKeyHasPrimaryUserId() =
subpacketsCallback = primaryKey.pair.publicKey.let { pk ->
preferencesCallback.then(setPrimaryUID).then(subpacketsCallback)) pk.userIDs.asSequence().any { uid ->
pk.getSignaturesForID(uid).asSequence().any {
it.hashedSubPackets.isPrimaryUserID
}
}
} }
/** /**
@ -302,7 +306,8 @@ open class GenerateOpenPgpKey(
return PGPSecretKeyRing( return PGPSecretKeyRing(
mutableListOf( mutableListOf(
toSecretKey(primaryKey, true, protector.getEncryptor(primaryKey.pair.keyID))) toSecretKey(
primaryKey, true, protector.getEncryptor(primaryKey.pair.keyID)))
.plus( .plus(
subkeys.map { subkeys.map {
toSecretKey(it, false, protector.getEncryptor(it.pair.keyID)) toSecretKey(it, false, protector.getEncryptor(it.pair.keyID))

View file

@ -215,7 +215,8 @@ class OpenPgpComponentKeyBuilder {
// Create back-sig // Create back-sig
val backSigBuilder = val backSigBuilder =
PrimaryKeyBindingSignatureBuilder(pair.privateKey, pair.publicKey, hashAlgorithm) PrimaryKeyBindingSignatureBuilder(
pair.privateKey, pair.publicKey, hashAlgorithm)
backSigBuilder.hashedSubpackets.setSignatureCreationTime(bindingTime) backSigBuilder.hashedSubpackets.setSignatureCreationTime(bindingTime)