S
S
Sergey Sokolov2017-09-01 17:48:04
Encryption
Sergey Sokolov, 2017-09-01 17:48:04

How to set a password to shuffle 32 elements?

N elements (up to 32) must be mixed with each other. For example:

было:  0,1,2,3,4,5,6,7
стало: 0,4,2,6,1,5,3,7

How to unambiguously obtain this mixing order from a string (password) of any length?
That is, for example, to get the order from a string and length:
f( 'суперпароль', 8) -> [0,4,2,6,1,5,3,7]
f( 'asdf123', 8)     -> [1,3,2,7,5,0,4,6]

NB It is known from combinatorics that 32 elements can be mixed in 32!a number of ways: quite a large number of the order of 2.63*10 35 .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-09-01
@sergiks

If you do not need cryptographic strength, then ...
1. Convert (uniquely) to a very long number.
2. We get such parts of this number.
• Remainder of division by 32
• Incomplete quotient by 32, then remainder by 31
• Incomplete quotient by 32 31, then remainder by 30
• Incomplete quotient by 32 31 30, then remainder by 29…
Scary in words, the algorithm is simple.
3. Of the 31 numbers - the first from 0 to 31, the second from 0 to 30, the last 0 or 1 - it's easy to get a permutation.
If you still need cryptographic strength, you will have to “salt” the password to a sufficient length and encrypt it with something.

I
Ivan Bogachev, 2017-09-01
@sfi0zy

N elements (up to 32).... uniquely from a string (password) of any length

I think you can do it using sha-256 (for 32 elements just right):
let permutation = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];

sha256.array('суперпароль').forEach((n, i) => {
    let j = n % (permutation.length - i) + i;
    
    [permutation[i], permutation[j]] = [permutation[j], permutation[i]];
});

console.log(permutation); // [19, 30, 25, 16, 23, 8, 11, 27, 13, 6, 4, 26, 21, 28, 24, 14, 2, 5, 15, 18, 10, 0, 7, 22, 31, 9, 17, 29, 20, 3, 12, 1]

code pen

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question