xarenard / simpleotp

Simple OTP implementation in node.js according to rfc 4996 and rfc 6238
MIT License
9 stars 3 forks source link

Token code not zero-padded to correct length #1

Closed conormkelly closed 4 years ago

conormkelly commented 4 years ago

https://github.com/xarenard/simpleotp/blob/c974078900aeac35676acf9448bef1ce1f5f0e64/lib/otp.js#L76

Hi @xarenard ,

I noticed that tokens that begin with a leading 0 are sometimes not the correct length.

The problem is that the if statement in the referenced line will only add one zero. If there are two or more leading zeros required, the generated code is not the correct length.

// Sample rfc_6238 test data - should be of length "8"

const testDatas = [
  // ...
  { seconds: 3060, algorithm: 'sha1', expected_token: '00629694' },
  { seconds: 4620, algorithm: 'sha256', expected_token: '00836417' },
  { seconds: 5100, algorithm: 'sha512', expected_token: '00458766' }
]

I can submit a PR if you'd like, but it should just be a matter of updating this if statement to a while loop.

xarenard commented 4 years ago

Hi,

Thanks for the feedback.I will have a look. Indeed no need for a PR

Cheers

Xavier

xarenard commented 4 years ago

An updated version has been published (version 0.7.7).Let me know if it does solve your issue.

conormkelly commented 4 years ago

Yes - looks good. Thank you for resolving this so quickly. Much appreciated!