1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-22 20:32:05 +01:00

Key generation: Set default expiration periof of 5 years

Can be changed by calling 'keyRingBuilder.setExpirationDate(null);'
This commit is contained in:
Paul Schaub 2023-06-01 23:43:41 +02:00
parent be5562d273
commit 528591f906
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 12 additions and 5 deletions

View file

@ -54,6 +54,8 @@ import org.pgpainless.util.Passphrase;
public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> { public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
private static final long YEAR_IN_SECONDS = 1000L * 60 * 60 * 24 * 365;
@SuppressWarnings("CharsetObjectCanBeUsed") @SuppressWarnings("CharsetObjectCanBeUsed")
private final Charset UTF8 = Charset.forName("UTF-8"); private final Charset UTF8 = Charset.forName("UTF-8");
@ -61,7 +63,7 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
private final List<KeySpec> subkeySpecs = new ArrayList<>(); private final List<KeySpec> subkeySpecs = new ArrayList<>();
private final Map<String, SelfSignatureSubpackets.Callback> userIds = new LinkedHashMap<>(); private final Map<String, SelfSignatureSubpackets.Callback> userIds = new LinkedHashMap<>();
private Passphrase passphrase = Passphrase.emptyPassphrase(); private Passphrase passphrase = Passphrase.emptyPassphrase();
private Date expirationDate = null; private Date expirationDate = new Date(new Date().getTime() + YEAR_IN_SECONDS * 5); // Expiration in 5 yeras
@Override @Override
public KeyRingBuilder setPrimaryKey(@Nonnull KeySpec keySpec) { public KeyRingBuilder setPrimaryKey(@Nonnull KeySpec keySpec) {
@ -97,7 +99,13 @@ public class KeyRingBuilder implements KeyRingBuilderInterface<KeyRingBuilder> {
} }
@Override @Override
public KeyRingBuilder setExpirationDate(@Nonnull Date expirationDate) { public KeyRingBuilder setExpirationDate(@Nullable Date expirationDate) {
if (expirationDate == null) {
// No expiration
this.expirationDate = null;
return this;
}
Date now = new Date(); Date now = new Date();
if (now.after(expirationDate)) { if (now.after(expirationDate)) {
throw new IllegalArgumentException("Expiration date must be in the future."); throw new IllegalArgumentException("Expiration date must be in the future.");

View file

@ -5,7 +5,6 @@
package org.pgpainless.key.modification; package org.pgpainless.key.modification;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -38,7 +37,7 @@ public class OldSignatureSubpacketsArePreservedOnNewSigTest {
assertNotNull(oldSignature); assertNotNull(oldSignature);
PGPSignatureSubpacketVector oldPackets = oldSignature.getHashedSubPackets(); PGPSignatureSubpacketVector oldPackets = oldSignature.getHashedSubPackets();
assertEquals(0, oldPackets.getKeyExpirationTime()); long oldExpiration = oldPackets.getKeyExpirationTime();
Date now = new Date(); Date now = new Date();
Date t1 = new Date(now.getTime() + millisInHour); Date t1 = new Date(now.getTime() + millisInHour);
@ -51,7 +50,7 @@ public class OldSignatureSubpacketsArePreservedOnNewSigTest {
assertNotNull(newSignature); assertNotNull(newSignature);
PGPSignatureSubpacketVector newPackets = newSignature.getHashedSubPackets(); PGPSignatureSubpacketVector newPackets = newSignature.getHashedSubPackets();
assertNotEquals(0, newPackets.getKeyExpirationTime()); assertNotEquals(oldExpiration, newPackets.getKeyExpirationTime());
assertArrayEquals(oldPackets.getPreferredHashAlgorithms(), newPackets.getPreferredHashAlgorithms()); assertArrayEquals(oldPackets.getPreferredHashAlgorithms(), newPackets.getPreferredHashAlgorithms());
} }