go implementation of CodeDweller/mishmash.hpp/cpp
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

mishmash.go 4.9KB

3 år sedan
3 år sedan
3 år sedan
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package mishmash
  2. var primes = [256]uint32{2781111551, 4115761697, 2188512439, 3902941499, 4078728127, 2919449173, 3950407729, 4081733563,
  3. 4061783399, 2728829231, 3557004557, 3778858159, 2397085727, 3808921189, 3159484147, 4084759639,
  4. 2938297727, 3839623429, 2441361773, 2471079053, 4270995293, 3336967261, 2717746523, 3306593077,
  5. 2846319523, 2704420699, 2772002447, 2664263209, 3452941517, 3422898851, 2502691043, 3785866613,
  6. 3164194337, 3348481009, 3448873073, 2340666883, 3742418039, 2298597811, 3061650257, 4286896921,
  7. 2816281231, 3814879819, 3245870963, 2718113173, 3051925081, 2527351147, 2737897993, 3567396421,
  8. 3422709179, 3542178331, 3740214493, 3720784621, 2821606727, 3764267237, 3923068349, 3880303043,
  9. 3816527917, 3032326837, 4006470463, 4165427173, 2511539731, 2993685649, 2268550831, 2899250059,
  10. 2432655553, 3959959639, 4142838449, 4100124029, 2962655827, 2648245423, 4117297801, 3331059481,
  11. 2287716593, 4115533181, 2411037073, 3273978871, 2395052183, 2776949353, 3871801559, 2948305931,
  12. 2257427881, 4288176181, 2624064151, 3144150523, 4282472071, 2206613407, 2960685401, 2761304627,
  13. 3187632967, 2447380627, 2781789473, 3657272401, 3603710699, 4042745507, 4087095841, 2354409557,
  14. 2239000559, 4164370321, 3636939347, 3622573529, 2202257819, 2792364713, 2803311809, 4006899277,
  15. 2835975767, 2951997809, 3185753917, 2417651723, 2631379747, 2175388331, 4277708983, 3547799261,
  16. 3093003779, 3653208029, 4084095199, 3161669417, 2698224923, 2746404013, 3209829191, 3024118079,
  17. 3799471327, 3012753409, 2653321639, 2777700139, 3794895031, 3075822329, 2774150557, 3135465059,
  18. 2294247001, 3557999149, 3189799397, 4080431539, 3397276529, 3931450153, 3518901437, 3135266501,
  19. 3275906693, 4143264221, 3439131923, 2462856037, 2441331799, 2640219781, 3504419027, 3721296989,
  20. 3434920081, 2852097271, 3199425679, 3785517557, 2984788193, 3678788947, 2308371491, 3354456173,
  21. 4148931503, 3239885063, 3894052103, 3930187787, 3521437319, 3203332001, 2894779337, 4197047957,
  22. 3080099353, 2304447583, 3731998477, 2733978179, 2235339121, 2490436339, 2147689987, 3276115639,
  23. 3923204273, 3402325321, 3082785493, 3420863399, 3203562407, 2292225781, 3461192447, 2589852949,
  24. 2772559931, 3667144931, 2163126419, 3412216831, 3699715037, 3619099453, 3875532569, 3047225267,
  25. 3780456941, 2512094297, 3527794547, 3130190737, 3013107347, 3691645577, 3877430399, 4270767517,
  26. 2797247227, 3976455169, 3541991143, 3737016677, 3801675821, 2233908737, 3210598117, 4200595549,
  27. 2772203789, 2415614503, 2467043143, 2611954561, 2791210277, 2599276769, 4034287091, 3521719193,
  28. 3060219497, 2799601709, 2178939557, 3800717749, 3426533269, 3519387097, 2861815447, 2884852603,
  29. 2814915449, 3359572921, 3013763303, 3565679363, 3491538893, 2588490083, 3441048649, 2501343701,
  30. 4191701149, 2251607773, 3543948791, 3511969763, 2966565907, 3640359409, 3640516129, 2586869203,
  31. 2328651011, 4037640461, 3601356797, 4260950119, 3198526459, 3478546597, 3009442531, 3594323353,
  32. 3205141207, 3592422991, 3357971081, 3217249313, 2903534827, 3716714951, 3388800011, 3045269987,
  33. 2325616213, 2688355151, 3022034467, 4221197753, 2426944493, 3090262709, 3471570467, 2437347167,
  34. }
  35. func slct(n uint64) uint32 {
  36. return primes[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. accumulator += uint64(slct(accumulator)) + uint64(b)
  42. accumulator *= uint64(slct(uint64(b)))
  43. accumulator += accumulator >> 32
  44. }
  45. return accumulator
  46. }
  47. func Mishmash(buffer string, nums ...uint64) uint32 {
  48. // nums is a seed/accumulator
  49. var accumulator uint64
  50. if 0 == len(nums) {
  51. // seeding with 0 by default - could be changed
  52. accumulator = Engine(buffer, len(buffer), 0)
  53. } else {
  54. accumulator = Engine(buffer, len(buffer), nums[0])
  55. }
  56. return uint32(accumulator & 0x00000000ffffffff)
  57. }
  58. func MishmashAccumulator(accumulator uint64) uint32 {
  59. // use when collecting accumulators for second hash
  60. return uint32(accumulator & 0x00000000ffffffff)
  61. }
  62. // example below shows calling Mishmash on "hello world"
  63. // and then collecting the hash. Given
  64. // a collision we simply call Mishmash again, giving it
  65. // the first hash as a seed this time and get a second hash.
  66. /*
  67. func main() {
  68. buf := []byte("Hello world")
  69. // grab the accumulator if necessary
  70. accum := Engine(buf, len(buf), 0)
  71. // grab the hash - decode accumulator into hash
  72. hash := MishmashAccumulator(accum)
  73. fmt.Printf("%08x\n", hash)
  74. // now get second hash - seed with accumulator
  75. secondHash := Mishmash(buf, accum)
  76. fmt.Printf("%08x\n", secondHash)
  77. // you can also get a simple hash
  78. simpleMishmash := Mishmash(buf)
  79. fmt.Printf("%08x\n", simpleMishmash)
  80. }
  81. */
  82. // example below used to validate output against C++ mishmash
  83. /*
  84. func main() {
  85. buf := "Hello world!"
  86. first := MishmashAccumulator(Engine(buf, len(buf), 0))
  87. second := MishmashAccumulator(Engine(buf, len(buf), 1))
  88. var combo uint64
  89. combo = uint64(first)<<32 | uint64(second)
  90. fmt.Println(first, second, combo)
  91. }
  92. */