Adler32非常重复 [英] Adler32 Repeating Very Quickly

查看:347
本文介绍了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的总和。可能存在非常短的字符串的冲突,但这不是一个可怕的算法。这绝对比Adler-32好一点哈希算法。

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天全站免登陆