Small tests i share because why not
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

416 строки
15 KiB

5 лет назад
  1. require "openssl"
  2. require "benchmark"
  3. require "digest"
  4. require "big"
  5. def to_hex(decimal)
  6. decimal_remainder = decimal % 16
  7. hexchars = "0123456789abcdef"
  8. div = decimal / 16
  9. if div == 0
  10. hexchars[decimal_remainder].to_s
  11. else
  12. to_hex(div) + hexchars[decimal_remainder].to_s
  13. end
  14. end
  15. class CRC
  16. # The initial value of the CRC checksum
  17. INIT_CRC = 0x00
  18. # The XOR mask to apply to the resulting CRC checksum
  19. XOR_MASK = 0x00
  20. # The bit width of the CRC checksum
  21. WIDTH = 0
  22. # Default place holder CRC table
  23. TABLE = ([] of UInt64).freeze
  24. #
  25. # Calculates the CRC checksum.
  26. #
  27. # @param [String] data
  28. # The given data.
  29. #
  30. # @return [Integer]
  31. # The CRC checksum.
  32. #
  33. def self.checksum(data)
  34. crc = self.new
  35. crc << data
  36. return crc.checksum
  37. end
  38. #
  39. # Packs the given CRC checksum.
  40. #
  41. # @return [String]
  42. # The packed CRC checksum.
  43. #
  44. def self.pack(crc)
  45. '\0'
  46. end
  47. #
  48. # Initializes the CRC checksum.
  49. #
  50. def initialize
  51. end
  52. #
  53. # The input block length.
  54. #
  55. # @return [1]
  56. #
  57. def block_length
  58. 1
  59. end
  60. #
  61. # The length of the digest.
  62. #
  63. # @return [Integer]
  64. # The length in bytes.
  65. #
  66. def digest_length
  67. (@width / 8.0).ceil
  68. end
  69. #
  70. # Updates the CRC checksum with the given data.
  71. #
  72. # @param [String] data
  73. # The data to update the CRC checksum with.
  74. #
  75. def update(data)
  76. end
  77. #
  78. # @see {#update}
  79. #
  80. def <<(data)
  81. update(data)
  82. return self
  83. end
  84. #
  85. # Resets the CRC checksum.
  86. #
  87. # @return [Integer]
  88. # The default value of the CRC checksum.
  89. #
  90. def reset
  91. @crc = @@INIT_CRC
  92. end
  93. #
  94. # The resulting CRC checksum.
  95. #
  96. # @return [Integer]
  97. # The resulting CRC checksum.
  98. #
  99. def checksum
  100. @crc ^ @@XOR_MASK
  101. end
  102. #
  103. # Finishes the CRC checksum calculation.
  104. #
  105. # @see {pack}
  106. #
  107. def finish
  108. self.class.pack(checksum)
  109. end
  110. end
  111. class CRC64 < CRC
  112. @@WIDTH = 64_u64
  113. @@INIT_CRC : UInt64 = 0x0000000000000000_u64
  114. @@XOR_MASK : UInt64 = 0x0000000000000000_u64
  115. # Generated by `./pycrc.py --algorithm=table-driven --model=crc-64 --generate=c`
  116. @@TABLE : Array(UInt64) = [
  117. 0x0000000000000000_u64, 0x01b0000000000000_u64, 0x0360000000000000_u64, 0x02d0000000000000_u64,
  118. 0x06c0000000000000_u64, 0x0770000000000000_u64, 0x05a0000000000000_u64, 0x0410000000000000_u64,
  119. 0x0d80000000000000_u64, 0x0c30000000000000_u64, 0x0ee0000000000000_u64, 0x0f50000000000000_u64,
  120. 0x0b40000000000000_u64, 0x0af0000000000000_u64, 0x0820000000000000_u64, 0x0990000000000000_u64,
  121. 0x1b00000000000000_u64, 0x1ab0000000000000_u64, 0x1860000000000000_u64, 0x19d0000000000000_u64,
  122. 0x1dc0000000000000_u64, 0x1c70000000000000_u64, 0x1ea0000000000000_u64, 0x1f10000000000000_u64,
  123. 0x1680000000000000_u64, 0x1730000000000000_u64, 0x15e0000000000000_u64, 0x1450000000000000_u64,
  124. 0x1040000000000000_u64, 0x11f0000000000000_u64, 0x1320000000000000_u64, 0x1290000000000000_u64,
  125. 0x3600000000000000_u64, 0x37b0000000000000_u64, 0x3560000000000000_u64, 0x34d0000000000000_u64,
  126. 0x30c0000000000000_u64, 0x3170000000000000_u64, 0x33a0000000000000_u64, 0x3210000000000000_u64,
  127. 0x3b80000000000000_u64, 0x3a30000000000000_u64, 0x38e0000000000000_u64, 0x3950000000000000_u64,
  128. 0x3d40000000000000_u64, 0x3cf0000000000000_u64, 0x3e20000000000000_u64, 0x3f90000000000000_u64,
  129. 0x2d00000000000000_u64, 0x2cb0000000000000_u64, 0x2e60000000000000_u64, 0x2fd0000000000000_u64,
  130. 0x2bc0000000000000_u64, 0x2a70000000000000_u64, 0x28a0000000000000_u64, 0x2910000000000000_u64,
  131. 0x2080000000000000_u64, 0x2130000000000000_u64, 0x23e0000000000000_u64, 0x2250000000000000_u64,
  132. 0x2640000000000000_u64, 0x27f0000000000000_u64, 0x2520000000000000_u64, 0x2490000000000000_u64,
  133. 0x6c00000000000000_u64, 0x6db0000000000000_u64, 0x6f60000000000000_u64, 0x6ed0000000000000_u64,
  134. 0x6ac0000000000000_u64, 0x6b70000000000000_u64, 0x69a0000000000000_u64, 0x6810000000000000_u64,
  135. 0x6180000000000000_u64, 0x6030000000000000_u64, 0x62e0000000000000_u64, 0x6350000000000000_u64,
  136. 0x6740000000000000_u64, 0x66f0000000000000_u64, 0x6420000000000000_u64, 0x6590000000000000_u64,
  137. 0x7700000000000000_u64, 0x76b0000000000000_u64, 0x7460000000000000_u64, 0x75d0000000000000_u64,
  138. 0x71c0000000000000_u64, 0x7070000000000000_u64, 0x72a0000000000000_u64, 0x7310000000000000_u64,
  139. 0x7a80000000000000_u64, 0x7b30000000000000_u64, 0x79e0000000000000_u64, 0x7850000000000000_u64,
  140. 0x7c40000000000000_u64, 0x7df0000000000000_u64, 0x7f20000000000000_u64, 0x7e90000000000000_u64,
  141. 0x5a00000000000000_u64, 0x5bb0000000000000_u64, 0x5960000000000000_u64, 0x58d0000000000000_u64,
  142. 0x5cc0000000000000_u64, 0x5d70000000000000_u64, 0x5fa0000000000000_u64, 0x5e10000000000000_u64,
  143. 0x5780000000000000_u64, 0x5630000000000000_u64, 0x54e0000000000000_u64, 0x5550000000000000_u64,
  144. 0x5140000000000000_u64, 0x50f0000000000000_u64, 0x5220000000000000_u64, 0x5390000000000000_u64,
  145. 0x4100000000000000_u64, 0x40b0000000000000_u64, 0x4260000000000000_u64, 0x43d0000000000000_u64,
  146. 0x47c0000000000000_u64, 0x4670000000000000_u64, 0x44a0000000000000_u64, 0x4510000000000000_u64,
  147. 0x4c80000000000000_u64, 0x4d30000000000000_u64, 0x4fe0000000000000_u64, 0x4e50000000000000_u64,
  148. 0x4a40000000000000_u64, 0x4bf0000000000000_u64, 0x4920000000000000_u64, 0x4890000000000000_u64,
  149. 0xd800000000000000_u64, 0xd9b0000000000000_u64, 0xdb60000000000000_u64, 0xdad0000000000000_u64,
  150. 0xdec0000000000000_u64, 0xdf70000000000000_u64, 0xdda0000000000000_u64, 0xdc10000000000000_u64,
  151. 0xd580000000000000_u64, 0xd430000000000000_u64, 0xd6e0000000000000_u64, 0xd750000000000000_u64,
  152. 0xd340000000000000_u64, 0xd2f0000000000000_u64, 0xd020000000000000_u64, 0xd190000000000000_u64,
  153. 0xc300000000000000_u64, 0xc2b0000000000000_u64, 0xc060000000000000_u64, 0xc1d0000000000000_u64,
  154. 0xc5c0000000000000_u64, 0xc470000000000000_u64, 0xc6a0000000000000_u64, 0xc710000000000000_u64,
  155. 0xce80000000000000_u64, 0xcf30000000000000_u64, 0xcde0000000000000_u64, 0xcc50000000000000_u64,
  156. 0xc840000000000000_u64, 0xc9f0000000000000_u64, 0xcb20000000000000_u64, 0xca90000000000000_u64,
  157. 0xee00000000000000_u64, 0xefb0000000000000_u64, 0xed60000000000000_u64, 0xecd0000000000000_u64,
  158. 0xe8c0000000000000_u64, 0xe970000000000000_u64, 0xeba0000000000000_u64, 0xea10000000000000_u64,
  159. 0xe380000000000000_u64, 0xe230000000000000_u64, 0xe0e0000000000000_u64, 0xe150000000000000_u64,
  160. 0xe540000000000000_u64, 0xe4f0000000000000_u64, 0xe620000000000000_u64, 0xe790000000000000_u64,
  161. 0xf500000000000000_u64, 0xf4b0000000000000_u64, 0xf660000000000000_u64, 0xf7d0000000000000_u64,
  162. 0xf3c0000000000000_u64, 0xf270000000000000_u64, 0xf0a0000000000000_u64, 0xf110000000000000_u64,
  163. 0xf880000000000000_u64, 0xf930000000000000_u64, 0xfbe0000000000000_u64, 0xfa50000000000000_u64,
  164. 0xfe40000000000000_u64, 0xfff0000000000000_u64, 0xfd20000000000000_u64, 0xfc90000000000000_u64,
  165. 0xb400000000000000_u64, 0xb5b0000000000000_u64, 0xb760000000000000_u64, 0xb6d0000000000000_u64,
  166. 0xb2c0000000000000_u64, 0xb370000000000000_u64, 0xb1a0000000000000_u64, 0xb010000000000000_u64,
  167. 0xb980000000000000_u64, 0xb830000000000000_u64, 0xbae0000000000000_u64, 0xbb50000000000000_u64,
  168. 0xbf40000000000000_u64, 0xbef0000000000000_u64, 0xbc20000000000000_u64, 0xbd90000000000000_u64,
  169. 0xaf00000000000000_u64, 0xaeb0000000000000_u64, 0xac60000000000000_u64, 0xadd0000000000000_u64,
  170. 0xa9c0000000000000_u64, 0xa870000000000000_u64, 0xaaa0000000000000_u64, 0xab10000000000000_u64,
  171. 0xa280000000000000_u64, 0xa330000000000000_u64, 0xa1e0000000000000_u64, 0xa050000000000000_u64,
  172. 0xa440000000000000_u64, 0xa5f0000000000000_u64, 0xa720000000000000_u64, 0xa690000000000000_u64,
  173. 0x8200000000000000_u64, 0x83b0000000000000_u64, 0x8160000000000000_u64, 0x80d0000000000000_u64,
  174. 0x84c0000000000000_u64, 0x8570000000000000_u64, 0x87a0000000000000_u64, 0x8610000000000000_u64,
  175. 0x8f80000000000000_u64, 0x8e30000000000000_u64, 0x8ce0000000000000_u64, 0x8d50000000000000_u64,
  176. 0x8940000000000000_u64, 0x88f0000000000000_u64, 0x8a20000000000000_u64, 0x8b90000000000000_u64,
  177. 0x9900000000000000_u64, 0x98b0000000000000_u64, 0x9a60000000000000_u64, 0x9bd0000000000000_u64,
  178. 0x9fc0000000000000_u64, 0x9e70000000000000_u64, 0x9ca0000000000000_u64, 0x9d10000000000000_u64,
  179. 0x9480000000000000_u64, 0x9530000000000000_u64, 0x97e0000000000000_u64, 0x9650000000000000_u64,
  180. 0x9240000000000000_u64, 0x93f0000000000000_u64, 0x9120000000000000_u64, 0x9090000000000000_u64
  181. ]
  182. @crc : UInt64
  183. def initialize
  184. @crc = @@INIT_CRC
  185. reset
  186. end
  187. #
  188. # Packs the CRC64 checksum.
  189. #
  190. # @param [Integer] crc
  191. # The checksum to pack.
  192. #
  193. # @return [String]
  194. # The packed checksum.
  195. #
  196. def self.pack(crc)
  197. buffer = ""
  198. buffer << ((crc & 0xff00000000000000) >> 56).chr
  199. buffer << ((crc & 0xff000000000000) >> 48).chr
  200. buffer << ((crc & 0xff0000000000) >> 40).chr
  201. buffer << ((crc & 0xff00000000) >> 32).chr
  202. buffer << ((crc & 0xff000000) >> 24).chr
  203. buffer << ((crc & 0xff0000) >> 16).chr
  204. buffer << ((crc & 0xff00) >> 8).chr
  205. buffer << (crc & 0xff).chr
  206. buffer
  207. end
  208. #
  209. # Updates the CRC64 checksum.
  210. #
  211. # @param [String] data
  212. # The data to update the checksum with.
  213. #
  214. def update(data)
  215. data.each_byte do |b|
  216. @crc = ((@@TABLE[(@crc ^ b) & 0xff] ^ (@crc >> 8)) & 0xffffffffffffffff)
  217. end
  218. return self
  219. end
  220. def digest
  221. v = @crc
  222. ptr = pointerof(v).as(UInt8*)
  223. slice = Slice(UInt8).new(ptr,8)
  224. return IO::ByteFormat::BigEndian.decode(UInt64, slice)
  225. end
  226. end
  227. def hash_ripemd160(data : String)
  228. ctx = OpenSSL::Digest.new("ripemd160")
  229. ctx << data
  230. digest = ctx.digest
  231. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  232. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  233. return tag
  234. end
  235. def hash_sha1(data : String)
  236. ctx = OpenSSL::Digest.new("sha1")
  237. ctx << data
  238. digest = ctx.digest
  239. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  240. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  241. return tag
  242. end
  243. def hash_sha224(data : String)
  244. ctx = OpenSSL::Digest.new("sha224")
  245. ctx << data
  246. digest = ctx.digest
  247. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  248. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  249. return tag
  250. end
  251. def hash_sha256(data : String)
  252. ctx = OpenSSL::Digest.new("sha256")
  253. ctx << data
  254. digest = ctx.digest
  255. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  256. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  257. return tag
  258. end
  259. def hash_sha384(data : String)
  260. ctx = OpenSSL::Digest.new("sha384")
  261. ctx << data
  262. digest = ctx.digest
  263. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  264. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  265. return tag
  266. end
  267. def hash_sha512(data : String)
  268. ctx = OpenSSL::Digest.new("sha512")
  269. ctx << data
  270. digest = ctx.digest
  271. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  272. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  273. return tag
  274. end
  275. def hash_md4(data : String)
  276. ctx = OpenSSL::Digest.new("md4")
  277. ctx << data
  278. digest = ctx.digest
  279. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  280. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  281. return tag
  282. end
  283. def hash_md5(data : String)
  284. ctx = OpenSSL::Digest.new("md5")
  285. ctx << data
  286. digest = ctx.digest
  287. tag = IO::ByteFormat::BigEndian.decode(UInt64, digest)
  288. tag ^= IO::ByteFormat::BigEndian.decode(UInt64, digest+8)
  289. return tag
  290. end
  291. def hash_crc64(data : String)
  292. ctx = CRC64.new
  293. ctx << data
  294. return ctx.digest
  295. end
  296. if to_hex(hash_crc64("azertyuiopqsdfghjklmwxcvbn123456"))!="c9292a94ae08e4b2"
  297. puts "CRITICAL CRC64 ERROR : got "+to_hex(hash_crc64("azertyuiopqsdfghjklmwxcvbn123456"))+"\n"
  298. end
  299. store = Hash(UInt64,Int32).new(10000000)
  300. s3 = Set(UInt64).new
  301. (5000000000_u64..5020000000_u64).each do |n|
  302. h = hash_crc64(to_hex(n))
  303. store[h] = store.fetch(h,0)+1
  304. end
  305. store.each do |k,v|
  306. if v>1
  307. puts "Got #{v} duplicates for key #{to_hex(k)}"
  308. end
  309. s3 << k
  310. end
  311. sum = BigRational.new(0)
  312. store.each do |k,v|
  313. sum+=k
  314. end
  315. mean = sum/store.size
  316. sumalt = BigRational.new(0)
  317. store.each do |k,v|
  318. sumalt+=((-mean+k)*(-mean+k))
  319. end
  320. stddev=Math.sqrt(sumalt/store.size)
  321. puts "Mean of CRCs: "+mean.to_f.to_s
  322. puts "StdDev of CRCs: "+stddev.to_f.to_s
  323. puts "32 bytes key ->"
  324. Benchmark.ips do |x|
  325. x.report("\tCRC64 32 bytes") { hash_crc64("azertyuiopqsdfghjklmwxcvbn123456") }
  326. x.report("\tripemd160 32 bytes") { hash_ripemd160("azertyuiopqsdfghjklmwxcvbn123456") }
  327. x.report("\tMD4 32 bytes") { hash_md4("azertyuiopqsdfghjklmwxcvbn123456") }
  328. x.report("\tMD5 32 bytes") { hash_md5("azertyuiopqsdfghjklmwxcvbn123456") }
  329. x.report("\tSHA1 32 bytes") { hash_sha1("azertyuiopqsdfghjklmwxcvbn123456") }
  330. x.report("\tSHA224 32 bytes") { hash_sha224("azertyuiopqsdfghjklmwxcvbn123456") }
  331. x.report("\tSHA256 32 bytes") { hash_sha256("azertyuiopqsdfghjklmwxcvbn123456") }
  332. x.report("\tSHA384 32 bytes") { hash_sha384("azertyuiopqsdfghjklmwxcvbn123456") }
  333. x.report("\tSHA512 32 bytes") { hash_sha512("azertyuiopqsdfghjklmwxcvbn123456") }
  334. end
  335. puts "128 bytes key ->"
  336. Benchmark.ips do |x|
  337. str = "azertyuiopqsdfghjklmwxcvbn123456azertyuiopqsdfghjklmwxcvbn123456azertyuiopqsdfghjklmwxcvbn123456azertyuiopqsdfghjklmwxcvbn123456"
  338. x.report("\tCRC64 128 bytes") { hash_crc64(str) }
  339. x.report("\tripemd160 128 bytes") { hash_ripemd160(str) }
  340. x.report("\tMD4 128 bytes") { hash_md4(str) }
  341. x.report("\tMD5 128 bytes") { hash_md5(str) }
  342. x.report("\tSHA1 128 bytes") { hash_sha1(str) }
  343. x.report("\tSHA224 128 bytes") { hash_sha224(str) }
  344. x.report("\tSHA256 128 bytes") { hash_sha256(str) }
  345. x.report("\tSHA384 128 bytes") { hash_sha384(str) }
  346. x.report("\tSHA512 128 bytes") { hash_sha512(str) }
  347. end
  348. puts "1024 bytes key ->"
  349. Benchmark.ips do |x|
  350. str = "azertyuiopqsdfghjklmwxcvbn123456azertyuiopqsdfghjklmwxcvbn123456azertyuiopqsdfghjklmwxcvbn123456azertyuiopqsdfghjklmwxcvbn123456"*8
  351. x.report("\tCRC64 1024 bytes") { hash_crc64(str) }
  352. x.report("\tripemd160 1024 bytes") { hash_ripemd160(str) }
  353. x.report("\tMD4 1024 bytes") { hash_md4(str) }
  354. x.report("\tMD5 1024 bytes") { hash_md5(str) }
  355. x.report("\tSHA1 1024 bytes") { hash_sha1(str) }
  356. x.report("\tSHA224 1024 bytes") { hash_sha224(str) }
  357. x.report("\tSHA256 1024 bytes") { hash_sha256(str) }
  358. x.report("\tSHA384 1024 bytes") { hash_sha384(str) }
  359. x.report("\tSHA512 1024 bytes") { hash_sha512(str) }
  360. end