/*
OTP4U 0.9.3 - One Time Pad for you
Copyright (C) 2003 Pierre Blanc
Pierre Blanc: blanc_teutoburgo@yahoo.it
http://www.teutoburgo.tk
http://it.geocities.com/teutoburgo
This program 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 program 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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or go to http://www.gnu.org/copyleft/gpl.html
*/
package tk.teutoburgo.otp4u.crypt;
import java.io.File;
import java.security.SecureRandom;
import tk.teutoburgo.otp4u.constants.Constants;
import tk.teutoburgo.otp4u.io.Io;
import tk.teutoburgo.otp4u.util.MessagesPrinter;
/**
* <p>Title: OTP4U</p>
* <p>Description: One Time Pad for you</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: Teutoburgo</p>
* @author Pierre Blanc
* @version 1.0
*/
public class RandomGenerator {
MessagesPrinter mp;
//Random rnd = new Random();
//12 Apr 2013 used SecureRandom instead of insecure Random
Io io;
boolean isGUI=false;
public static final String PSEUDO_RANDOM
=Constants.
PSEUDO_RANDOM;
public RandomGenerator() {
}
public RandomGenerator
(String home,
boolean isGUI
){
OTP4U_HOME=home;
this.isGUI=isGUI;
mp = new MessagesPrinter(isGUI);
io = new Io(home, isGUI);
}
/**
* Used to generate a PSEUDO-random file in the entropySource directory
* or in the key4key directory.
* Use it to test the functionalities of OTP4U.
* @param pathName the path name of the pseudo-random file
* @param fileName the file name
* @param length the file length
*/
public void generateRandomFile
(String pathName,
String fileName,
int length){
int fLength=fileName.length();
int max=0;
byte[] randomBytes = new byte[length];
rnd.nextBytes(randomBytes);
if(fileName.equals(PSEUDO_RANDOM)){
File[] list
= f.
listFiles();
try{
for (int i=0; i<list.length; i++){
if (!(fLength>list[i].getName().toString().length())){
if(list[i].getName().toString().substring(
0,fLength).equals(fileName))
max++;
}
}
e.printStackTrace();
}
generated=pathName+fileName+(max+1)+".rnd";
} else
generated=pathName+fileName;
try{
io.writeFile(generated,randomBytes);
e.printStackTrace();
}
mp.sPrintLog("Generated "+length+" pseudo random bytes in file "+
generated);
}
}