Fixed a bug that dropped some zeros in the message digest.
Added a method that directly takes a byte array as input.
This commit is contained in:
		
							parent
							
								
									01254c1a88
								
							
						
					
					
						commit
						1690b6bf58
					
				
					 1 changed files with 7 additions and 3 deletions
				
			
		| 
						 | 
					@ -22,12 +22,16 @@ public class MD5Encoder {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static String encode(String str) throws NoSuchAlgorithmException {
 | 
					    public static String encode(String str) throws NoSuchAlgorithmException {
 | 
				
			||||||
 | 
					        return encode (str.getBytes());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public static String encode(byte[] message) throws NoSuchAlgorithmException {
 | 
				
			||||||
        md = MessageDigest.getInstance("MD5");
 | 
					        md = MessageDigest.getInstance("MD5");
 | 
				
			||||||
        byte[] b = md.digest(str.getBytes());
 | 
					        byte[] b = md.digest(message);
 | 
				
			||||||
        StringBuffer buf = new StringBuffer();
 | 
					        StringBuffer buf = new StringBuffer(b.length*2);
 | 
				
			||||||
        for ( int i=0; i<b.length; i++ )    {
 | 
					        for ( int i=0; i<b.length; i++ )    {
 | 
				
			||||||
            int j = (b[i]<0) ? 256+b[i] : b[i];
 | 
					            int j = (b[i]<0) ? 256+b[i] : b[i];
 | 
				
			||||||
            if ( j<10 ) buf.append("0");
 | 
					            if ( j<16 ) buf.append("0");
 | 
				
			||||||
            buf.append(Integer.toHexString(j));
 | 
					            buf.append(Integer.toHexString(j));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return buf.toString();
 | 
					        return buf.toString();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue