/** * Generates a new KeyIdentifier and sets that to be the current key being used. * @return A new KeyIdentifier. */ protected final KeyIdentifier generateKey() { Random rand = new Random(); int nextId; do { nextId = rand.nextInt(Integer.MAX_VALUE); } while (hasKey(nextId)); long now = System.currentTimeMillis(); SecretKey nextKey = keyGenerator.generateKey(); KeyIdentifier keyIdentifier = new KeyIdentifier(nextKey, nextId, keyExpirationPeriod > 0 ? (now + keyExpirationPeriod) : Long.MAX_VALUE); addKey(keyIdentifier); this.currentKey = keyIdentifier; LOG.info("Changed current key to {}", currentKey); return keyIdentifier; }