Leseratte10 / acsm-calibre-plugin

Calibre plugin for ACSM->EPUB and ACSM->PDF conversion.
https://www.mobileread.com/forums/showthread.php?t=341975
GNU General Public License v3.0
600 stars 23 forks source link

Calculate Nonce #27

Closed JJTech0130 closed 1 year ago

JJTech0130 commented 1 year ago

I'm implementing this in Lua (for real this time :) so that I can make a KOReader plugin. I've gotten through signing in, and now I need to do activation. The one thing that's tripping me up is calculating this nonce:

dt = datetime.utcnow()
usec = dt.microsecond
sec = (dt - datetime(1970,1,1)).total_seconds()

Ntime = int(int(sec * 1000) + usec/1000)

I think I get the part about converting it to Gregorian after that, and it looks like you're just calculating the current unix timestamp, right? I don't get the whole usec thing though... why are you converting the unix timestamp to μs, and then adding the current μs divided by 1000 to it?

Leseratte10 commented 1 year ago

The end result (the Ntime) is supposed to be milliseconds since 1970. However, the datetime object only supports total_seconds, not something like total_milliseconds.

So I multiply the seconds by 1000 to get milliseconds, but then I need to add the remaining 0-999 milliseconds from within the current second, and I get these by taking the current microsecond counter (from 0 to 999999) and dividing that by thousand.

Basically, all these four lines of code are doing is getting the amount of milliseconds since 1970.

JJTech0130 commented 1 year ago

Ah, that makes sense. But isn't total_seconds a decimal? When I put it into an interpreter I get 1657554769.363188 - so you could just multiply it, right?

Part of my confusion was that I mixed up micro and milli seconds a bit 😅, it makes somewhat more sense now.

JJTech0130 commented 1 year ago

I'll close this issue, as I think I have enough understanding of it to write it in lua.

Leseratte10 commented 1 year ago

Hm, looking at it again, my nonce calculation code still seems to be wrong. Given that the nonce's only purpose seems to be blocking replay attacks it probably doesn't matter, but still, something is wrong about my code.

You are absolutely correct, I can just multiply it. I guess I didn't notice that because I didn't implement that function from scratch, I basically ported it from C. I'm going to fix that, thanks for the report.