Download this file

Zeroizer.java    174 lines (153 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package cologne.eck.tools;
/*
* Peafactory - Production of Password Encryption Archives
* Copyright (C) 2015 Axel von dem Bruch
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* See: http://www.gnu.org/licenses/gpl-2.0.html
* You should have received a copy of the GNU General Public License
* along with this library.
*/
/**
* Zeroization of sensitive data.
*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
/*
* Class to prevent compilers' redundant code elimination.
*
*/
public class Zeroizer {
private static byte zero8bit = 0;
private static char zeroChar = '\0';
private static int zero32bit = 0;
private static long zero64bit = 0L;
public final static void zero(byte[] input) {
if (input != null && input.length > 0) {
Arrays.fill(input, (byte) 0);
/*for (int i = 0; i < input.length; i++) {
zero8bit |= input[i];
} */
zero8bit |= input[input.length - 1];
}
}
public final static void zero(int[] input) {
if (input != null && input.length > 0) {
Arrays.fill(input, 0);
/*for (int i = 0; i < input.length; i++) {
zero32bit |= input[i];
}*/
zero32bit |= input[input.length - 1];
}
}
public final static void zero(long[] input) {
if (input != null && input.length > 0) {
Arrays.fill(input, 0);
/*for (int i = 0; i < input.length; i++) {
zero64bit |= input[i];
}*/
zero64bit |= input[input.length - 1];
}
}
public final static void zero(char[] input) {
/*for (int i = 0; i < input.length; i++) {
zeroChar |= input[i];
}*/
if (input != null && input.length > 0) {
Arrays.fill(input, '\0');
zeroChar |= input[input.length - 1];
}
}
public final static void zero(byte zero) {
zero = (byte) 0;
zero8bit |= zero;
}
public final static void zero(int zero) {
zero = 0;
zero32bit |= zero;
}
public final static void zero(long zero) {
zero = (byte) 0;
zero64bit |= zero;
}
public final static void zero(char zero) {
zero = '\0';
zeroChar |= zero;
}
/**
* Overwrite a file with zeros to clear secret values.
* This will not work for very large files, because it is not buffered.
*
* @param fileName the name of the file to clear
*/
public final static void zeroFile(String fileName) {
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(fileName, "rw");
// first overwrite with 1
byte[] oneBytes = new byte[(int)raf.length()];
Arrays.fill(oneBytes, (byte) 0xFF);
raf.seek(0);
raf.write(oneBytes);
// then overwrite with 0
byte[] zeroBytes = new byte[(int)raf.length()];
raf.seek(0);
raf.write(zeroBytes);
raf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* To check the execution, this function should be
* called as last step before the application exits
*/
public final static void getZeroizationResult() {
if (zeroChar != '\0') {
System.err.println("Zeroization failed - char: " + zeroChar);
}
if ( (zero8bit != 0) || (zero32bit != 0) || (zero64bit != 0)) {
System.err.println("Zeroization failed - \nbyte: " + zero8bit
+ "\nint: " + zero32bit
+ "\nlong: " + zero64bit);
} else {
System.out.println("Zeroization: success");
}
}
// Test:
/* public static void main(String[] args) {
char[] testChars = "abcdefghijklmnopqrstuvwxyz".toCharArray();
byte[] testBytes = new byte[128];
Arrays.fill(testBytes, (byte) 5);
int[] testInts = new int[55];
Arrays.fill(testInts, 55);
long[] testLongs = new long[33];
Arrays.fill(testLongs, 33L);
byte b = (byte) 0xFF;
int i = 0xFFFFFFFF;
long l = 0xFFFFFFFFFFFFFFFFL;
char c = 'X';
zero(testChars);
zero(testBytes);
zero(testInts);
zero(testLongs);
zero(b);
zero(i);
zero(l);
zero(c);
getZeroizationResult();
} */
}