optimus icon indicating copy to clipboard operation
optimus copied to clipboard

Generated number length?

Open ioleo opened this issue 9 years ago • 11 comments

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

?

ioleo avatar Feb 09 '16 12:02 ioleo

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 avatar Mar 02 '16 07:03 mtangoo

@mtangoo I actually created my own library for this loostro/cryptomute - an implementation of Format Preserving Encryption based on Feistel Ciphers.

ioleo avatar Mar 02 '16 08:03 ioleo

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.

jenssegers avatar Mar 02 '16 08:03 jenssegers

Could you also elaborate on the use case?

jenssegers avatar Mar 02 '16 08:03 jenssegers

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

mtangoo avatar Mar 02 '16 10:03 mtangoo

@loostro If I happen to have a Billion IDs will there be a collision with your library?

mtangoo avatar Mar 02 '16 10:03 mtangoo

@mtangoo no, there will be none. Read on Formart Preserving Encryption

ioleo avatar Mar 02 '16 11:03 ioleo

@loostro thanks.

mtangoo avatar Mar 02 '16 18:03 mtangoo

You may do:

$myId = str_pad($optimusId, 7, 0, STR_PAD_LEFT);

EthraZa avatar Jul 07 '16 13:07 EthraZa

any updates on this?

jonagoldman avatar Oct 14 '17 17:10 jonagoldman

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.

courtney-miles avatar Mar 01 '18 01:03 courtney-miles