secure.bmx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ' Copyright (c) 2023 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: Random Numbers - Secure
  25. End Rem
  26. Module Random.Secure
  27. ModuleInfo "Version: 1.01"
  28. ModuleInfo "License: zlib"
  29. ModuleInfo "Copyright: 2023 Bruce A Henderson"
  30. ModuleInfo "History: 1.01"
  31. ModuleInfo "History: Fixed use of bmx_secure_next_double()"
  32. ModuleInfo "History: 1.00"
  33. ModuleInfo "History: Initial Release."
  34. Import Random.Core
  35. ?macos
  36. Import "-framework Security"
  37. Import "macos_glue.c"
  38. ?win32
  39. Import "-ladvapi32"
  40. Import "win32_glue.c"
  41. ?linux
  42. Import "linux_glue.c"
  43. ?
  44. Type TSecureRandom Extends TRandom
  45. Private
  46. ?win32
  47. Field context:ULongInt
  48. ?linux
  49. Field fd:Int
  50. ?
  51. Public
  52. Method New()
  53. ?win32
  54. context = bmx_secure_init()
  55. ?linux
  56. fd = bmx_secure_init()
  57. ?
  58. End Method
  59. Method New(seed:Int)
  60. ?win32
  61. context = bmx_secure_init()
  62. ?linux
  63. fd = bmx_secure_init()
  64. ?
  65. End Method
  66. Method RndFloat:Float() Override
  67. Return Float(RndDouble())
  68. End Method
  69. Method RndDouble:Double() Override
  70. ?win32
  71. Return bmx_secure_next_double(context)
  72. ?macos
  73. Return bmx_secure_next_double()
  74. ?linux
  75. Return bmx_secure_next_double(fd)
  76. ?
  77. End Method
  78. Method Rnd:Double(minValue:Double = 1, maxValue:Double = 0) Override
  79. ?win32
  80. If maxValue > minValue Return bmx_secure_next_double(context) * (maxValue - minValue) + minValue
  81. Return bmx_secure_next_double(context) * (minValue - maxValue) + maxValue
  82. ?macos
  83. If maxValue > minValue Return bmx_secure_next_double() * (maxValue - minValue) + minValue
  84. Return bmx_secure_next_double() * (minValue - maxValue) + maxValue
  85. ?linux
  86. If maxValue > minValue Return bmx_secure_next_double(fd) * (maxValue - minValue) + minValue
  87. Return bmx_secure_next_double(fd) * (minValue - maxValue) + maxValue
  88. ?
  89. End Method
  90. Method Rand:Int(minValue:Int, maxValue:Int = 1) Override
  91. Local Range:Double = maxValue - minValue
  92. ?win32
  93. If Range > 0 Return Int( bmx_secure_next_double(context)*(1:Double+Range) )+minValue
  94. Return Int( bmx_secure_next_double(context)*(1:Double-Range) )+maxValue
  95. ?macos
  96. If Range > 0 Return Int( bmx_secure_next_double()*(1:Double+Range) )+minValue
  97. Return Int( bmx_secure_next_double()*(1:Double-Range) )+maxValue
  98. ?linux
  99. If Range > 0 Return Int( bmx_secure_next_double(fd)*(1:Double+Range) )+minValue
  100. Return Int( bmx_secure_next_double(fd)*(1:Double-Range) )+maxValue
  101. ?
  102. End Method
  103. Method SeedRnd(seed:Int) Override
  104. ' no op
  105. End Method
  106. Method RndSeed:Int() Override
  107. Return 0
  108. End Method
  109. Method GetName:String() Override
  110. Return "Secure"
  111. End Method
  112. Method Delete()
  113. ?win32
  114. bmx_secure_destroy(context)
  115. ?linux
  116. bmx_secure_destroy(fd)
  117. ?
  118. End Method
  119. End Type
  120. Private
  121. Type TSecureRandomFactory Extends TRandomFactory
  122. Method New()
  123. Super.New()
  124. Init()
  125. End Method
  126. Method GetName:String()
  127. Return "Secure"
  128. End Method
  129. Method Create:TRandom(seed:Int)
  130. Return New TSecureRandom()
  131. End Method
  132. Method Create:TRandom()
  133. Return New TSecureRandom()
  134. End Method
  135. End Type
  136. Extern
  137. ?macos
  138. Function bmx_secure_next_double:Double()
  139. ?win32
  140. Function bmx_secure_next_double:Double(context:ULongInt)
  141. Function bmx_secure_destroy(context:ULongInt)
  142. Function bmx_secure_init:ULongInt()
  143. ?linux
  144. Function bmx_secure_next_double:Double(fd:Int)
  145. Function bmx_secure_init:Int()
  146. Function bmx_secure_destroy(fd:Int)
  147. ?
  148. End Extern
  149. New TSecureRandomFactory