如何设计一个连续的哈希函数一样 [英] How to design a sequential hash-like function

查看:148
本文介绍了如何设计一个连续的哈希函数一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开发类似的东西的jsfiddle在用户可以输入一些数据,然后保存,并得到一个唯一的随机找的网址加载数据。

I want to develop something similar to jsfiddle in where the user can input some data and then "save" it and get a unique random looking url that loads that data.

我不想让节约的顺序,因为我不想让任何人抢我所有的作品中,一些可以是私有的。但是在服务器上,我想将它保存在顺序。

I don't want to make the saves sequential because I don't want anyone to grab all of my entries, as some can be private. However on the server I would like to save it in sequential order.

有一个功能或技术,可将数字转换成一个散列,有4 charactors没有任何冲突,直到(62 * 62 * 62 * 62 === 14776336)项?

Is there a function or technique that converts a number into a hash that has 4 charactors without any collisions until (62 * 62 * 62 * 62 === 14776336) entries?

例如在服务器上的第一个条目将被命名为 1 服务器,但 iUew3 用户上,下一个项目将是 2 在服务器上,但 ueGR 用户...

For example the first entry on the server will be named 1 on the server but iUew3 to the user, the next entry will be 2 on the server but ueGR to the user...

编辑:我不知道这是否是显而易见的,但这个散列般的功能必须是可逆的,因为当用户请求 ueGR 服务器需要知道服务器时,它文件 2

I'm not sure if it's obvious but this hash-like function needs to be reversible because when the user requests ueGR the server needs to know to server it file 2

推荐答案

这是可能做到这一点,但我会建议使用64个字符,因为这将使它轻松了许多。 4 6位字符= 24位。

It's possible to do this, but I would suggest using 64 characters, as that will make it a lot easier. 4 6bit characters = 24bits.

使用它们的组合:

  • bit reordering
  • xor with a number
  • put it into a 24bit maximal length LFSR and do a couple of cycles.

LFSR强烈推荐,因为它会做一个很好的扰码。其余的都是可选的。所有这些操作都是可逆并保证每个输出将是唯一

LFSR is highly recommended as it will do a good scrambling. The rest are optional. All of these manipulations are reversible and guarantee that each output is going to be unique.

在计算出的洗牌号只需将其打包为一个二进制字符串和连接code用 base64_en code

When you calculated the "shuffled" number simply pack it to a binary string and encode it with base64_encode.

有关解码简单地做这些操作的逆。

For decoding simply do the inverse of these operations.

样品(2 ^ 24长的唯一序列):

Sample (2^24 long unique sequence):

function lfsr($x) {
    return ($x >> 1) ^ (($x&1) ? 0xe10000 : 0);
}
function to_4($x) {
    for($i=0;$i<24;$i++)
        $x = lfsr($x);
    $str = pack("CCC", $x >> 16, ($x >> 8) & 0xff, $x & 0xff);
    return base64_encode($str);
}

function rev_lfsr($x) {
    $bit = $x & 0x800000;
    $x = $x ^ ($bit ? 0xe10000 : 0);
    return ($x << 1) + ($bit ? 1 : 0);
}
function from_4($str) {
    $str = base64_decode($str);
    $x = unpack("C*", $str);
    $x = $x[1]*65536 + $x[2] * 256 + $x[3];
    for($i=0;$i<24;$i++)
        $x = rev_lfsr($x);
    return $x;
}

for($i=0; $i<256; $i++) {
    $enc = to_4($i);
    echo $enc . " " . from_4($enc) . "\n";
}

输出:

AAAA 0
kgQB 1
5ggD 2
dAwC 3
DhAH 4
nBQG 5
6BgE 6
ehwF 7
HCAO 8
jiQP 9
+igN 10
aCwM 11
EjAJ 12
gDQI 13
9DgK 14
ZjwL 15
OEAc 16
qkQd 17
3kgf 18
TEwe 19
NlAb 20
pFQa 21
0FgY 22

...

注:网址替换 + / - _

注:虽然这个工程,对于一个简单的场景像你这样的,它可能更容易地创建一个随机文件名,直到它不存在。无人问津的条目的数量。

Note: although this works, for a simple scenario like yours it's probably easier to create a random filename, till it doesn't exist. nobody cares about the number of the entry.

这篇关于如何设计一个连续的哈希函数一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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