1 7 package org.jboss.security.plugins; 8 9 import java.io.File ; 10 import java.io.FileInputStream ; 11 import java.io.IOException ; 12 import java.io.CharArrayWriter ; 13 import java.io.RandomAccessFile ; 14 15 import org.jboss.logging.Logger; 16 17 30 public class TmpFilePassword 31 { 32 private static Logger log = Logger.getLogger(TmpFilePassword.class); 33 private File passwordFile; 34 35 public TmpFilePassword(String file) 36 { 37 passwordFile = new File (file); 38 } 39 40 public char[] toCharArray() 41 throws IOException 42 { 43 while( passwordFile.exists() == false ) 44 { 45 log.info("Waiting for password file: "+passwordFile.getAbsolutePath()); 46 try 47 { 48 Thread.sleep(10*1000); 49 } 50 catch(InterruptedException e) 51 { 52 log.info("Exiting wait on InterruptedException"); 53 break; 54 } 55 } 56 FileInputStream fis = new FileInputStream (passwordFile); 57 CharArrayWriter writer = new CharArrayWriter (); 58 int b; 59 while( (b = fis.read()) >= 0 ) 60 { 61 if( b == '\r' || b == '\n' ) 62 continue; 63 writer.write(b); 64 } 65 fis.close(); 66 char[] password = writer.toCharArray(); 67 writer.reset(); 68 for(int n = 0; n < password.length; n ++) 69 writer.write('\0'); 70 71 try 73 { 74 RandomAccessFile raf = new RandomAccessFile (passwordFile, "rws"); 75 for(int i = 0; i < 10; i ++) 76 { 77 raf.seek(0); 78 for(int j = 0; j < password.length; j ++) 79 raf.write(j); 80 } 81 raf.close(); 82 if( passwordFile.delete() == false ) 83 log.warn("Was not able to delete the password file"); 84 } 85 catch(Exception e) 86 { 87 log.warn("Failed to zero the password file", e); 88 } 89 return password; 90 } 91 } 92 | Popular Tags |