algorithm - Map incrementing integer range to six-digit base 26 max, but unpredictably -


I want to design a URL shortener for a particular use case and want to target the end-user of that type I have decided that I want to submit the URL internally according to an auto-infractioning integer key. However, it is also imperative that a key is represented in the URL as a base 6 (AZ) of six digits and it is not possible to estimate that the base 26 URL key is on the increasing integer key is based. In other words, the first URL key should not be virtual, the next time a person creates a URL, it should not be AAIAB, etc., and no loop does not want to see any randomness and fishing in the database. Whether it is repeated or not. / P>

The second part of the requirements (for external URLs to be estimated in an external 26) is a more interesting part. Ideally, I would have to do the same number of all numbers in the 26 ^ 6 range in the same number of algorithms 1-1 mapping in the same number, I can just print in base 26, and that I can undo algorithms Can not do it and when I want to see the URL then it needs to be stored in a separate table. How can I accomplish this?

Why not just assume base 26 values ​​before shuffling bits in a specific order? For example, bit becomes 0 bit 5, bit becomes 1 bit 2, etc. To decode, just do the reverse.

Here is an example in Python. (Edit to include now in changing base.)

  Import random # generates a random bit order # You will need to permanently save this mapping, maybe it Only hardcod # map how ever many bits you mapping your integer space mapping = range (28) Reverse () # linear. To change the basis of the # alphabet to be replaced by 10 characters = 'abcdefghijklmnopqrstuvwxyz' # BIFETE DEF encode (N) must be represented to shuffle: Consequences = 0, mapping for B: B1 = 1 & Lt; & Lt; I have B2 = 1 & lt; & Lt; Mapping [i] if N & amp; B1: Result | = B2 Return result # Beat def decode (N): For i = b, calculate the result in = 0, B (mapping): B1 = 1 & lt; & Lt; I have B2 = 1 & lt; & Lt; Mapping [i] if N & amp; B2: Result | = B1 Return Results # Base Def Nbase (X) Replaces: N = Lane (Characters) If X & LT; N: Return Character [x] Return Nbase (X / N) + Chars [x% n] # Back to 10 DEF DBUS (X): N = For Lane (Characters), Result = 0, Enur in i (Reverse (X)): Results == chars.index (c) * (n ** i) Return Results # This is a test for a limit (200): b = encode (A) C = NbS (B) ) D = DBUS (C) E = Decode (D) while Lane (C) & lt; 7: c = '' + c print '% 6d% 6d% s% 6d% 6d'% (a, b, c, d, e)  

The output of this script, encoding Showing and decoding process:

  0 0 0 1 134217728 lhskyi 134217728 1 2 67108864 Faculty 67108864 2 3 201326592 qyoqkm 201326592 3 4 33554432 cvlctc 33554432 4 5 167772160 Odencr 167772160 5 6 100663296 imhifg 100663296 6 7 234881024 ttztdo 234881024 7 8 16777216 bksojo 16777216 8 9 150994944 mskzhw 150994944 9 10 83886080 hbotvs 83886080 10 11 218103808 sjheua 218103808 11 12 50331648 egdrcq 50331648 12 13 184549376 pnwcay 184549376 13 14 117440512 jwzwou 117440512 14 15 251658240 veshnc 251658240 15 16 8388608 sjheu 8388608 16 17 142606336 mabsdc 142606336 17 18 754 9 7472 gjfmqy 75 497472 18 19 20 9 715200 rqxxpg 20 9 715200 19  

Note that zero The map is at zero, but you can leave that number.

It should be simple, efficient and sufficient for your purposes, if you really want something safe then I would not explicitly recommend it. This is basically a simple block cipher. No conflicts will happen.

Perhaps the best bit is to ensure that bit N sometimes does not map to bits (no changes) and may be best if mapped to some bit-bit bit in input In the output, in other words in general, you want to generate mapping by hand. In fact, a decent mapping will just push the bit order back. (That's what I did for sample output above.)


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -