Home > Net >  Is there any way to check a date is correct (under no tampering attempt) without any connectivity?
Is there any way to check a date is correct (under no tampering attempt) without any connectivity?

Time:10-15

I'm developing an app where I get to retrieve current date in several places, by using new Date(), I'd like that if the user has changed the date in the phone it's not allowed to take the date as it's wrong.

Due to the nature of the application, it's possible that when the operation that takes the date is done, there may be no internet connection, so instead of dealing with something like an NTP server, we can more and less ensure that the date is correct if the user has options Use network date/time and Use network timezone enabled and he has connectivity to telephony network (sure, if may be possible to tamper the date but it looks secure enough).

Problem is that it may be also be possible that there may be no telephony network where the phone is being used and I've checked the following:

  1. You can disable Use network date/time and set a date of your liking.

  2. You activate airplane mode (or similar no connectivity situation)

  3. You restart the phone.

  4. You enable Use network date/time.

Now, despite having set Use network date/time and Use network timezone, there's an incorrect date on the phone. Indeed, if I quit airplane mode date is properly set again.

Maybe a situation like the previous could be detected with something like a service that checks that the user has restarted the phone, and then a service at phone startup that determines date is taken again properly, so then app checks that service says that it's properly taken and so it allows the user to take it... But all this seems hard to do and we may bug honest users that had to restart their phones in a zone with no telephony connectivity and then try to use the app without trying to trick other date.

So, any idea on a not too complicated method and no bugging for honest users that could achieve getting the date with no connectivity of any class? Or is this impossible?

CodePudding user response:

In order to get system time in java you could use the System.currentTimeMillis(), although this module synchronizes with the phone time and date. In order to get system time without relation to the phone date and time in java you could use the System.nanoTime() which is independent to phone clock.

However the nanoTime module is not absolute and measures in relation to some arbitrary point, so you should save some value of nanoTime in the code where it is a known date, and compare to it.

See how to format the time from nanoSeconds to date in this article.

Related article about nanoTime.

CodePudding user response:

You are talking about

System.currentTimeMillis(). 
// user can change it by adjusting the DATE of the phone.

But this can help you.

SystemClock.elapsedRealtime()

Difference between these twos can help you.

https://developer.android.com/reference/android/os/SystemClock

  • After coding, You should convert miliseconds to Date
  • Related