如何生成的android一个唯一的哈希code字符串输入...? [英] How to generate a unique hash code for string input in android...?

查看:714
本文介绍了如何生成的android一个唯一的哈希code字符串输入...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一个唯一的哈希code的字符串中投放机器人。 有pdefined库中的任何$ P $是存在的,或者我们需要手动生成。请任何机构如果知道请present链接或code的东西。

I wanted to generate a unique hash code for a string in put in android. Is there any predefined library is there or we have to generate manually. Please any body if knows please present a link or a code stuff.

推荐答案

这要看你是什么意思:

  • 正如 String.hash code()为您提供了一个32位散列code。

  • As mentioned String.hashCode() gives you a 32 bit hash code.

如果你想(比如说)C你是64位的散列$ C $可以很容易地实现它自己。

If you want (say) a 64-bit hashcode you can easily implement it yourself.

如果你想有一个字符串的加密哈希,Java的加密库包括MD5的实现,SHA-1等。你通常需要将字符串转换为字节数组,然后喂了哈希生成器/消化发电机。例如,看到@Bryan坎普的答案。

If you want a cryptographic hash of a String, the Java crypto libraries include implementations of MD5, SHA-1 and so on. You'll typically need to turn the String into a byte array, and then feed that to the hash generator / digest generator. For example, see @Bryan Kemp's answer.

如果你想有一个保证唯一散code,你的运气了。哈希和散列codeS是唯一的。

If you want a guaranteed unique hash code, you are out of luck. Hashes and hash codes are non-unique.

长Java字符串N具有 65536 ^ N 可能的状态,并需要使用整数16 * N 位重新present所有可能的值。如果你写产生整数与一个较小的范围的哈希函数(如小于 16 * N 位),你最终会发现情况下,多个字符串散列相同整数;即,散列codeS不能是唯一的。这就是所谓的鸽巢原理的,并且有一个直向前数学证明。 (你可以不打数学和赢!)

A Java String of length N has 65536 ^ N possible states, and requires an integer with 16 * N bits to represent all possible values. If you write a hash function that produces integer with a smaller range (e.g. less than 16 * N bits), you will eventually find cases where more than one String hashes to the same integer; i.e. the hash codes cannot be unique. This is called the Pigeonhole Principle, and there is a straight forward mathematical proof. (You can't fight math and win!)

但是,如果可能唯一的一个非常小的几率非唯一性是可以接受的,那么加密哈希是一个很好的答案。数学会告诉你有多大(即有多少位),哈希已是实现一个给定的(足够低)的非唯一性的概率。

But if "probably unique" with a very small chance of non-uniqueness is acceptable, then crypto hashes are a good answer. The math will tell you how big (i.e. how many bits) the hash has to be to achieve a given (low enough) probability of non-uniqueness.

这篇关于如何生成的android一个唯一的哈希code字符串输入...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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