Ooops
November 28, 2020, 11:20:52 AM

Author Topic: [bb] Xor Checksumming by Vic 3 Babes [ 1+ years ago ]  (Read 712 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Xor Checksumming by Vic 3 Babes [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Xor Checksumming
Author : Vic 3 Babes
Posted : 1+ years ago

Description : Xor gets a bit of a rough deal in the Command Index - it's pretty useful for a few things, one of which is checksumming external files.

This example code shows how to xor checksum a bank to ensure no file-corruption for I/O error checking, or tampering has occurred - such as on highscore tables/graphics files.

It's better to load a file into a bank and xor checksum it, rather than doing it on disk.


Code :
Code: BlitzBasic
  1. ;Use Xor Checksums to check for external file-corruption
  2. ;or tampering - much easier to checksum a prefs file than
  3. ;test the validity of every variable that it might hold
  4. ;when you reload it.
  5. AppTitle "Xor Checksumming"
  6. Graphics 400,400,0,2
  7.  
  8. Const RANDLOW=$1234, RANDHIGH=$56789ABC
  9. Const SIZEOFBANK=2000
  10.  
  11. Global bank=CreateBank(SIZEOFBANK)
  12.  
  13. SetBuffer BackBuffer()
  14. SeedRnd MilliSecs()
  15.  
  16. ;fill example bank with random numbers
  17. For offset=0 To SIZEOFBANK-4 Step 4
  18.         PokeInt bank,offset,Rand($FFFFFFFF,$7FFFFFFF)
  19. Next
  20.  
  21. ;get a checksum from the bank - but ignore the last
  22. ;four bytes.
  23.  
  24. checksum=PeekInt(bank,0)
  25. For offset=4 To SIZEOFBANK-8 Step 4
  26.         checksum=checksum Xor PeekInt(bank,offset)
  27. Next
  28.  
  29. ;use the checksum as a random seed
  30. SeedRnd checksum
  31. ;generate a random number from this seed
  32. ;in the given range
  33. checksum=Rand(RANDLOW, RANDHIGH)
  34. ;poke it into the last 4 bytes of the bank
  35. PokeInt bank,SIZEOFBANK-4,checksum
  36. Text 0,0,"Random number from checksum seed: "+checksum
  37.  
  38. Text 0,30,"Press a key to see a demo of a seed bug"
  39. Flip
  40. WaitKey()
  41.  
  42. ;Now when you load your bank back in later, you
  43. ;can simply reaquire the checksum with the second
  44. ;For loop above - use it for the seed with SeedRnd.
  45. ;Generate a random number between RANDLOW and RANDHIGH
  46. ;and compare it with the random number you stuck at
  47. ;the end of the bank - or wherever you stuck it.
  48. ;This method guards against tampering as well as
  49. ;file corruption.
  50. ;
  51. ;That's it for the algorithm - now a couple of things
  52. ;to be aware of:
  53. ;
  54. ;1.     you can use byte or short checksumming, but make
  55. ;       sure your bank is divisible by 2 or 4 if using
  56. ;       shorts/integers - or take it into account when
  57. ;       checksumming
  58. ;2.     Blitz2D and Blitz+ don't generate the same sequence
  59. ;       of random numbers from the same seeds.
  60. ;3.     If you game/program relies on random numbers, you
  61. ;       might want to preserve the seed with RndSeed or
  62. ;       randomize it again with SeedRnd - because if your
  63. ;       bank is always the same, then the same random
  64. ;       numbers will be generated after loading.
  65. ;4.     seed bug as at 01 Feb 07 - may have been fixed by
  66. ;       the time you read this.
  67. ;       Seeds of $FFFFFFFF or $7FFFFFFF always generate n1
  68. ;       in Rand(n1,n2)
  69. Cls
  70. Text 0,0,"Print 15 random numbers between 10 and 50"
  71. SeedRnd $7FFFFFFF
  72. For randnum=1 To 15
  73.         Text 0,randnum Shl 4,Rand(10,50)
  74. Next
  75. Text 0,260,"Press a key to end"
  76. ;since one number is the highest positive integer
  77. ;and the other is the lowest, I guess it's an
  78. ;overflow thing
  79. Flip
  80. WaitKey
  81. End


Comments :


Graythe(Posted 1+ years ago)

 Thanks - this really helped


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal