android黑窗口获取md5_Android获取本应用的MD5值

有些对安全要求比较高的应用,需要和服务器做MD5的校验,这里提供Android获取本应用的MD5值的方法

/**

* 获取应用MD5

* @author YOLANDA

* @param context

* @return

*/

public static String getAPKMD5(Context context) {

String md5 = "";

String path = context.getApplicationContext().getPackageResourcePath();

File file = new File(path);

try {

md5 = StringUtil.getFileMD5String(file);

} catch (Exception e) {

e.printStackTrace();

}

return md5;

}

/**

* 获取文件MD5

* @author YOLANDA

* @param file

* @return

* @throws Exception

*/

public static String getFileMD5String(File file) throws Exception {

InputStream fis;

MessageDigest messagedigest = MessageDigest.getInstance("MD5");

fis = new FileInputStream(file);

byte[] buffer = new byte[1024];

int numRead = 0;

while ((numRead = fis.read(buffer)) > 0) {

messagedigest.update(buffer, 0, numRead);

}

fis.close();

return byte2Hex(messagedigest.digest());

}

/**

* byte[]转Hex字符串

* @author YOLANDA

* @param b

* @return

*/

public static String byte2Hex(byte[] b) {

if (null == b)

return null;

StringBuffer sBuffer = new StringBuffer();

String sTmep;

for (int i = 0; i < b.length; i++) {

sTmep = Integer.toHexString(b[i] & 0xFF);

if (sTmep.length() == 1)

sBuffer.append("0");

sBuffer.append(sTmep.toUpperCase(Locale.getDefault()));

}

return sBuffer.toString();

}String转hex 16进制,hex 16进制转String可以去看我另一片博文:

http://www.javashuo.com/article/p-mubbbdaz-vd.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值