qubes.GetRandomizedTime 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 library is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU Lesser General Public
  8. # License as published by the Free Software Foundation; either
  9. # version 2.1 of the License, or (at your option) any later version.
  10. #
  11. # This library 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 GNU
  14. # Lesser General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Lesser General Public
  17. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  18. ## Similar code as Boot Clock Randomization.
  19. ## https://www.whonix.org/wiki/Boot_Clock_Randomization
  20. set -e
  21. ## Get a random 0 or 1.
  22. ## Will use this to decide to use plus or minus.
  23. ZERO_OR_ONE="$(shuf -i0-1 -n1 --random-source=/dev/random)"
  24. ## Create a random number between 0 and 180.
  25. DELAY="$(shuf -i0-180 -n1 --random-source=/dev/random)"
  26. ## Create a random number between 0 and 999999999.
  27. ##
  28. ## Thanks to
  29. ## https://stackoverflow.com/questions/22887891/how-can-i-get-a-random-dev-random-number-between-0-and-999999999-in-bash
  30. NANOSECONDS="$(shuf -i0-999999999 -n1 --random-source=/dev/random)"
  31. ## Examples NANOSECONDS:
  32. ## 117752805
  33. ## 38653957
  34. ## Add leading zeros, because `date` expects 9 digits.
  35. NANOSECONDS="$(printf '%0*d\n' 9 "$NANOSECONDS")"
  36. ## Using
  37. ## printf '%0*d\n' 9 "38653957"
  38. ## 38653957
  39. ## becomes
  40. ## 038653957
  41. ## Examples NANOSECONDS:
  42. ## 117752805
  43. ## 038653957
  44. if [ "$ZERO_OR_ONE" = "0" ]; then
  45. PLUS_OR_MINUS="-"
  46. elif [ "$ZERO_OR_ONE" = "1" ]; then
  47. PLUS_OR_MINUS="+"
  48. else
  49. exit 2
  50. fi
  51. #OLD_TIME="$(date)"
  52. #OLD_TIME_NANOSECONDS="$(date +%s.%N)"
  53. OLD_UNIXTIME="$(date +%s)"
  54. NEW_TIME="$(( $OLD_UNIXTIME $PLUS_OR_MINUS $DELAY ))"
  55. NEW_TIME_NANOSECONDS="$NEW_TIME.$NANOSECONDS"
  56. echo "$NEW_TIME_NANOSECONDS"
  57. ## Testing the `date` syntax:
  58. ## date --date @1396733199.112834496
  59. ## date --date "@$NEW_TIME_NANOSECONDS"