Let's generate several keys to be scrambling with: import random Armed with that we can calculate scramble1(8031810103, 3319920713, string_to_number("vkunmhd")) to find out that vkunmhd came from 4. ![]() Thanks to Fermat's little theorem we know for p prime and 1 <= k < p that (k * k^(p-2)) % p = 1. They just have to guess the prime and algorithm that we used, look at 2 consecutive values to get the hidden parameter, then look at a couple of more to verify it.īefore addressing that, let's figure out how to take a string and get the number back. ![]() But will be reversible for any knowledgeable someone who puts modest effort in. Picking a random number to scramble by, int(random.random() * 26**7) happened to give me 3661807866, we get a sequence we can calculate with: for i in range(1, 5): Which is to use the fact that multiplication modulo a prime scrambles the numbers in the range 1.(p-1). Now there is an easy way to scramble them. That's how many numbers we'll be able to handle. With this we find that we can use the prime 8031810103. Print(last_prime_before(len(alphabet)**len_of_codes) (Because you know how to turn those into strings.) Well, there are lots of tricks for primes, so let's find a decent sized prime that fits in the range that you want. So now your problem is how to take an ascending stream of numbers and get an apparently random stream of numbers out of it instead. alphabet = 'abcdefghijklmnopqrstuvwxyz'Ĭhars.append(alphabet) ![]() I'm going to outline a solution for you that is going to resist casual inspection even by a knowledgeable person, though it probably IS NOT cryptographically secure.įirst, your strings and numbers are in a one-to-one map. One option is to populate a table/dictionary with millions of strings, shuffle them and keep track of index to that table but it takes a lot of memory.Īn option is also to check the database of existing IDs after generating a random string to make sure it doesn't exist but the problem is as I get closer to the K (26^n) the chance of collision increases and it wouldn't be efficient to make a lot of check_if_exist queries against the database.Īlso if n was long enough I could use UUID with small chance of collision but in my case n is 7. The other problem with this code is that the strings that are created after each other look very similar and I need them to look random. For example generate 1 million unique strings today and 2 million tomorrow without looping through or collision with the first 1 million. The problem with this code is there is no index that I can use to generate unique strings on demand by tracking that index. This is my current code in Python: chars = "abcdefghijklmnopqrstuvwxyz"įor item in itertools.product(chars, repeat=n): (So that users can not predict the pattern and the next IDs) For example they are not abcdef1 and abcdef2. ![]() Two strings generated by number i and i+1 don't look similar most of the times.K is the number of possible strings = 26^n. In other words as long as i,jdifferent numbers can not generate same string.I have an application that is kind of like a URL shortener and need to generate unique URL whenever a user requests.įor this I need a function to map an index/number to a unique string of length n with two requirements:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |