如何在Android中对字符串进行哈希处理? [英] How to hash a string in Android?

查看:21
本文介绍了如何在Android中对字符串进行哈希处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Android 应用程序,并且有几个字符串我想在发送到数据库之前对其进行加密.我想要一些安全、易于实现的东西,每次传递相同的数据时都会生成相同的东西,并且无论传递给它的字符串有多大,最好都会产生一个保持恒定长度的字符串.也许我正在寻找哈希.

I am working on an Android app and have a couple strings that I would like to encrypt before sending to a database. I'd like something that's secure, easy to implement, will generate the same thing every time it's passed the same data, and preferably will result in a string that stays a constant length no matter how large the string being passed to it is. Maybe I'm looking for a hash.

推荐答案

此代码段计算任何给定字符串的 md5

This snippet calculate md5 for any given string

public String md5(String s) {
    try {
        // Create MD5 Hash
        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        digest.update(s.getBytes());
        byte messageDigest[] = digest.digest();

        // Create Hex String
        StringBuffer hexString = new StringBuffer();
        for (int i=0; i<messageDigest.length; i++)
            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
        return hexString.toString();

    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return "";
}

来源:http://www.androidsnippets.com/snippets/52/index.html

希望对你有用

这篇关于如何在Android中对字符串进行哈希处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆