Adler32很快重复 [英] Adler32 Repeating Very Quickly

查看:282
本文介绍了Adler32很快重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用adler32校验和算法从数据库id生成一个数字。所以,当我插入一行到数据库,我采取该行的身份,并使用它来创建校验和。我遇到的问题是,我只是生成一个重复校验和,只有207插入到数据库。这比我预期的快得多。这是我的代码:

I'm using the adler32 checksum algorithm to generate a number from a database id. So, when I insert a row into the database, I take the identity of that row and use it to create the checksum. The problem that I'm running into is that I just generated a repeat checksum after only 207 inserts into the database. This is much much faster than I expected. Here is my code:

String dbIdStr = Long.toString(dbId);
byte[] bytes = dbIdStr.getBytes();
Checksum checksum = new Adler32();
checksum.update(bytes, 0, bytes.length);
result = checksum.getValue();

我在做什么/怎么办?我应该使用不同的方法来创建唯一的字符串吗?我这样做,因为我不想在一个url中使用db id ...对db结构的改变会破坏世界上所有的链接。

Is there something wrong with what/how I'm doing? Should I be using a different method to create unique strings? I'm doing this because I don't want to use the db id in a url... a change to the structure of the db will break all the links out there in the world.

感谢!

推荐答案

您应该不要使用Adler-32哈希码发生器。这不是它的目的。您应该使用具有良好的哈希属性的算法,这样做可以最小化冲突的概率。

You should not be using Adler-32 as a hash code generator. That's not what it's for. You should use an algorithm that has good hash properties, which, among other things minimizes the probability of collisions.

您可以简单地使用Java的hashCode 方法(对任何对象)。对于String对象,哈希码是字符串值乘以31的连续幂的总和。可能有非常短的字符串的冲突,但它不是一个可怕的算法。

You can simply use Java's hashCode method (on any object). For the String object, the hash code is the sum of the byte values of string times successive powers of 31. There can be collisions with very short strings, but it's not a horrible algorithm. It's definitely a lot better than Adler-32 as a hash algorithm.

使用加密安全哈希函数(像SHA-256)的建议对于你的应用程序来说肯定是过度的,在执行时间和哈希码大小方面。你应该尝试Java的hashCode,看看你得到了多少冲突。如果它看起来比您预期的频率高出一个 2 -n 概率(其中 n 是哈希码中的位数),那么你可以覆盖它与更好的一个。您可以在此处获得体面的Java哈希函数中找到链接。

The suggestions to use a cryptographically secure hash function (like SHA-256) are certainly overkill for your application, both in terms of execution time and hash code size. You should try Java's hashCode and see how many collisions you get. If it seems much more frequent than you'd expect for a 2-n probability (where n is the number of bits in the hash code), then you can override it with a better one. You can find a link here for decent Java hash functions.

这篇关于Adler32很快重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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