|  | 
  | 
    
      | 
        
          | 
            
              | 
                
                  | 자주 쓰이는 메소드및 API 를 정리합니다. |  
                  | 
                    
                      | 탭으로 나뉘어져 있는 글입니다.                          [1][2] |  
                      | 
                        
                          |   | 등록일:2008-03-18 15:34:12           (0%) 작성자:
   제목:java.security > MessageDigest
 |  |  
                      | MessageDigest:  getInstance(String  algorithm) 
 import  java.io.BufferedInputStream;
 import  java.io.FileInputStream;
 import  java.io.IOException;
 import  java.security.DigestInputStream;
 import  java.security.MessageDigest;
 import  java.security.NoSuchAlgorithmException;
 import  java.security.Provider;
 import  java.security.Security;
 import  java.util.zip.CRC32;
 
 public  class  MainClass  {
 static  private  String  hexDigit(byte  x)  {
 StringBuffer  sb  =  new  StringBuffer();
 char  c;
 //  First  nibble
 c  =  (char)  ((x  >>  4)  &  0xf);
 if  (c  >  9)  {
 c  =  (char)  ((c  -  10)  +  'a');
 }  else  {
 c  =  (char)  (c  +  '0');
 }
 sb.append(c);
 //  Second  nibble
 c  =  (char)  (x  &  0xf);
 if  (c  >  9)  {
 c  =  (char)  ((c  -  10)  +  'a');
 }  else  {
 c  =  (char)  (c  +  '0');
 }
 sb.append(c);
 return  sb.toString();
 }
 
 static  private  String  computeDigest(MessageDigest  algorithm,  String  filename)  {
 String  returnValue  =  "";
 try  {
 algorithm.reset();
 FileInputStream  fis  =  new  FileInputStream(filename);
 BufferedInputStream  bis  =  new  BufferedInputStream(fis);
 DigestInputStream  dis  =  new  DigestInputStream(bis,  algorithm);
 int  ch;
 while  ((ch  =  dis.read())  !=  -1)
 ;
 StringBuffer  hexString  =  new  StringBuffer();
 byte  digest[]  =  algorithm.digest();
 int  digestLength  =  digest.length;
 for  (int  i  =  0;  i  <  digestLength;  i++)  {
 hexString.append(hexDigit(digest[i]));
 hexString.append("  ");
 }
 returnValue  =  hexString.toString();
 }  catch  (IOException  e)  {
 System.err.println("Error  generating  digest  for:  "  +  filename);
 }
 return  returnValue;
 }
 
 public  static  void  main(String  args[])  {
 MessageDigest  algorithm  =  null;
 Security.addProvider(new  MyProvider());
 try  {
 algorithm  =  MessageDigest.getInstance("CRC32");
 }  catch  (NoSuchAlgorithmException  e)  {
 System.err.println("Invalid  algorithm");
 System.exit(-1);
 }
 int  argsLength  =  args.length;
 for  (int  i  =  0;  i  <  argsLength;  i++)  {
 String  digest  =  computeDigest(algorithm,  args[i]);
 System.out.println(args[i]  +  "  :  "  +  digest);
 
 }
 }
 }
 
 class  MyProvider  extends  Provider  {
 public  MyProvider()  {
 super("MyProvider",  1.0,  "Java2s.com  Collections  Example");
 put("CRC32",  "CRC");
 }
 }
 
 class  CRC  extends  MessageDigest  {
 CRC32  crc;
 
 public  CRC()  {
 super("CRC");
 crc  =  new  CRC32();
 }
 
 protected  void  engineReset()  {
 crc.reset();
 }
 
 protected  void  engineUpdate(byte  input)  {
 crc.update(input);
 }
 
 protected  void  engineUpdate(byte[]  input,  int  offset,  int  len)  {
 crc.update(input,  offset,  len);
 }
 
 protected  byte[]  engineDigest()  {
 long  l  =  crc.getValue();
 byte[]  bytes  =  new  byte[4];
 bytes[3]  =  (byte)  ((l  &  0xFF000000)  >>  24);
 bytes[2]  =  (byte)  ((l  &  0x00FF0000)  >>  16);
 bytes[1]  =  (byte)  ((l  &  0x0000FF00)  >>  8);
 bytes[0]  =  (byte)  ((l  &  0x000000FF)  >>  0);
 return  bytes;
 }
 }
 
 
 |  
                      | 
 extends  MessageDigest
 
 import  java.io.BufferedInputStream;
 import  java.io.FileInputStream;
 import  java.io.IOException;
 import  java.security.DigestInputStream;
 import  java.security.MessageDigest;
 import  java.security.NoSuchAlgorithmException;
 import  java.security.Provider;
 import  java.security.Security;
 import  java.util.zip.CRC32;
 
 public  class  MainClass  {
 static  private  String  hexDigit(byte  x)  {
 StringBuffer  sb  =  new  StringBuffer();
 char  c;
 //  First  nibble
 c  =  (char)  ((x  >>  4)  &  0xf);
 if  (c  >  9)  {
 c  =  (char)  ((c  -  10)  +  'a');
 }  else  {
 c  =  (char)  (c  +  '0');
 }
 sb.append(c);
 //  Second  nibble
 c  =  (char)  (x  &  0xf);
 if  (c  >  9)  {
 c  =  (char)  ((c  -  10)  +  'a');
 }  else  {
 c  =  (char)  (c  +  '0');
 }
 sb.append(c);
 return  sb.toString();
 }
 
 static  private  String  computeDigest(MessageDigest  algorithm,  String  filename)  {
 String  returnValue  =  "";
 try  {
 algorithm.reset();
 FileInputStream  fis  =  new  FileInputStream(filename);
 BufferedInputStream  bis  =  new  BufferedInputStream(fis);
 DigestInputStream  dis  =  new  DigestInputStream(bis,  algorithm);
 int  ch;
 while  ((ch  =  dis.read())  !=  -1)
 ;
 StringBuffer  hexString  =  new  StringBuffer();
 byte  digest[]  =  algorithm.digest();
 int  digestLength  =  digest.length;
 for  (int  i  =  0;  i  <  digestLength;  i++)  {
 hexString.append(hexDigit(digest[i]));
 hexString.append("  ");
 }
 returnValue  =  hexString.toString();
 }  catch  (IOException  e)  {
 System.err.println("Error  generating  digest  for:  "  +  filename);
 }
 return  returnValue;
 }
 
 public  static  void  main(String  args[])  {
 MessageDigest  algorithm  =  null;
 Security.addProvider(new  MyProvider());
 try  {
 algorithm  =  MessageDigest.getInstance("CRC32");
 }  catch  (NoSuchAlgorithmException  e)  {
 System.err.println("Invalid  algorithm");
 System.exit(-1);
 }
 int  argsLength  =  args.length;
 for  (int  i  =  0;  i  <  argsLength;  i++)  {
 String  digest  =  computeDigest(algorithm,  args[i]);
 System.out.println(args[i]  +  "  :  "  +  digest);
 
 }
 }
 }
 
 class  MyProvider  extends  Provider  {
 public  MyProvider()  {
 super("MyProvider",  1.0,  "Java2s.com  Collections  Example");
 put("CRC32",  "CRC");
 }
 }
 
 class  CRC  extends  MessageDigest  {
 CRC32  crc;
 
 public  CRC()  {
 super("CRC");
 crc  =  new  CRC32();
 }
 
 protected  void  engineReset()  {
 crc.reset();
 }
 
 protected  void  engineUpdate(byte  input)  {
 crc.update(input);
 }
 
 protected  void  engineUpdate(byte[]  input,  int  offset,  int  len)  {
 crc.update(input,  offset,  len);
 }
 
 protected  byte[]  engineDigest()  {
 long  l  =  crc.getValue();
 byte[]  bytes  =  new  byte[4];
 bytes[3]  =  (byte)  ((l  &  0xFF000000)  >>  24);
 bytes[2]  =  (byte)  ((l  &  0x00FF0000)  >>  16);
 bytes[1]  =  (byte)  ((l  &  0x0000FF00)  >>  8);
 bytes[0]  =  (byte)  ((l  &  0x000000FF)  >>  0);
 return  bytes;
 }
 }
 [2008년  03월  18일  15:34:32  수정되었습니다.]
 |  
                      | [본문링크] java.security > MessageDigest |  
                      | 탭으로 나뉘어져 있는 글입니다.                          [1][2] |  
                      |  |  |  |  |  
  | 
    
      | 코멘트(이글의 트랙백 주소:/cafe/tb_receive.php?no=2879   |  |  
  |  |  
  |  |  
  |  |  
          |  |  
          |  |  |  |  | 
|  
	
		| 
	
	| Copyright byCopyright ⓒ2005, SSISO Community All Rights Reserved.   |  |  |