Search in sources :

Example 1 with Trivium

use of run.wallet.common.delete.ciphers.Trivium in project run-wallet-android by runplay.

the class SecureDeleteFile method delete.

/**
 * Securely delete a file.
 *
 * Currently, there is only 1 pass that overwrites the file first
 * with a random bit stream generated by Trivium.
 *
 * @param file
 * @return true if this File was deleted, false otherwise.
 */
public static boolean delete(File file) {
    // Log.e("FILEDEL", file.getAbsolutePath());
    if (file.exists()) {
        SecureRandom random = new SecureRandom();
        Trivium tri = new Trivium();
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "rw");
            FileChannel channel = raf.getChannel();
            MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, raf.length());
            byte[] key = new byte[10];
            byte[] nonce = new byte[10];
            random.nextBytes(key);
            random.nextBytes(nonce);
            tri.setupKey(Trivium.MODE_DECRYPT, key, 0);
            tri.setupNonce(nonce, 0);
            int buffersize = 1024;
            byte[] bytes = new byte[1024];
            // overwrite with random numbers
            while (buffer.hasRemaining()) {
                int max = buffer.limit() - buffer.position();
                if (max > buffersize)
                    max = buffersize;
                // random.nextBytes(bytes);
                tri.process(bytes, 0, bytes, 0, max);
                buffer.put(bytes, 0, max);
            }
            buffer.force();
            buffer.rewind();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "FileNotFoundException", e);
        } catch (IOException e) {
            Log.d(TAG, "IOException", e);
        } catch (ESJException e) {
            Log.d(TAG, "ESJException", e);
        }
        // return true;
        return file.delete();
    }
    return false;
}
Also used : Trivium(run.wallet.common.delete.ciphers.Trivium) RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) SecureRandom(java.security.SecureRandom) IOException(java.io.IOException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 SecureRandom (java.security.SecureRandom)1 Trivium (run.wallet.common.delete.ciphers.Trivium)1