1 16 package org.mortbay.util; 17 import java.io.IOException ; 18 19 import org.apache.commons.logging.Log; 20 import org.mortbay.log.LogFactory; 21 22 23 48 public class Password extends Credential 49 { 50 private static Log log = LogFactory.getLog(Password.class); 51 52 private String _pw; 53 54 55 58 public Password(String password) 59 { 60 _pw=password; 61 62 while (_pw!=null && _pw.startsWith("OBF:")) 64 _pw=deobfuscate(_pw); 65 } 66 67 68 public String toString() 69 { 70 return _pw; 71 } 72 73 74 public String toStarString() 75 { 76 return "*****************************************************" 77 .substring(0,_pw.length()); 78 } 79 80 81 public boolean check(Object credentials) 82 { 83 if (this == credentials) 84 return true; 85 86 if (credentials instanceof Password) 87 return credentials.equals(_pw); 88 89 if (credentials instanceof String ) 90 return credentials.equals(_pw); 91 92 if (credentials instanceof Credential) 93 return ((Credential)credentials).check(_pw); 94 95 return false; 96 } 97 98 99 public boolean equals(Object o) 100 { 101 if (this == o) 102 return true; 103 104 if (null == o) 105 return false; 106 107 if (o instanceof Password) 108 { 109 Password p=(Password)o; 110 return p._pw == _pw || (null != _pw && _pw.equals(p._pw)); 111 } 112 113 if (o instanceof String ) 114 return o.equals(_pw); 115 116 return false; 117 } 118 119 120 public int hashCode() { 121 return null == _pw ? super.hashCode() : _pw.hashCode(); 122 } 123 124 125 public static String obfuscate(String s) 126 { 127 StringBuffer buf = new StringBuffer (); 128 byte[] b = s.getBytes(); 129 130 synchronized(buf) 131 { 132 buf.append("OBF:"); 133 for (int i=0;i<b.length;i++) 134 { 135 byte b1 = b[i]; 136 byte b2 = b[s.length()-(i+1)]; 137 int i1= (int)b1+(int)b2+127; 138 int i2= (int)b1-(int)b2+127; 139 int i0=i1*256+i2; 140 String x=Integer.toString(i0,36); 141 142 switch(x.length()) 143 { 144 case 1:buf.append('0'); 145 case 2:buf.append('0'); 146 case 3:buf.append('0'); 147 default:buf.append(x); 148 } 149 } 150 return buf.toString(); 151 } 152 } 153 154 155 public static String deobfuscate(String s) 156 { 157 if (s.startsWith("OBF:")) 158 s=s.substring(4); 159 160 byte[] b=new byte[s.length()/2]; 161 int l=0; 162 for (int i=0;i<s.length();i+=4) 163 { 164 String x=s.substring(i,i+4); 165 int i0 = Integer.parseInt(x,36); 166 int i1=(i0/256); 167 int i2=(i0%256); 168 b[l++]=(byte)((i1+i2-254)/2); 169 } 170 171 return new String (b,0,l); 172 } 173 174 175 186 public static Password getPassword(String realm,String dft, String promptDft) 187 { 188 String passwd=System.getProperty(realm,dft); 189 if (passwd==null || passwd.length()==0) 190 { 191 try 192 { 193 System.out.print(realm+ 194 ((promptDft!=null && promptDft.length()>0) 195 ?" [dft]":"")+" : "); 196 System.out.flush(); 197 byte[] buf = new byte[512]; 198 int len=System.in.read(buf); 199 if (len>0) 200 passwd=new String (buf,0,len).trim(); 201 } 202 catch(IOException e) 203 { 204 log.warn(LogSupport.EXCEPTION,e); 205 } 206 if (passwd==null || passwd.length()==0) 207 passwd=promptDft; 208 } 209 return new Password(passwd); 210 } 211 212 213 214 217 public static void main(String [] arg) 218 { 219 if (arg.length!=1 && arg.length!=2 ) 220 { 221 System.err.println("Usage - java org.mortbay.util.Password [<user>] <password>"); 222 System.err.println("If the password is ?, the user will be prompted for the password"); 223 System.exit(1); 224 } 225 String p=arg[arg.length==1?0:1]; 226 Password pw = "?".equals(p)?new Password(p):new Password(p); 227 System.err.println(pw.toString()); 228 System.err.println(obfuscate(pw.toString())); 229 System.err.println(Credential.MD5.digest(p)); 230 if (arg.length==2) 231 System.err.println(Credential.Crypt.crypt(arg[0],pw.toString())); 232 } 233 } 234 235 236 | Popular Tags |