content: package androidkun.com.versionupdatelibrary.utils;
  
  import android.content.Context;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  
  import java.security.MessageDigest;
  
  /**
   * Created by Kun on 2017/11/13.
   * GitHub: https://github.com/AndroidKun
   * CSDN: http://blog.csdn.net/a1533588867
   * Description:MD5工具
   */
  
  public class MD5Util {
  
      public final static String MD5(String pwd) {
          char md5String[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                  'A', 'B', 'C', 'D', 'E', 'F' };
          try {
              byte[] btInput = pwd.getBytes();
  
              MessageDigest mdInst = MessageDigest.getInstance("MD5");
  
              mdInst.update(btInput);
  
              byte[] md = mdInst.digest();
  
              int j = md.length;
              char str[] = new char[j * 2];
              int k = 0;
              for (int i = 0; i < j; i++) {   //  i = 0
                  byte byte0 = md[i];  //95
                  str[k++] = md5String[byte0 >>> 4 & 0xf];    //    5
                  str[k++] = md5String[byte0 & 0xf];   //   F
              }
              return new String(str);
          } catch (Exception e) {
              return null;
          }
      }
  
      /**
       * 从一个apk文件去获取该文件的版本信息
       *
       * @param context
       *            本应用程序上下文
       * @param archiveFilePath
       *            APK文件的路径。如：/sdcard/download/XX.apk
       * @return
       */
      public static String getVersionNameFromApk(Context context, String archiveFilePath) {
          PackageManager pm = context.getPackageManager();
          PackageInfo packInfo = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);
          if(packInfo!=null) {
              return packInfo.versionName;
          }
          return null;
      }
  
  
  }
  