Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to design a sequential hash-like function

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.

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?

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...

EDIT: 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

like image 987
qwertymk Avatar asked Mar 21 '12 11:03

qwertymk


People also ask

What is sequential hashing?

We propose SeqHash, a sequential hashing scheme that constructs reversible multi-level hash arrays for fast and accurate detection of heavy keys while incurring a small increase in memory usage with respect to the derived lower bound.

How do you create a hash function?

With modular hashing, the hash function is simply h(k) = k mod m for some m (usually, the number of buckets). The value k is an integer hash code generated from the key. If m is a power of two (i.e., m=2p), then h(k) is just the p lowest-order bits of k.

What are the most important considerations when designing a hash function?

In general, a hash function should depend on every single bit of the key, so that two keys that differ in only one bit or one group of bits (regardless of whether the group is at the beginning, end, or middle of the key or present throughout the key) hash into different values.


2 Answers

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.

Use a combination of these:

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

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.

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.

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";
}

Output:

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: for URL replace + and / with - and _.

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.

like image 191
Karoly Horvath Avatar answered Sep 25 '22 12:09

Karoly Horvath


In my opinion if you also keeping the save time of entry on server, you can generate a hash function. hash = func(id, time) but with only hash = func(id) gonna be to easy to resolve

like image 31
safarov Avatar answered Sep 25 '22 12:09

safarov