Skip to content

Commit 4bc650f

Browse files
optimising previous salt calculation function
1 parent a5c659a commit 4bc650f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/main/java/com/uid2/admin/secret/SaltRotation.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
public class SaltRotation {
2020
private final IKeyGenerator keyGenerator;
2121
private final long THIRTY_DAYS_IN_MS = Duration.ofDays(30).toMillis();
22+
private final long DAY_IN_MS = Duration.ofDays(1).toMillis();
2223

2324
public SaltRotation(IKeyGenerator keyGenerator) {
2425
this.keyGenerator = keyGenerator;
@@ -83,12 +84,13 @@ private long calculateRefreshFrom(long lastUpdated, long nextEffective) {
8384
return lastUpdated + (multiplier * THIRTY_DAYS_IN_MS);
8485
}
8586

86-
private String calculatePreviousSalt(SaltEntry oldSalt, boolean shouldRotate, long nextEffective) throws Exception {
87+
private String calculatePreviousSalt(SaltEntry salt, boolean shouldRotate, long nextEffective) throws Exception {
8788
if (shouldRotate) {
88-
return oldSalt.currentSalt();
89+
return salt.currentSalt();
8990
}
90-
if (ChronoUnit.DAYS.between(Instant.ofEpochMilli(oldSalt.lastUpdated()), Instant.ofEpochMilli(nextEffective)) < 90) {
91-
return oldSalt.previousSalt();
91+
long age = nextEffective - salt.lastUpdated();
92+
if ( age / DAY_IN_MS < 90) {
93+
return salt.previousSalt();
9294
}
9395
return null;
9496
}

src/test/java/com/uid2/admin/secret/SaltRotationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ void rotateSaltsRemovePreviousSaltIfOver90DaysOld() throws Exception {
280280
final long over90Days = daysEarlier(100).toEpochMilli();
281281
final RotatingSaltProvider.SaltSnapshot lastSnapshot = SnapshotBuilder.start()
282282
.withEntries(
283-
new SaltEntry(1, "1", lessThan90Days, "currentSalt", null, "lessThan90Days", null, null),
284-
new SaltEntry(2, "2", lessThan90Days, "currentSalt", null, "lessThan90Days", null, null),
283+
new SaltEntry(1, "1", lessThan90Days, "currentSalt", null, "lessThan90DaysOld", null, null),
284+
new SaltEntry(2, "2", lessThan90Days, "currentSalt", null, "lessThan90DaysOld", null, null),
285285
new SaltEntry(3, "3", is90DaysOld, "currentSalt", null, "90DaysOld", null, null),
286286
new SaltEntry(4, "4", is90DaysOld, "currentSalt", null, "90DaysOld", null, null),
287287
new SaltEntry(5, "5", over90Days, "currentSalt", null, null, null, null),
@@ -291,7 +291,7 @@ void rotateSaltsRemovePreviousSaltIfOver90DaysOld() throws Exception {
291291

292292
final SaltRotation.Result result = saltRotation.rotateSalts(lastSnapshot, minAges, 0.5, targetDate);
293293
assertTrue(result.hasSnapshot());
294-
assertEquals(2, Arrays.stream(result.getSnapshot().getAllRotatingSalts()).filter(s -> "lessThan90Days".equals(s.previousSalt())).count());
294+
assertEquals(2, Arrays.stream(result.getSnapshot().getAllRotatingSalts()).filter(s -> "lessThan90DaysOld".equals(s.previousSalt())).count());
295295
assertEquals(1, Arrays.stream(result.getSnapshot().getAllRotatingSalts()).filter(s -> s.previousSalt() == null).count());
296296
assertEquals(3, Arrays.stream(result.getSnapshot().getAllRotatingSalts()).filter(s -> "currentSalt".equals(s.previousSalt())).count());
297297

0 commit comments

Comments
 (0)