CheckSumHelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Security.Cryptography;
  6. using System.Diagnostics.CodeAnalysis;
  7. namespace EasyDevCore.Common
  8. {
  9. /// <summary>
  10. /// Check sum
  11. /// </summary>
  12. public class CheckSumHelper
  13. {
  14. private static readonly ushort[] CRC16Table =
  15. {
  16. 0X0000, 0X1021, 0X2042, 0X3063, 0X4084, 0X50A5, 0X60C6, 0X70E7,
  17. 0X8108, 0X9129, 0XA14A, 0XB16B, 0XC18C, 0XD1AD, 0XE1CE, 0XF1EF,
  18. 0X1231, 0X0210, 0X3273, 0X2252, 0X52B5, 0X4294, 0X72F7, 0X62D6,
  19. 0X9339, 0X8318, 0XB37B, 0XA35A, 0XD3BD, 0XC39C, 0XF3FF, 0XE3DE,
  20. 0X2462, 0X3443, 0X0420, 0X1401, 0X64E6, 0X74C7, 0X44A4, 0X5485,
  21. 0XA56A, 0XB54B, 0X8528, 0X9509, 0XE5EE, 0XF5CF, 0XC5AC, 0XD58D,
  22. 0X3653, 0X2672, 0X1611, 0X0630, 0X76D7, 0X66F6, 0X5695, 0X46B4,
  23. 0XB75B, 0XA77A, 0X9719, 0X8738, 0XF7DF, 0XE7FE, 0XD79D, 0XC7BC,
  24. 0X48C4, 0X58E5, 0X6886, 0X78A7, 0X0840, 0X1861, 0X2802, 0X3823,
  25. 0XC9CC, 0XD9ED, 0XE98E, 0XF9AF, 0X8948, 0X9969, 0XA90A, 0XB92B,
  26. 0X5AF5, 0X4AD4, 0X7AB7, 0X6A96, 0X1A71, 0X0A50, 0X3A33, 0X2A12,
  27. 0XDBFD, 0XCBDC, 0XFBBF, 0XEB9E, 0X9B79, 0X8B58, 0XBB3B, 0XAB1A,
  28. 0X6CA6, 0X7C87, 0X4CE4, 0X5CC5, 0X2C22, 0X3C03, 0X0C60, 0X1C41,
  29. 0XEDAE, 0XFD8F, 0XCDEC, 0XDDCD, 0XAD2A, 0XBD0B, 0X8D68, 0X9D49,
  30. 0X7E97, 0X6EB6, 0X5ED5, 0X4EF4, 0X3E13, 0X2E32, 0X1E51, 0X0E70,
  31. 0XFF9F, 0XEFBE, 0XDFDD, 0XCFFC, 0XBF1B, 0XAF3A, 0X9F59, 0X8F78,
  32. 0X9188, 0X81A9, 0XB1CA, 0XA1EB, 0XD10C, 0XC12D, 0XF14E, 0XE16F,
  33. 0X1080, 0X00A1, 0X30C2, 0X20E3, 0X5004, 0X4025, 0X7046, 0X6067,
  34. 0X83B9, 0X9398, 0XA3FB, 0XB3DA, 0XC33D, 0XD31C, 0XE37F, 0XF35E,
  35. 0X02B1, 0X1290, 0X22F3, 0X32D2, 0X4235, 0X5214, 0X6277, 0X7256,
  36. 0XB5EA, 0XA5CB, 0X95A8, 0X8589, 0XF56E, 0XE54F, 0XD52C, 0XC50D,
  37. 0X34E2, 0X24C3, 0X14A0, 0X0481, 0X7466, 0X6447, 0X5424, 0X4405,
  38. 0XA7DB, 0XB7FA, 0X8799, 0X97B8, 0XE75F, 0XF77E, 0XC71D, 0XD73C,
  39. 0X26D3, 0X36F2, 0X0691, 0X16B0, 0X6657, 0X7676, 0X4615, 0X5634,
  40. 0XD94C, 0XC96D, 0XF90E, 0XE92F, 0X99C8, 0X89E9, 0XB98A, 0XA9AB,
  41. 0X5844, 0X4865, 0X7806, 0X6827, 0X18C0, 0X08E1, 0X3882, 0X28A3,
  42. 0XCB7D, 0XDB5C, 0XEB3F, 0XFB1E, 0X8BF9, 0X9BD8, 0XABBB, 0XBB9A,
  43. 0X4A75, 0X5A54, 0X6A37, 0X7A16, 0X0AF1, 0X1AD0, 0X2AB3, 0X3A92,
  44. 0XFD2E, 0XED0F, 0XDD6C, 0XCD4D, 0XBDAA, 0XAD8B, 0X9DE8, 0X8DC9,
  45. 0X7C26, 0X6C07, 0X5C64, 0X4C45, 0X3CA2, 0X2C83, 0X1CE0, 0X0CC1,
  46. 0XEF1F, 0XFF3E, 0XCF5D, 0XDF7C, 0XAF9B, 0XBFBA, 0X8FD9, 0X9FF8,
  47. 0X6E17, 0X7E36, 0X4E55, 0X5E74, 0X2E93, 0X3EB2, 0X0ED1, 0X1EF0
  48. };
  49. private static readonly uint[] CRC32Table = {
  50. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419,
  51. 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4,
  52. 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07,
  53. 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE,
  54. 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856,
  55. 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  56. 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4,
  57. 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B,
  58. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3,
  59. 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A,
  60. 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599,
  61. 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
  62. 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190,
  63. 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
  64. 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E,
  65. 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01,
  66. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED,
  67. 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
  68. 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3,
  69. 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2,
  70. 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A,
  71. 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
  72. 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010,
  73. 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
  74. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17,
  75. 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6,
  76. 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615,
  77. 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8,
  78. 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344,
  79. 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  80. 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A,
  81. 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5,
  82. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1,
  83. 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C,
  84. 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF,
  85. 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
  86. 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE,
  87. 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
  88. 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C,
  89. 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713,
  90. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B,
  91. 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
  92. 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1,
  93. 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C,
  94. 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278,
  95. 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
  96. 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66,
  97. 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
  98. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605,
  99. 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8,
  100. 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B,
  101. 0x2D02EF8D
  102. };
  103. private static readonly ulong[] CRC64Table = {
  104. 0x0000000000000000, 0x7ad870c830358979, 0xf5b0e190606b12f2, 0x8f689158505e9b8b, 0xc038e5739841b68f, 0xbae095bba8743ff6,
  105. 0x358804e3f82aa47d, 0x4f50742bc81f2d04, 0xab28ecb46814fe75, 0xd1f09c7c5821770c, 0x5e980d24087fec87, 0x24407dec384a65fe,
  106. 0x6b1009c7f05548fa, 0x11c8790fc060c183, 0x9ea0e857903e5a08, 0xe478989fa00bd371, 0x7d08ff3b88be6f81, 0x07d08ff3b88be6f8,
  107. 0x88b81eabe8d57d73, 0xf2606e63d8e0f40a, 0xbd301a4810ffd90e, 0xc7e86a8020ca5077, 0x4880fbd87094cbfc, 0x32588b1040a14285,
  108. 0xd620138fe0aa91f4, 0xacf86347d09f188d, 0x2390f21f80c18306, 0x594882d7b0f40a7f, 0x1618f6fc78eb277b, 0x6cc0863448deae02,
  109. 0xe3a8176c18803589, 0x997067a428b5bcf0, 0xfa11fe77117cdf02, 0x80c98ebf2149567b, 0x0fa11fe77117cdf0, 0x75796f2f41224489,
  110. 0x3a291b04893d698d, 0x40f16bccb908e0f4, 0xcf99fa94e9567b7f, 0xb5418a5cd963f206, 0x513912c379682177, 0x2be1620b495da80e,
  111. 0xa489f35319033385, 0xde51839b2936bafc, 0x9101f7b0e12997f8, 0xebd98778d11c1e81, 0x64b116208142850a, 0x1e6966e8b1770c73,
  112. 0x8719014c99c2b083, 0xfdc17184a9f739fa, 0x72a9e0dcf9a9a271, 0x08719014c99c2b08, 0x4721e43f0183060c, 0x3df994f731b68f75,
  113. 0xb29105af61e814fe, 0xc849756751dd9d87, 0x2c31edf8f1d64ef6, 0x56e99d30c1e3c78f, 0xd9810c6891bd5c04, 0xa3597ca0a188d57d,
  114. 0xec09088b6997f879, 0x96d1784359a27100, 0x19b9e91b09fcea8b, 0x636199d339c963f2, 0xdf7adabd7a6e2d6f, 0xa5a2aa754a5ba416,
  115. 0x2aca3b2d1a053f9d, 0x50124be52a30b6e4, 0x1f423fcee22f9be0, 0x659a4f06d21a1299, 0xeaf2de5e82448912, 0x902aae96b271006b,
  116. 0x74523609127ad31a, 0x0e8a46c1224f5a63, 0x81e2d7997211c1e8, 0xfb3aa75142244891, 0xb46ad37a8a3b6595, 0xceb2a3b2ba0eecec,
  117. 0x41da32eaea507767, 0x3b024222da65fe1e, 0xa2722586f2d042ee, 0xd8aa554ec2e5cb97, 0x57c2c41692bb501c, 0x2d1ab4dea28ed965,
  118. 0x624ac0f56a91f461, 0x1892b03d5aa47d18, 0x97fa21650afae693, 0xed2251ad3acf6fea, 0x095ac9329ac4bc9b, 0x7382b9faaaf135e2,
  119. 0xfcea28a2faafae69, 0x8632586aca9a2710, 0xc9622c4102850a14, 0xb3ba5c8932b0836d, 0x3cd2cdd162ee18e6, 0x460abd1952db919f,
  120. 0x256b24ca6b12f26d, 0x5fb354025b277b14, 0xd0dbc55a0b79e09f, 0xaa03b5923b4c69e6, 0xe553c1b9f35344e2, 0x9f8bb171c366cd9b,
  121. 0x10e3202993385610, 0x6a3b50e1a30ddf69, 0x8e43c87e03060c18, 0xf49bb8b633338561, 0x7bf329ee636d1eea, 0x012b592653589793,
  122. 0x4e7b2d0d9b47ba97, 0x34a35dc5ab7233ee, 0xbbcbcc9dfb2ca865, 0xc113bc55cb19211c, 0x5863dbf1e3ac9dec, 0x22bbab39d3991495,
  123. 0xadd33a6183c78f1e, 0xd70b4aa9b3f20667, 0x985b3e827bed2b63, 0xe2834e4a4bd8a21a, 0x6debdf121b863991, 0x1733afda2bb3b0e8,
  124. 0xf34b37458bb86399, 0x8993478dbb8deae0, 0x06fbd6d5ebd3716b, 0x7c23a61ddbe6f812, 0x3373d23613f9d516, 0x49aba2fe23cc5c6f,
  125. 0xc6c333a67392c7e4, 0xbc1b436e43a74e9d, 0x95ac9329ac4bc9b5, 0xef74e3e19c7e40cc, 0x601c72b9cc20db47, 0x1ac40271fc15523e,
  126. 0x5594765a340a7f3a, 0x2f4c0692043ff643, 0xa02497ca54616dc8, 0xdafce7026454e4b1, 0x3e847f9dc45f37c0, 0x445c0f55f46abeb9,
  127. 0xcb349e0da4342532, 0xb1eceec59401ac4b, 0xfebc9aee5c1e814f, 0x8464ea266c2b0836, 0x0b0c7b7e3c7593bd, 0x71d40bb60c401ac4,
  128. 0xe8a46c1224f5a634, 0x927c1cda14c02f4d, 0x1d148d82449eb4c6, 0x67ccfd4a74ab3dbf, 0x289c8961bcb410bb, 0x5244f9a98c8199c2,
  129. 0xdd2c68f1dcdf0249, 0xa7f41839ecea8b30, 0x438c80a64ce15841, 0x3954f06e7cd4d138, 0xb63c61362c8a4ab3, 0xcce411fe1cbfc3ca,
  130. 0x83b465d5d4a0eece, 0xf96c151de49567b7, 0x76048445b4cbfc3c, 0x0cdcf48d84fe7545, 0x6fbd6d5ebd3716b7, 0x15651d968d029fce,
  131. 0x9a0d8ccedd5c0445, 0xe0d5fc06ed698d3c, 0xaf85882d2576a038, 0xd55df8e515432941, 0x5a3569bd451db2ca, 0x20ed197575283bb3,
  132. 0xc49581ead523e8c2, 0xbe4df122e51661bb, 0x3125607ab548fa30, 0x4bfd10b2857d7349, 0x04ad64994d625e4d, 0x7e7514517d57d734,
  133. 0xf11d85092d094cbf, 0x8bc5f5c11d3cc5c6, 0x12b5926535897936, 0x686de2ad05bcf04f, 0xe70573f555e26bc4, 0x9ddd033d65d7e2bd,
  134. 0xd28d7716adc8cfb9, 0xa85507de9dfd46c0, 0x273d9686cda3dd4b, 0x5de5e64efd965432, 0xb99d7ed15d9d8743, 0xc3450e196da80e3a,
  135. 0x4c2d9f413df695b1, 0x36f5ef890dc31cc8, 0x79a59ba2c5dc31cc, 0x037deb6af5e9b8b5, 0x8c157a32a5b7233e, 0xf6cd0afa9582aa47,
  136. 0x4ad64994d625e4da, 0x300e395ce6106da3, 0xbf66a804b64ef628, 0xc5bed8cc867b7f51, 0x8aeeace74e645255, 0xf036dc2f7e51db2c,
  137. 0x7f5e4d772e0f40a7, 0x05863dbf1e3ac9de, 0xe1fea520be311aaf, 0x9b26d5e88e0493d6, 0x144e44b0de5a085d, 0x6e963478ee6f8124,
  138. 0x21c640532670ac20, 0x5b1e309b16452559, 0xd476a1c3461bbed2, 0xaeaed10b762e37ab, 0x37deb6af5e9b8b5b, 0x4d06c6676eae0222,
  139. 0xc26e573f3ef099a9, 0xb8b627f70ec510d0, 0xf7e653dcc6da3dd4, 0x8d3e2314f6efb4ad, 0x0256b24ca6b12f26, 0x788ec2849684a65f,
  140. 0x9cf65a1b368f752e, 0xe62e2ad306bafc57, 0x6946bb8b56e467dc, 0x139ecb4366d1eea5, 0x5ccebf68aecec3a1, 0x2616cfa09efb4ad8,
  141. 0xa97e5ef8cea5d153, 0xd3a62e30fe90582a, 0xb0c7b7e3c7593bd8, 0xca1fc72bf76cb2a1, 0x45775673a732292a, 0x3faf26bb9707a053,
  142. 0x70ff52905f188d57, 0x0a2722586f2d042e, 0x854fb3003f739fa5, 0xff97c3c80f4616dc, 0x1bef5b57af4dc5ad, 0x61372b9f9f784cd4,
  143. 0xee5fbac7cf26d75f, 0x9487ca0fff135e26, 0xdbd7be24370c7322, 0xa10fceec0739fa5b, 0x2e675fb4576761d0, 0x54bf2f7c6752e8a9,
  144. 0xcdcf48d84fe75459, 0xb71738107fd2dd20, 0x387fa9482f8c46ab, 0x42a7d9801fb9cfd2, 0x0df7adabd7a6e2d6, 0x772fdd63e7936baf,
  145. 0xf8474c3bb7cdf024, 0x829f3cf387f8795d, 0x66e7a46c27f3aa2c, 0x1c3fd4a417c62355, 0x935745fc4798b8de, 0xe98f353477ad31a7,
  146. 0xa6df411fbfb21ca3, 0xdc0731d78f8795da, 0x536fa08fdfd90e51, 0x29b7d047efec8728
  147. };
  148. /// <summary>
  149. /// MD5 hash. (128 bits)
  150. /// </summary>
  151. /// <param name="stream">The stream.</param>
  152. /// <returns></returns>
  153. public static string MD5(Stream stream)
  154. {
  155. using (var encrypt = System.Security.Cryptography.MD5.Create())
  156. {
  157. byte[] hash = encrypt.ComputeHash(stream);
  158. return BitConverter.ToString(hash);
  159. }
  160. }
  161. /// <summary>
  162. /// Ms the d5.
  163. /// </summary>
  164. /// <param name="value">The value.</param>
  165. /// <returns></returns>
  166. public static string MD5(string value)
  167. {
  168. return MD5(System.Text.Encoding.UTF8.GetBytes(value));
  169. }
  170. /// <summary>
  171. /// MD5 hash. (128 bits)
  172. /// </summary>
  173. /// <param name="buffer">The buffer.</param>
  174. /// <param name="offset">The offset.</param>
  175. /// <param name="count">The count.</param>
  176. /// <returns></returns>
  177. public static string MD5(byte[] buffer, int offset, int count)
  178. {
  179. if (offset < 0 || count < 0 || offset + count > buffer.Length)
  180. {
  181. throw new ArgumentOutOfRangeException();
  182. }
  183. using (var encrypt = System.Security.Cryptography.MD5.Create())
  184. {
  185. byte[] hash = encrypt.ComputeHash(buffer, offset, count);
  186. return BitConverter.ToString(hash);
  187. }
  188. }
  189. /// <summary>
  190. /// MD5 hash. (128 bits)
  191. /// </summary>
  192. /// <param name="buffer">The buffer.</param>
  193. /// <returns></returns>
  194. public static string MD5(byte[] buffer)
  195. {
  196. return MD5(buffer, 0, buffer.Length);
  197. }
  198. /// <summary>
  199. /// SHA1 hash. (160 bits)
  200. /// </summary>
  201. /// <param name="stream">The stream.</param>
  202. /// <returns></returns>
  203. public static string SHA1(Stream stream)
  204. {
  205. using (var encrypt = System.Security.Cryptography.SHA1.Create())
  206. {
  207. byte[] hash = encrypt.ComputeHash(stream);
  208. return BitConverter.ToString(hash);
  209. }
  210. }
  211. /// <summary>
  212. /// SHA1 hash. (160 bits)
  213. /// </summary>
  214. /// <param name="buffer">The buffer.</param>
  215. /// <param name="offset">The offset.</param>
  216. /// <param name="count">The count.</param>
  217. /// <returns></returns>
  218. public static string SHA1(byte[] buffer, int offset, int count)
  219. {
  220. if (offset < 0 || count < 0 || offset + count > buffer.Length)
  221. {
  222. throw new ArgumentOutOfRangeException();
  223. }
  224. using (var encrypt = System.Security.Cryptography.SHA1.Create())
  225. {
  226. byte[] hash = encrypt.ComputeHash(buffer, offset, count);
  227. return BitConverter.ToString(hash);
  228. }
  229. }
  230. /// <summary>
  231. /// SHA1 hash. (160 bits)
  232. /// </summary>
  233. /// <param name="buffer">The buffer.</param>
  234. /// <returns></returns>
  235. public static string SHA1(byte[] buffer)
  236. {
  237. return SHA1(buffer, 0, buffer.Length);
  238. }
  239. /// <summary>
  240. /// SHA1 hash. (160 bits)
  241. /// </summary>
  242. /// <param name="value">The value (UTF8 encoding).</param>
  243. /// <returns></returns>
  244. public static string SHA1(string value)
  245. {
  246. return SHA1(System.Text.Encoding.UTF8.GetBytes(value));
  247. }
  248. /// <summary>
  249. /// CRC16 hash. (16 bits).
  250. /// </summary>
  251. /// <returns></returns>
  252. public static ushort CRC16(byte[] buffer, int offset, int count)
  253. {
  254. ushort crcSeed = 0xFFFF;
  255. ushort crc = 0;
  256. if (offset < 0 || count < 0 || offset + count > buffer.Length)
  257. {
  258. throw new ArgumentOutOfRangeException();
  259. }
  260. crc ^= crcSeed;
  261. while (--count >= 0)
  262. {
  263. byte index = (byte)(crc ^ buffer[offset]);
  264. crc = (ushort)((crc >> 8) ^ CRC16Table[index]);
  265. }
  266. crc ^= crcSeed;
  267. return crc;
  268. }
  269. /// <summary>
  270. /// CRC16 hash. (16 bits).
  271. /// </summary>
  272. /// <returns></returns>
  273. public static ushort CRC16(byte[] buffer)
  274. {
  275. return CRC16(buffer, 0, buffer.Length);
  276. }
  277. /// <summary>
  278. /// CRC16 hash. (16 bits).
  279. /// </summary>
  280. /// <param name="value">The value (UTF8 encoding).</param>
  281. /// <returns></returns>
  282. public static ushort CRC16(string value)
  283. {
  284. return CRC16(System.Text.Encoding.UTF8.GetBytes(value));
  285. }
  286. /// <summary>
  287. /// CRC32 hash. (32 bits).
  288. /// </summary>
  289. /// <returns></returns>
  290. public static uint CRC32(byte[] buffer, int offset, int count)
  291. {
  292. uint crcSeed = 0xFFFFFFFF;
  293. uint crc = 0;
  294. if (offset < 0 || count < 0 || offset + count > buffer.Length)
  295. {
  296. throw new ArgumentOutOfRangeException();
  297. }
  298. crc ^= crcSeed;
  299. while (--count >= 0)
  300. {
  301. crc = CRC32Table[(crc ^ buffer[offset++]) & 0xFF] ^ (crc >> 8);
  302. }
  303. crc ^= crcSeed;
  304. return crc;
  305. }
  306. /// <summary>
  307. /// CRC32 hash. (32 bits).
  308. /// </summary>
  309. /// <returns></returns>
  310. public static uint CRC32(byte[] buffer)
  311. {
  312. return CRC32(buffer, 0, buffer.Length);
  313. }
  314. /// <summary>
  315. /// CRC32 hash. (32 bits).
  316. /// </summary>
  317. /// <param name="value">The value (UTF8 encoding).</param>
  318. /// <returns></returns>
  319. public static uint CRC32(string value)
  320. {
  321. return CRC32(System.Text.Encoding.UTF8.GetBytes(value));
  322. }
  323. /// <summary>
  324. /// CRC32 hash. (32 bits).
  325. /// </summary>
  326. /// <returns></returns>
  327. public static ulong CRC64(byte[] buffer, int offset, int count)
  328. {
  329. ulong crcSeed = 0xFFFFFFFFFFFFFFFF;
  330. ulong crc = 0;
  331. if (offset < 0 || count < 0 || offset + count > buffer.Length)
  332. {
  333. throw new ArgumentOutOfRangeException();
  334. }
  335. crc ^= crcSeed;
  336. while (--count >= 0)
  337. {
  338. crc = CRC64Table[(byte)(crc ^ buffer[offset++])] ^ (crc >> 8);
  339. }
  340. crc ^= crcSeed;
  341. return crc;
  342. }
  343. /// <summary>
  344. /// CRC64 hash. (64 bits).
  345. /// </summary>
  346. /// <returns></returns>
  347. public static ulong CRC64(byte[] buffer)
  348. {
  349. return CRC64(buffer, 0, buffer.Length);
  350. }
  351. /// <summary>
  352. /// CRC32 hash. (32 bits).
  353. /// </summary>
  354. /// <param name="value">The value (UTF8 encoding).</param>
  355. /// <returns></returns>
  356. public static ulong CRC64(string value)
  357. {
  358. return CRC64(System.Text.Encoding.UTF8.GetBytes(value));
  359. }
  360. /// <summary>
  361. /// RSAs the specified buffer.
  362. /// </summary>
  363. /// <param name="buffer">The buffer.</param>
  364. /// <param name="hashAlgorithm">The hash algorithm (SHA1, MD5, RIPEMD160).</param>
  365. /// <param name="xmlPrivateKey">The XML private key.</param>
  366. /// <returns></returns>
  367. public static string RSA(byte[] buffer, string hashAlgorithm, string xmlPrivateKey)
  368. {
  369. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
  370. {
  371. rsa.FromXmlString(xmlPrivateKey);
  372. var halg = CryptoConfig.MapNameToOID(hashAlgorithm);
  373. if (halg == null) return string.Empty;
  374. return Convert.ToBase64String(rsa.SignData(buffer, halg));
  375. }
  376. }
  377. /// <summary>
  378. /// Generates the RSA private key.
  379. /// </summary>
  380. /// <param name="dwKeySize">Size of the dw key.</param>
  381. /// <returns></returns>
  382. public static string GenerateRSAPrivateKey(int dwKeySize)
  383. {
  384. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(dwKeySize))
  385. {
  386. return rsa.ToXmlString(true);
  387. }
  388. }
  389. /// <summary>
  390. /// RSAs the specified value.
  391. /// </summary>
  392. /// <param name="value">The value.</param>
  393. /// <param name="hashAlgorithm">The hash algorithm.</param>
  394. /// <param name="xmlPrivateKey">The XML private key.</param>
  395. /// <returns></returns>
  396. public static string RSA(string value, string hashAlgorithm, string xmlPrivateKey)
  397. {
  398. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
  399. {
  400. rsa.FromXmlString(xmlPrivateKey);
  401. var halg = CryptoConfig.MapNameToOID(hashAlgorithm);
  402. if (halg == null) return string.Empty;
  403. return Convert.ToBase64String(rsa.SignData(Encoding.UTF8.GetBytes(value), halg));
  404. }
  405. }
  406. /// <summary>
  407. /// Verifies the RSA.
  408. /// </summary>
  409. /// <param name="buffer">The buffer.</param>
  410. /// <param name="signature">The signature.</param>
  411. /// <param name="hashAlgorithm">The hash algorithm (SHA1, MD5, RIPEMD160).</param>
  412. /// <param name="xmlPublicKey">The XML public key.</param>
  413. /// <returns></returns>
  414. public static bool VerifyRSA(byte[] buffer, string signature, string hashAlgorithm, string xmlPublicKey)
  415. {
  416. using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
  417. {
  418. rsa.FromXmlString(xmlPublicKey);
  419. var halg = CryptoConfig.MapNameToOID(hashAlgorithm);
  420. if (halg == null) return false;
  421. return rsa.VerifyData(buffer, halg, Convert.FromBase64String(signature));
  422. }
  423. }
  424. /// <summary>
  425. /// RSAs the specified data.
  426. /// </summary>
  427. /// <param name="data">The data.</param>
  428. /// <param name="signature">The signature.</param>
  429. /// <param name="hashAlgorithm">The hash algorithm (SHA1, MD5, RIPEMD160).</param>
  430. /// <param name="xmlPublicKey">The XML public key.</param>
  431. /// <returns></returns>
  432. public static bool VerifyRSA(string data, string signature, string hashAlgorithm, string xmlPublicKey)
  433. {
  434. return VerifyRSA(Encoding.UTF8.GetBytes(data), signature, hashAlgorithm, xmlPublicKey);
  435. }
  436. /// <summary>
  437. /// Get the hash code of string.
  438. /// </summary>
  439. /// <param name="s">The s.</param>
  440. /// <returns></returns>
  441. public unsafe static uint StringHashCode(string s)
  442. {
  443. fixed (char* text = s)
  444. {
  445. char* chPtr = text;
  446. int num = 0x15051505;
  447. int num2 = num;
  448. int* numPtr = (int*)chPtr;
  449. for (int i = (int)s.Length; i > 0; i -= 4)
  450. {
  451. num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];
  452. if (i <= 2)
  453. {
  454. break;
  455. }
  456. num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];
  457. numPtr += 2;
  458. }
  459. return (uint)(num + (num2 * 0x5d588b65));
  460. }
  461. }
  462. }
  463. }