mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-25 04:17:59 +01:00
Improve readability of primaryUserId adjustment
This commit is contained in:
parent
2f7266d771
commit
9785582bd2
2 changed files with 23 additions and 17 deletions
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue