Java 将字符串转换为散列,然后稍后重新调整字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4050524/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:58:14  来源:igfitidea点击:

Convert string to hash and then reform the string later

javahash

提问by Steve

I need to hash some strings so I can pass them into some libraries, this is straight forward using the String.hashCode call.

我需要散列一些字符串,以便我可以将它们传递到一些库中,这是使用 String.hashCode 调用直接进行的。

However once everything is processed I'd like to convert the integer generated from the hashCode back into the String value. I could obviously track the string and hashcode values somewhere else and do the conversion there, but I'm wondering is there anything in Java that will do this automatically.

但是,一旦处理完所有内容,我想将从 hashCode 生成的整数转换回 String 值。我显然可以在其他地方跟踪字符串和哈希码值并在那里进行转换,但我想知道 Java 中是否有任何东西可以自动执行此操作。

采纳答案by Carra

I think you misunderstand the concept of a hash. A hash is a one way function. Worse, two strings might generate the same hash.

我认为您误解了哈希的概念。散列是一种单向函数。更糟糕的是,两个字符串可能会生成相同的哈希值。

So no, it's not possible.

所以不,这是不可能的。

回答by aioobe

That is not possible in general. The hashCodeis what one would call a one-way-function.

这在一般情况下是不可能的。这hashCode就是人们所说的单向函数

Besides, there are more strings than integers, so there is a one-to-many mapping from integers to strings. The strings "0-42L"and "0-43-"for instance, have the same hash-code. (Demonstration on ideone.com.)

此外,字符串比整数多,因此整数到字符串是一对多的映射。的字符串"0-42L""0-43-"例如,具有相同的哈希码。(ideone.com 上的演示。

What you coulddo however, (as an estimate), would be to store the strings you pass into the API and remember their hash-codes like this:

但是,您可以做的是(作为估计)将您传递给 API 的字符串存储起来并记住它们的哈希码,如下所示:

import java.util.*;

public class Main {
    public static void main(String[] args) {

        // Keep track of the corresponding strings
        Map<Integer, String> hashedStrings = new HashMap<Integer, String>();

        String str1 = "hello";
        String str2 = "world";

        // Compute hash-code and remember which string that gave rise to it.
        int hc = str1.hashCode();
        hashedStrings.put(hc, str1);

        apiMethod(hc);

        // Get back the string that corresponded to the hc hash code.
        String str = hashedStrings.get(hc);
    }
}

回答by zengr

Not possible to convert the .hashcode()output to the original form. It's a one way process.

无法将.hashcode()输出转换为原始形式。这是一个单向过程。

You can use a base64 encoder schemewhere you will encode the data, use it where ever you want to and then decode it to the original form.

您可以使用base64 编码器方案,您将在其中对数据进行编码,在您想要的任何地方使用它,然后将其解码为原始形式。

回答by uckelman

hashCode()is a not generally going to be a bijection, because it's not generally going to be an injectivemap.

hashCode()是一个通常不会是一个bijection,因为它通常不会是一个单映射。

hashCode()has ints as its range. There are only 2^32 distinct intvalues, so for any object where there there can be more than 2^32 different ones (e.g., think about Long), you are guaranteed (by the pigeonhole principlethat at least two distinct objects will have the same hash code.

hashCode()ints 作为它的范围。只有 2^32 个不同的int值,因此对于可能有超过 2^32个不同值的任何对象(例如,想想Long),您可以保证(根据鸽笼原理,至少两个不同的对象将具有相同哈希码。

The only guarantee that hashCode()gives you is that if a.equals(b), then a.hashCode() == b.hashCode(). Every object having the same hash code is consistent with this.

hashCode()给您的唯一保证是如果a.equals(b),则a.hashCode() == b.hashCode()。每个具有相同哈希码的对象都与此一致。

You canuse the hashCode()to uniquely identify objects in some very limited circumstances: You must have a particular class in where there are no more than 2^32 possible different instances (i.e., there are at most 2^32 objects of your class which pairwise are such that !a.equals(b)). In that case, so long as you ensure that whenever !a.equals(b)and both aand bare objects of your class, that a.hashCode() != b.hashCode(), you will have a bijection between (equivalence classes of) objects and hash codes. (It could be done like this for the Integerclass, for example.)

在某些非常有限的情况下,您可以使用hashCode()来唯一标识对象:您必须有一个特定的类,其中不超过 2^32 个可能的不同实例(即,您的类最多有 2^32 个对象,它们成对存在)这样的!a.equals(b))。在这种情况下,只要你保证只要!a.equals(b)两者ab是你的类的对象,即a.hashCode() != b.hashCode(),你将不得不之间(的等价类)和散列码的双射对象。(例如,可以为Integer班级这样做。)

However, unless you're in this very special case, you should create a unique id some other way.

但是,除非您处于这种非常特殊的情况下,否则您应该以其他方式创建唯一 id。