qubes.GetRandomizedTime 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # The Qubes OS Project, http://www.qubes-os.org
  3. #
  4. # Copyright (C) 2016 Patrick Schleizer <adrelanos@riseup.net>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. ## Similar code as Boot Clock Randomization.
  20. ## https://www.whonix.org/wiki/Boot_Clock_Randomization
  21. set -e
  22. ## Get a random 0 or 1.
  23. ## Will use this to decide to use plus or minus.
  24. ZERO_OR_ONE="$(shuf -i0-1 -n1 --random-source=/dev/random)"
  25. ## Create a random number between 0 and 180.
  26. DELAY="$(shuf -i0-180 -n1 --random-source=/dev/random)"
  27. ## Create a random number between 0 and 999999999.
  28. ##
  29. ## Thanks to
  30. ## https://stackoverflow.com/questions/22887891/how-can-i-get-a-random-dev-random-number-between-0-and-999999999-in-bash
  31. NANOSECONDS="$(shuf -i0-999999999 -n1 --random-source=/dev/random)"
  32. ## Examples NANOSECONDS:
  33. ## 117752805
  34. ## 38653957
  35. ## Add leading zeros, because `date` expects 9 digits.
  36. NANOSECONDS="$(printf '%0*d\n' 9 "$NANOSECONDS")"
  37. ## Using
  38. ## printf '%0*d\n' 9 "38653957"
  39. ## 38653957
  40. ## becomes
  41. ## 038653957
  42. ## Examples NANOSECONDS:
  43. ## 117752805
  44. ## 038653957
  45. if [ "$ZERO_OR_ONE" = "0" ]; then
  46. PLUS_OR_MINUS="-"
  47. elif [ "$ZERO_OR_ONE" = "1" ]; then
  48. PLUS_OR_MINUS="+"
  49. else
  50. exit 2
  51. fi
  52. #OLD_TIME="$(date)"
  53. #OLD_TIME_NANOSECONDS="$(date +%s.%N)"
  54. OLD_UNIXTIME="$(date +%s)"
  55. NEW_TIME="$(( $OLD_UNIXTIME $PLUS_OR_MINUS $DELAY ))"
  56. NEW_TIME_NANOSECONDS="$NEW_TIME.$NANOSECONDS"
  57. echo "$NEW_TIME_NANOSECONDS"
  58. ## Testing the `date` syntax:
  59. ## date --date @1396733199.112834496
  60. ## date --date "@$NEW_TIME_NANOSECONDS"