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,26 +147,30 @@ open class GenerateOpenPgpKey(
userId: CharSequence,
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply {
val hasPrimaryUID =
primaryKey.pair.publicKey.userIDs.asSequence().any { uid ->
primaryKey.pair.publicKey.getSignaturesForID(uid).asSequence().any {
it.hashedSubPackets.isPrimaryUserID
}
}
val setPrimaryUID =
SelfSignatureSubpackets.applyHashed {
if (hasPrimaryUID) {
setPrimaryUserId(null)
} else {
setPrimaryUserId()
}
}
primaryKey.userId(
userId,
subpacketsCallback =
preferencesCallback.then(setPrimaryUID).then(subpacketsCallback))
preferencesCallback.then(adjustPrimaryUserId()).then(subpacketsCallback))
}
private fun adjustPrimaryUserId() =
SelfSignatureSubpackets.applyHashed {
if (primaryKeyHasPrimaryUserId()) {
setPrimaryUserId(null)
} else {
setPrimaryUserId()
}
}
private fun primaryKeyHasPrimaryUserId() =
primaryKey.pair.publicKey.let { pk ->
pk.userIDs.asSequence().any { uid ->
pk.getSignaturesForID(uid).asSequence().any {
it.hashedSubPackets.isPrimaryUserID
}
}
}
/**
* Add a user-attribute to the key. The subpackets of the binding signature are
* prepopulated, setting algorithm preferences and features. However, the subpackets can
@ -302,7 +306,8 @@ open class GenerateOpenPgpKey(
return PGPSecretKeyRing(
mutableListOf(
toSecretKey(primaryKey, true, protector.getEncryptor(primaryKey.pair.keyID)))
toSecretKey(
primaryKey, true, protector.getEncryptor(primaryKey.pair.keyID)))
.plus(
subkeys.map {
toSecretKey(it, false, protector.getEncryptor(it.pair.keyID))

View File

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