基于一对字符串生成唯一字符串 [英] Generate a unique string based on a pair of strings

查看:45
本文介绍了基于一对字符串生成唯一字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串 StringA 和 StringB.我想生成一个唯一的字符串来表示这一对.

I've two strings StringA, StringB. I want to generate a unique string to denote this pair.

f(x, y) 对于每个 x, y 和 f(x, y) = f(y, x) 都应该是唯一的,其中 x, y 是字符串.

f(x, y) should be unique for every x, y and f(x, y) = f(y, x) where x, y are strings.

有什么想法吗?

推荐答案

计算两个字符串的消息摘要并对值进行异或

Compute a message digest of both strings and XOR the values

MD5(x) ^ MD5(Y)

消息摘要为您提供每个字符串的唯一值,XOR 使 f(x, y) 可能等于 f(y, x).

The message digest gives you unique value for each string and the XOR makes it possible for f(x, y) to be equal to f(y, x).

正如@Phil H 所观察到的,您必须将收到两个相等字符串作为输入的情况处理,这将在 XOR 之后生成 0.如果 x 和 y 相同,您可以返回类似 MD5(x+y) 的内容,其余值则返回 MD5(x) ^ MD5(y) .

As @Phil H observed, you have to treat the case in which you receive two equal strings as input, which would generate 0 after the XOR. You could return something like an MD5(x+y) if x and y are the same, and MD5(x) ^ MD5(y) for the rest of values.

这篇关于基于一对字符串生成唯一字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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