diff --git a/src/main/java/io/github/novacrypto/SecureCharBuffer.java b/src/main/java/io/github/novacrypto/SecureCharBuffer.java index 6b0710f..e57306e 100644 --- a/src/main/java/io/github/novacrypto/SecureCharBuffer.java +++ b/src/main/java/io/github/novacrypto/SecureCharBuffer.java @@ -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. @@ -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(); diff --git a/src/test/java/io/github/novacrypto/SecureCharBufferTests.java b/src/test/java/io/github/novacrypto/SecureCharBufferTests.java index 9dcec15..d6b8149 100644 --- a/src/test/java/io/github/novacrypto/SecureCharBufferTests.java +++ b/src/test/java/io/github/novacrypto/SecureCharBufferTests.java @@ -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);