MDUtil

该博客介绍了MDUtil工具类,它支持MD5和SHA - 1两种消息摘要算法。提供了字节数组转十六进制字符串、消息摘要编码、编码转字符串以及校验编码等功能,还给出了测试代码,可用于对信息进行摘要处理和验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
 *          @(#) MDUtil.java
 */
package edu.hust.util.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
 *       MDUtil is a util tool for message digest.
 *       There are two MessageDigitest algorithm supported: MD5 and SHA-1
 *         
 *       @author quickpoint
 *       @version 1.0 06/21/2005    
 *
 */
public class MDUtil {
     // algorithm md5   
        public static final String MD5 = "MD5";
        // algoritym sha-1
        public static final String SHA = "SHA-1";

        // hex digits
        private static final String[] hexDigits = {
           "0", "1", "2", "3", "4", "5", "6", "7",
            "8", "9", "a", "b", "c", "d", "e", "f"
          };
         
         /**
          *   Convert byte array to hex string
          *   @param b byte array
          *   @return hex string
          */
         public static String byteArrayToHexString( byte[] b ) {
              StringBuffer buf = new StringBuffer();
              for( int i = 0; i < b.length; i++) {
                  buf.append( byteToHexString( b[i]));
              }
                  return buf.toString();
           }
        
         /**
          *    Convert byte to hex String
          *    @param b byte
          *    @return hex String
          */
         private static String byteToHexString( byte b ) {
               int n = b;
               // n < 0 then make n positive
               if( n < 0 )  {
                     n += 256;
               }
               int d1 = n >> 4; // upper bit
               int d2 = n % 16; // lower bit
               
               return hexDigits[d1] + hexDigits[d2];
         }
        
          /**
           *   Encode Message Digest with algorithm
           *   @param origin information string
           *   @param algorithm  algorithm used in digest
           *   @return byte array of message digest
           *   @exception NoSuchAlgorithmException throws when algorithm does not support
           */
          public static byte[] encode( String origin, String algorithm )
                      throws NoSuchAlgorithmException
           {
               String resultStr = null;
               resultStr = new String(origin );
               MessageDigest md = MessageDigest.getInstance( algorithm );
               md.update( resultStr.getBytes());
               return md.digest();
           }
          
           /**
            *   Encode Message Digest with MD5
            *   @param origin information string
            *   @return byte array of message digest
            *   @exception NoSuchAlgorithmException throws when MD5 does not support.
            */  
           public static byte[] encodeWithMD5( String origin )
                    throws NoSuchAlgorithmException
           {
                return encode( origin, MDUtil.MD5);
           }
           
           /**
            *   Encode Message Digest with SHA
            *   @param origin information string
            *   @return byte array of message digest
            *   @exception NoSuchAlgorithmException throws when SHA does not support
            */   
           public static byte[] encodeWithSHA( String origin )
                    throws NoSuchAlgorithmException
           {
                return encode( origin, MDUtil.SHA);
           }
          
           /**
            *   Encode Message Digest with algorithm   
            *   @param origin information string
            *   @param algorithm algorithm used in digest
            *   @return message digest( in string ) or null
            */  
           public static String encodeToString( String origin, String algorithm ) {
                  String resultStr = null;
                  try { 
                     return byteArrayToHexString(encode( origin, algorithm));
                  } catch ( NoSuchAlgorithmException ex ) {
                     return null;
                  }        
           }
           
           /**
            *   Encode Message Digest with MD5
            *   @param origin information string
            *   @return message digest( in string ) or null
            */
           public static String encodeToStringWithMD5( String origin ) {
                  return encodeToString( origin, MDUtil.MD5);
           } 
          
           /**
            *    Encode Message Digest with SHA
            *    @param origin information string
            *    @return message digest( in string ) or null
            */
           public static String encodeToStringWithSHA( String origin ) {
                  return encodeToString( origin, MDUtil.SHA );
           }
          
           /**
            *     Check encode to ensure match of message digest
            *     @param origin information string
            *     @param mDigest digest information
            *     @param algorithm algorithm used in digest
            *     @return true if match, or else return false
            *     @exception NoSuchAlgorithmException throws when algorithm does not support.
            */
           public static boolean checkEncode( String origin, byte[] mDigest, String algorithm )
                  throws NoSuchAlgorithmException
           {
               MessageDigest md = MessageDigest.getInstance( algorithm );
               md.update( origin.getBytes());
               if( md.isEqual( mDigest, md.digest()) ) {
                    return true;
               } else {
                    return false;
               }  
           }  
          
           /**
            *    Check encode to ensure match of message digest encoded by MD5  
            *    @param origin information string
            *    @param mDigest digest information encoded by MD5
            *    @return true if match or else return false
            */
           public static boolean checkEncodeWithMD5( String origin, byte[] mDigest ) {
               try {
                    return checkEncode( origin, mDigest, MDUtil.MD5 );
               } catch ( NoSuchAlgorithmException ex ) {
                    return false;
               }
           }
          
           /**
            *     Check encode to ensure match of message digest encoded by SHA
            *     @param origin information string
            *     @param mDigest digest information encoded in SHA
            *     @return true if match or else return false
            */
           public static boolean checkEncodeWithSHA( String origin, byte[] mDigest ) {
               try {
                    return checkEncode( origin, mDigest, MDUtil.SHA );
               } catch ( NoSuchAlgorithmException ex ) {
                    return false;
               } 
           }
          
           // main
           // just for test.
           public static void main( String[] args ) {
                  System.out.println( encodeToStringWithMD5("quickpoint"));
                  System.out.println( encodeToStringWithSHA("quickpoint"));
                  try {
                    System.out.println( checkEncodeWithMD5( "quickpoint", encodeWithMD5("quickpoint")));
                    System.out.println( checkEncodeWithSHA( "quickpoint", encodeWithSHA("quickpoint")));
                  } catch ( NoSuchAlgorithmException ex ) {
                    ex.printStackTrace();
                  }   
            }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值