optimus
optimus copied to clipboard
Generated number length?
Is it possible to control the generated number length?
Eg. say I need 7-digits unique numbers, generated based on some integer eg.
1 --> 1234567
23 --> 7654321
100 --> 0112110
?
I'm interested also to this. Is it possible to generate fixed length numbers or at least know the length of the number in terms of range?
@mtangoo I actually created my own library for this loostro/cryptomute - an implementation of Format Preserving Encryption based on Feistel Ciphers.
The maximum value is controlled by the const MAX_INT = 2147483647;
value. But there is no easy way of overwriting that value at this moment.
But you could try extending the class and overwriting the MAX_INT value.
Could you also elaborate on the use case?
Suppose you generate something like Student ID. You want all student IDs to have same length in digits despite their real IDs. Another example might be a bank account @jenssegers
@loostro If I happen to have a Billion IDs will there be a collision with your library?
@mtangoo no, there will be none. Read on Formart Preserving Encryption
@loostro thanks.
You may do:
$myId = str_pad($optimusId, 7, 0, STR_PAD_LEFT);
any updates on this?
PR #36 introduces flexibility around this.
Note that from what I could see, the maths for generating the integers requires a binary numbers. This means you could restrict the numbers to 65535 but not 99999.
For anyone who desperately needs more flexibility, a similar strategy is described at A practical use of multiplicative inverses which uses Modulus rather than a Bitwise And to shift the value. I found in testing that this method allows any int to be used as a max. However it also requires the use of co-prime rather than a prime. So the strategy is not compatible with Optimus.