Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integer codec does not preserve length #2

Closed
dcalde opened this issue Jun 14, 2018 · 1 comment
Closed

Integer codec does not preserve length #2

dcalde opened this issue Jun 14, 2018 · 1 comment

Comments

@dcalde
Copy link

dcalde commented Jun 14, 2018

The Integer codec does not preserve length if the pre-unpack encoded string contains leading 0's as those get stripped by the conversion to int.

i = pyffx.Integer(b'secret-key', length=5)
assert 4 == len(str(i.encrypt(1234)))

class IntegerIrreversable(pyffx.Integer):
    def unpack(self, v, t):
        return int(super(pyffx.Integer, self).unpack(v, t).lstrip('0').ljust(self.length, '9'))

i2 = IntegerIrreversable(b'secret-key', length=5)
for num in range(1000, 9999):
    assert 5 == len(str(i2.encrypt(num)))
@emulbreh
Copy link
Owner

emulbreh commented Jul 3, 2018

The Integer codec is not meant to preserve length: it's for bijections on range(10**(length+1)).
If you want leading zeros to be significant (and preserve length) you can use the String codec instead:

>>> import pyffx, string
>>> c = pyffx.String('secret', alphabet=string.digits, length=5)
>>> c.encrypt('00001')
'05465'
>>> c.decrypt('05465')
'00001'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants