xxh3digest.bmx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. '
  2. ' Copyright (c) 2024 Bruce A Henderson
  3. '
  4. ' Redistribution and use in source and binary forms, with or without
  5. ' modification, are permitted provided that the following conditions are met:
  6. '
  7. ' 1. Redistributions of source code must retain the above copyright notice, this
  8. ' list of conditions and the following disclaimer.
  9. '
  10. ' 2. Redistributions in binary form must reproduce the above copyright notice,
  11. ' this list of conditions and the following disclaimer in the documentation
  12. ' and/or other materials provided with the distribution.
  13. '
  14. ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. ' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. ' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. ' FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ' DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. ' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. ' CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. ' OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. ' OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. '
  25. SuperStrict
  26. Rem
  27. bbdoc: A digest implementation for the XXH3 64-bit hash algorithm.
  28. about: The XXH3 hash algorithm is a fast, non-cryptographic hash algorithm.
  29. End Rem
  30. Module Crypto.XXH3Digest
  31. Import "common.bmx"
  32. New TXXH3DigestRegister
  33. Rem
  34. bbdoc: A digest implementation for the XXH3 hash algorithm.
  35. End Rem
  36. Type TXXH3Digest Extends TMessageDigest
  37. Method New()
  38. digestPtr = bmx_digest_xxh3_init()
  39. End Method
  40. Method OutBytes:Int() Override
  41. Return 8
  42. End Method
  43. Rem
  44. bbdoc: Resets the calculation to its initial state.
  45. about: This is useful if you want to reuse the same instance for multiple calculations.
  46. End Rem
  47. Method Reset()
  48. bmx_digest_xxh3_reset(digestPtr)
  49. End Method
  50. Rem
  51. bbdoc: Updates the calculation with @dataLen bytes of data.
  52. End Rem
  53. Method Update:Int(data:Byte Ptr, dataLen:Int) Override
  54. bmx_digest_xxh3_update(digestPtr, data, dataLen)
  55. End Method
  56. Rem
  57. bbdoc: Calculates the XXH3 hash for the given #String, setting the value in @result.
  58. End Rem
  59. Method Digest(txt:String, result:ULong Var)
  60. result = txt.Hash() ' we already have a hash function for strings which uses XXH3
  61. End Method
  62. Rem
  63. bbdoc: Calculates the XXH3 hash for the given #TStream, setting the value in @result.
  64. End Rem
  65. Method Digest(stream:TStream, result:ULong Var)
  66. Local buf:Byte[8192]
  67. Reset() ' always reset before calculating a new hash
  68. Local bytesRead:Long
  69. Repeat
  70. bytesRead = stream.Read(buf, buf.length)
  71. If bytesRead > 0 Then
  72. Update(buf, Int(bytesRead))
  73. End If
  74. Until bytesRead <= 0
  75. If Not bytesRead Then
  76. Finish(result)
  77. End If
  78. End Method
  79. Rem
  80. bbdoc: Finishes calculation and produces the result, filling @result with the calculated bytes.
  81. about: The state is reset, ready to create a new calculation.
  82. End Rem
  83. Method Finish:Int(result:Byte[]) Override
  84. Assert result.length >= 8, "Byte array must be at least 8 bytes."
  85. bmx_digest_xxh3_finish(digestPtr, result, 8)
  86. End Method
  87. Rem
  88. bbdoc: Finishes calculation and produces the result, setting @result with the result.
  89. about: The state is reset, ready to create a new calculation.
  90. End Rem
  91. Method Finish:Int(result:ULong Var)
  92. bmx_digest_xxh3_finish_ulong(digestPtr, result)
  93. End Method
  94. Method Delete()
  95. If digestPtr Then
  96. bmx_digest_xxh3_free(digestPtr)
  97. digestPtr = Null
  98. End If
  99. End Method
  100. End Type
  101. Type TXXH3DigestRegister Extends TDigestRegister
  102. Method GetDigest:TMessageDigest( name:String ) Override
  103. If name.ToUpper() = "XXH3" Then
  104. Return New TXXH3Digest
  105. End If
  106. End Method
  107. Method ToString:String() Override
  108. Return "XXH3"
  109. End Method
  110. End Type