go implementation of CodeDweller/mishmash.hpp/cpp
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package mishmash
  2. var PrimeSet = [256]uint32{
  3. 3238836313, 3360886099, 3993634273, 4064669561, 4208856901, 4049234129, 2251082663, 2536872017,
  4. 3172087459, 4219386191, 3254853737, 2374917353, 3448754471, 3118274443, 3572204863, 3686337631,
  5. 2961431969, 3025133497, 2856326813, 4277854241, 2469276751, 3175023217, 4065345943, 2191667573,
  6. 3716607571, 3186473551, 2993231543, 2854210481, 3661300009, 4160057677, 3393580357, 2401731751,
  7. 3089619301, 3049406923, 2689305253, 2226455687, 2384594237, 2321314543, 3358155473, 2769745513,
  8. 4064412181, 3546679369, 2971824769, 3312622147, 3878690237, 4226789561, 3945135893, 2667481697,
  9. 3886727213, 2447929819, 2905769257, 2655507487, 2151468307, 2774610127, 3117477541, 2619506011,
  10. 2224830217, 4005223571, 2817826603, 4024485649, 2357623691, 3380603881, 3799676627, 3174723401,
  11. 2925703279, 3692030071, 3928564727, 3822903739, 2520472573, 2345602981, 2318506963, 2933432651,
  12. 2964809711, 3441413681, 3537168083, 3213751901, 3785291413, 3874794739, 2703288127, 4139044993,
  13. 2757352427, 3533741287, 2767266521, 3616593653, 4121176723, 2759804627, 2613878749, 3621322741,
  14. 3365797381, 3780923077, 4090912909, 2400326909, 3222524423, 2824243943, 2496802109, 2268391639,
  15. 3124087573, 3716479807, 3131673143, 2598657163, 3239086001, 3049736639, 2288116447, 3310352849,
  16. 3095504927, 3299510729, 4177820137, 3414229717, 3243438811, 3532678967, 3232117841, 3951863177,
  17. 2555079403, 3921722923, 2767778263, 3642539597, 2259834439, 3378013361, 3318867061, 2219022677,
  18. 4283411207, 3528301687, 2626036943, 2258523251, 3833937103, 3639081947, 4029829451, 2360238479,
  19. 2184594703, 4187931989, 2562197887, 3622455827, 3374449921, 3194095649, 4048368617, 2355855647,
  20. 4187863969, 3671153483, 2548899877, 3752659289, 3019207333, 3000748913, 2969725901, 2288226749,
  21. 3941344529, 2160836201, 2557613741, 2747755807, 2813873353, 2437638083, 2278088377, 2925209927,
  22. 4245758497, 3470038853, 2625083243, 2293827821, 3819793067, 3887526641, 3810382399, 4129510223,
  23. 3184769573, 3942422921, 3000411851, 2267160979, 3295636387, 3962735783, 2857209623, 2388617971,
  24. 3691266013, 2683345051, 3245511107, 3349852217, 2544095017, 3631826647, 4105881239, 2762987873,
  25. 2666463031, 2946438881, 3113899517, 2223454549, 2568559537, 3101729287, 2555320727, 3030152533,
  26. 2979132743, 4253672737, 2870043779, 2843306603, 3926687683, 4185601351, 3731200831, 3722500409,
  27. 4115072947, 2615487569, 2389531631, 3868949611, 2337008593, 2515079899, 2521605199, 3168567119,
  28. 3580694591, 3845527589, 3722753051, 2668632049, 3667111729, 3874685057, 3226838213, 2720181421,
  29. 2612026369, 2694744433, 4155238081, 3521473309, 3862885931, 3115061903, 4166213797, 3520042793,
  30. 3944208013, 4078403383, 2285388311, 3719800027, 4016404867, 3390427661, 3338388139, 2660266009,
  31. 3032933947, 4200211513, 3155686909, 3019786009, 4125535433, 3244923947, 2398660417, 2758948859,
  32. 2944556507, 3818410403, 2247818519, 2586876613, 3420280733, 4236637451, 2724168703, 3321056041,
  33. 2625015193, 2795623993, 3911522341, 3507684083, 3553065317, 3987511039, 2786582429, 2534596151,
  34. }
  35. func slct(n uint64) uint32 {
  36. return PrimeSet[n&0xff]
  37. }
  38. func Engine(buffer string, length int, accumulator uint64) uint64 {
  39. for i := 0; i < length; i++ {
  40. b := buffer[i]
  41. var accumulator1 uint64 = uint64(slct(accumulator) + uint32(b))
  42. var accumulator2 uint64 = ^accumulator * uint64(slct(uint64(b)))
  43. var accumulator3 = accumulator >> (32 + ((b & 0x1F) ^ (b >> 5)))
  44. accumulator = accumulator1 + accumulator2 + accumulator3
  45. }
  46. return accumulator
  47. }
  48. func Mishmash(buffer string, nums ...uint64) uint32 {
  49. // nums is a seed/accumulator
  50. var accumulator uint64
  51. if 0 == len(nums) {
  52. // seeding with 0 by default - could be changed
  53. accumulator = Engine(buffer, len(buffer), 0)
  54. } else {
  55. accumulator = Engine(buffer, len(buffer), nums[0])
  56. }
  57. return uint32(accumulator & 0x00000000ffffffff)
  58. }
  59. // example below shows calling Mishmash on "hello world"
  60. // and then collecting the hash. Given
  61. // a collision we simply call Mishmash again, giving it
  62. // the first hash as a seed this time and get a second hash.
  63. /*
  64. func main() {
  65. buf := []byte("Hello world")
  66. // grab the accumulator if necessary
  67. accum := Engine(buf, len(buf), 0)
  68. // grab the hash - decode accumulator into hash
  69. hash := MishmashAccumulator(accum)
  70. fmt.Printf("%08x\n", hash)
  71. // now get second hash - seed with accumulator
  72. secondHash := Mishmash(buf, accum)
  73. fmt.Printf("%08x\n", secondHash)
  74. // you can also get a simple hash
  75. simpleMishmash := Mishmash(buf)
  76. fmt.Printf("%08x\n", simpleMishmash)
  77. }
  78. */
  79. // example below used to validate output against C++ mishmash
  80. /*
  81. func main() {
  82. buf := "Hello world!"
  83. first := MishmashAccumulator(Engine(buf, len(buf), 0))
  84. second := MishmashAccumulator(Engine(buf, len(buf), 1))
  85. var combo uint64
  86. combo = uint64(first)<<32 | uint64(second)
  87. fmt.Println(first, second, combo)
  88. }
  89. */