Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/io/github/novacrypto/SecureCharBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
package io.github.novacrypto;

import java.io.Closeable;
import java.util.Timer;
import java.util.TimerTask;

/**
* A store of char data that is encrypted with a one-time-pad.
Expand Down Expand Up @@ -107,6 +109,21 @@ public void close() {
buffer.close();
}

/**
* @param delay delay in milliseconds before task is to be executed.
*/
public void timout(int delay) {
new Timer().schedule(
new TimerTask() {
@Override
public void run() {
buffer.close();
}
},
delay
);
}

@Override
public String toString() {
throw new UnsupportedOperationException();
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/io/github/novacrypto/SecureCharBufferTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ public void readAndWriteAllLargeChars() {
}
}

@Test
public void testTimeout() throws InterruptedException {
SecureCharBuffer buffer = new SecureCharBuffer();
buffer.append("abc");
buffer.timout(1000);
Thread.sleep(1500);
final String read = readWholeBufferAsString(buffer);
assertEquals(512, buffer.length());
assertEquals(512, read.length());
for (int i = 0; i < read.length(); i++) {
assertEquals('\0', read.charAt(i));
}
}

private static char readCharAt(SecureCharBuffer buffer, int index) {
final char c1 = buffer.get(index);
final char c2 = buffer.charAt(index);
Expand Down