你好,游客 登录 注册 搜索
背景:
阅读新闻

从C#中移植CRC64(ECMA-182)的实现到 MySQL 存储过程 - Jerry.Wang(++) の 博客

[日期:2013-04-10] 来源:  作者: [字体: ]

CRC64(ECMA-182规范), 查表法在C#中的实现如下:

public static class CRC64
{
    private static readonly UInt64[] s_CRC64Table = new UInt64[256] 
    { 
        0x0000000000000000, 0x42F0E1EBA9EA3693, 0x85E1C3D753D46D26, 0xC711223CFA3E5BB5,
        0x493366450E42ECDF, 0x0BC387AEA7A8DA4C, 0xCCD2A5925D9681F9, 0x8E224479F47CB76A,
        0x9266CC8A1C85D9BE, 0xD0962D61B56FEF2D, 0x17870F5D4F51B498, 0x5577EEB6E6BB820B,
        0xDB55AACF12C73561, 0x99A54B24BB2D03F2, 0x5EB4691841135847, 0x1C4488F3E8F96ED4,
        0x663D78FF90E185EF, 0x24CD9914390BB37C, 0xE3DCBB28C335E8C9, 0xA12C5AC36ADFDE5A,
        0x2F0E1EBA9EA36930, 0x6DFEFF5137495FA3, 0xAAEFDD6DCD770416, 0xE81F3C86649D3285,
        0xF45BB4758C645C51, 0xB6AB559E258E6AC2, 0x71BA77A2DFB03177, 0x334A9649765A07E4,
        0xBD68D2308226B08E, 0xFF9833DB2BCC861D, 0x388911E7D1F2DDA8, 0x7A79F00C7818EB3B,
        0xCC7AF1FF21C30BDE, 0x8E8A101488293D4D, 0x499B3228721766F8, 0x0B6BD3C3DBFD506B,
        0x854997BA2F81E701, 0xC7B97651866BD192, 0x00A8546D7C558A27, 0x4258B586D5BFBCB4,
        0x5E1C3D753D46D260, 0x1CECDC9E94ACE4F3, 0xDBFDFEA26E92BF46, 0x990D1F49C77889D5,
        0x172F5B3033043EBF, 0x55DFBADB9AEE082C, 0x92CE98E760D05399, 0xD03E790CC93A650A,
        0xAA478900B1228E31, 0xE8B768EB18C8B8A2, 0x2FA64AD7E2F6E317, 0x6D56AB3C4B1CD584,
        0xE374EF45BF6062EE, 0xA1840EAE168A547D, 0x66952C92ECB40FC8, 0x2465CD79455E395B,
        0x3821458AADA7578F, 0x7AD1A461044D611C, 0xBDC0865DFE733AA9, 0xFF3067B657990C3A,
        0x711223CFA3E5BB50, 0x33E2C2240A0F8DC3, 0xF4F3E018F031D676, 0xB60301F359DBE0E5,
        0xDA050215EA6C212F, 0x98F5E3FE438617BC, 0x5FE4C1C2B9B84C09, 0x1D14202910527A9A,
        0x93366450E42ECDF0, 0xD1C685BB4DC4FB63, 0x16D7A787B7FAA0D6, 0x5427466C1E109645,
        0x4863CE9FF6E9F891, 0x0A932F745F03CE02, 0xCD820D48A53D95B7, 0x8F72ECA30CD7A324,
        0x0150A8DAF8AB144E, 0x43A04931514122DD, 0x84B16B0DAB7F7968, 0xC6418AE602954FFB,
        0xBC387AEA7A8DA4C0, 0xFEC89B01D3679253, 0x39D9B93D2959C9E6, 0x7B2958D680B3FF75,
        0xF50B1CAF74CF481F, 0xB7FBFD44DD257E8C, 0x70EADF78271B2539, 0x321A3E938EF113AA,
        0x2E5EB66066087D7E, 0x6CAE578BCFE24BED, 0xABBF75B735DC1058, 0xE94F945C9C3626CB,
        0x676DD025684A91A1, 0x259D31CEC1A0A732, 0xE28C13F23B9EFC87, 0xA07CF2199274CA14,
        0x167FF3EACBAF2AF1, 0x548F120162451C62, 0x939E303D987B47D7, 0xD16ED1D631917144,
        0x5F4C95AFC5EDC62E, 0x1DBC74446C07F0BD, 0xDAAD56789639AB08, 0x985DB7933FD39D9B,
        0x84193F60D72AF34F, 0xC6E9DE8B7EC0C5DC, 0x01F8FCB784FE9E69, 0x43081D5C2D14A8FA,
        0xCD2A5925D9681F90, 0x8FDAB8CE70822903, 0x48CB9AF28ABC72B6, 0x0A3B7B1923564425,
        0x70428B155B4EAF1E, 0x32B26AFEF2A4998D, 0xF5A348C2089AC238, 0xB753A929A170F4AB,
        0x3971ED50550C43C1, 0x7B810CBBFCE67552, 0xBC902E8706D82EE7, 0xFE60CF6CAF321874,
        0xE224479F47CB76A0, 0xA0D4A674EE214033, 0x67C58448141F1B86, 0x253565A3BDF52D15,
        0xAB1721DA49899A7F, 0xE9E7C031E063ACEC, 0x2EF6E20D1A5DF759, 0x6C0603E6B3B7C1CA,
        0xF6FAE5C07D3274CD, 0xB40A042BD4D8425E, 0x731B26172EE619EB, 0x31EBC7FC870C2F78,
        0xBFC9838573709812, 0xFD39626EDA9AAE81, 0x3A28405220A4F534, 0x78D8A1B9894EC3A7,
        0x649C294A61B7AD73, 0x266CC8A1C85D9BE0, 0xE17DEA9D3263C055, 0xA38D0B769B89F6C6,
        0x2DAF4F0F6FF541AC, 0x6F5FAEE4C61F773F, 0xA84E8CD83C212C8A, 0xEABE6D3395CB1A19,
        0x90C79D3FEDD3F122, 0xD2377CD44439C7B1, 0x15265EE8BE079C04, 0x57D6BF0317EDAA97,
        0xD9F4FB7AE3911DFD, 0x9B041A914A7B2B6E, 0x5C1538ADB04570DB, 0x1EE5D94619AF4648,
        0x02A151B5F156289C, 0x4051B05E58BC1E0F, 0x87409262A28245BA, 0xC5B073890B687329,
        0x4B9237F0FF14C443, 0x0962D61B56FEF2D0, 0xCE73F427ACC0A965, 0x8C8315CC052A9FF6,
        0x3A80143F5CF17F13, 0x7870F5D4F51B4980, 0xBF61D7E80F251235, 0xFD913603A6CF24A6,
        0x73B3727A52B393CC, 0x31439391FB59A55F, 0xF652B1AD0167FEEA, 0xB4A25046A88DC879,
        0xA8E6D8B54074A6AD, 0xEA16395EE99E903E, 0x2D071B6213A0CB8B, 0x6FF7FA89BA4AFD18,
        0xE1D5BEF04E364A72, 0xA3255F1BE7DC7CE1, 0x64347D271DE22754, 0x26C49CCCB40811C7,
        0x5CBD6CC0CC10FAFC, 0x1E4D8D2B65FACC6F, 0xD95CAF179FC497DA, 0x9BAC4EFC362EA149,
        0x158E0A85C2521623, 0x577EEB6E6BB820B0, 0x906FC95291867B05, 0xD29F28B9386C4D96,
        0xCEDBA04AD0952342, 0x8C2B41A1797F15D1, 0x4B3A639D83414E64, 0x09CA82762AAB78F7,
        0x87E8C60FDED7CF9D, 0xC51827E4773DF90E, 0x020905D88D03A2BB, 0x40F9E43324E99428,
        0x2CFFE7D5975E55E2, 0x6E0F063E3EB46371, 0xA91E2402C48A38C4, 0xEBEEC5E96D600E57,
        0x65CC8190991CB93D, 0x273C607B30F68FAE, 0xE02D4247CAC8D41B, 0xA2DDA3AC6322E288,
        0xBE992B5F8BDB8C5C, 0xFC69CAB42231BACF, 0x3B78E888D80FE17A, 0x7988096371E5D7E9,
        0xF7AA4D1A85996083, 0xB55AACF12C735610, 0x724B8ECDD64D0DA5, 0x30BB6F267FA73B36,
        0x4AC29F2A07BFD00D, 0x08327EC1AE55E69E, 0xCF235CFD546BBD2B, 0x8DD3BD16FD818BB8,
        0x03F1F96F09FD3CD2, 0x41011884A0170A41, 0x86103AB85A2951F4, 0xC4E0DB53F3C36767,
        0xD8A453A01B3A09B3, 0x9A54B24BB2D03F20, 0x5D45907748EE6495, 0x1FB5719CE1045206,
        0x919735E51578E56C, 0xD367D40EBC92D3FF, 0x1476F63246AC884A, 0x568617D9EF46BED9,
        0xE085162AB69D5E3C, 0xA275F7C11F7768AF, 0x6564D5FDE549331A, 0x279434164CA30589,
        0xA9B6706FB8DFB2E3, 0xEB46918411358470, 0x2C57B3B8EB0BDFC5, 0x6EA7525342E1E956,
        0x72E3DAA0AA188782, 0x30133B4B03F2B111, 0xF7021977F9CCEAA4, 0xB5F2F89C5026DC37,
        0x3BD0BCE5A45A6B5D, 0x79205D0E0DB05DCE, 0xBE317F32F78E067B, 0xFCC19ED95E6430E8,
        0x86B86ED5267CDBD3, 0xC4488F3E8F96ED40, 0x0359AD0275A8B6F5, 0x41A94CE9DC428066,
        0xCF8B0890283E370C, 0x8D7BE97B81D4019F, 0x4A6ACB477BEA5A2A, 0x089A2AACD2006CB9,
        0x14DEA25F3AF9026D, 0x562E43B4931334FE, 0x913F6188692D6F4B, 0xD3CF8063C0C759D8,
        0x5DEDC41A34BBEEB2, 0x1F1D25F19D51D821, 0xD80C07CD676F8394, 0x9AFCE626CE85B507 
    };

    public static ulong Compute(byte[] data)
    {
        ulong crc = 0xffffffffffffffff;
        for (int i = 0; i < data.Length; i++)
        {
            uint tableIndex = (((uint)(crc >> 56)) ^ data[i]) & 0xff;
            crc = s_CRC64Table[tableIndex] ^ (crc << 8);
        }
        return (crc ^ 0xffffffffffffffff);
    }

    public static ulong ComputeAsAsciiString(string text)
    {
        if (text == null)
            text = string.Empty;
        byte[] buffer = Encoding.ASCII.GetBytes(text.ToLowerInvariant());
        return Compute(buffer);
    }


}

移植到MySQL存储过程

-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: Caculate CRC64 for ASCII string (will convert to lowercase automaticlly)
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE PROCEDURE `crc64` (
IN $str VARCHAR(1000)
)
BEGIN


DECLARE $crc BIGINT UNSIGNED DEFAULT 0xffffffffffffffff;
DECLARE $i INT DEFAULT 0;
DECLARE $len INT DEFAULT CHAR_LENGTH($str);
DECLARE $c INT;
DECLARE $idx INT UNSIGNED;
DECLARE $v BIGINT UNSIGNED;


/* convert to lower case */
SET $str = LCASE($str);


WHILE $i < $len DO
	SET $c = ASCII(MID( $str, $i + 1, 1));
	
	SET $idx = ((($crc >> 56) & 255) ^ $c) & 255;

	SELECT ( CASE $idx
	WHEN 0 THEN 0
	WHEN 1 THEN 4823603603198064275
	WHEN 2 THEN 9647207206396128550
	WHEN 3 THEN 14344283933443513269
	WHEN 4 THEN 5274672035359026399
	WHEN 5 THEN 847670339082705484
	WHEN 6 THEN 14759040976900489721
	WHEN 7 THEN 10241823793177474922
	WHEN 8 THEN 10549344070718052798
	WHEN 9 THEN 15030250704074698541
	WHEN 10 THEN 1695340678165410968
	WHEN 11 THEN 6158653484774949387
	WHEN 12 THEN 15804726273676621153
	WHEN 13 THEN 11071337880091427826
	WHEN 14 THEN 6824194888265062471
	WHEN 15 THEN 2036903512645398228
	WHEN 16 THEN 7367177604490692079
	WHEN 17 THEN 2651944067726553980
	WHEN 18 THEN 16419204125234161865
	WHEN 19 THEN 11613757334439845466
	WHEN 20 THEN 3390681356330821936
	WHEN 21 THEN 7926053118503640995
	WHEN 22 THEN 12317306969549898774
	WHEN 23 THEN 16726154088988619397
	WHEN 24 THEN 17607865585094646865
	WHEN 25 THEN 13162708473643690690
	WHEN 26 THEN 8194994013375312247
	WHEN 27 THEN 3695931686473304036
	WHEN 28 THEN 13648389776530124942
	WHEN 29 THEN 18417527692557321757
	WHEN 30 THEN 4073807025290796456
	WHEN 31 THEN 8825348881154370363
	WHEN 32 THEN 14734355208981384158
	WHEN 33 THEN 10271039580541631821
	WHEN 34 THEN 5303888135453107960
	WHEN 35 THEN 822984195088142443
	WHEN 36 THEN 9604374506261047041
	WHEN 37 THEN 14391664176758772114
	WHEN 38 THEN 47380625301539367
	WHEN 39 THEN 4780770595170139316
	WHEN 40 THEN 6781362712661643872
	WHEN 41 THEN 2084283301222999283
	WHEN 42 THEN 15852106237007281990
	WHEN 43 THEN 11028505464239851989
	WHEN 44 THEN 1670654249350217407
	WHEN 45 THEN 6187869865390245932
	WHEN 46 THEN 10578560694269006745
	WHEN 47 THEN 15005564104267687178
	WHEN 48 THEN 12269926345859042865
	WHEN 49 THEN 16768987096479742114
	WHEN 50 THEN 3433514057002836759
	WHEN 51 THEN 7878672873577829764
	WHEN 52 THEN 16389988026750624494
	WHEN 53 THEN 11638443477897467005
	WHEN 54 THEN 7391863372946608072
	WHEN 55 THEN 2622728278751721819
	WHEN 56 THEN 4044590402276644751
	WHEN 57 THEN 8850035479350698268
	WHEN 58 THEN 13673076206955870889
	WHEN 59 THEN 18388311311405091898
	WHEN 60 THEN 8147614050581592912
	WHEN 61 THEN 3738764100714335683
	WHEN 62 THEN 17650697762308740726
	WHEN 63 THEN 13115328684529279205
	WHEN 64 THEN 15709965168302367023
	WHEN 65 THEN 11021966344253216700
	WHEN 66 THEN 6909860770376862729
	WHEN 67 THEN 2095335087373712026
	WHEN 68 THEN 10607776270906215920
	WHEN 69 THEN 15115916238825782115
	WHEN 70 THEN 1645968390176284886
	WHEN 71 THEN 6063892853452478021
	WHEN 72 THEN 5216239979862816913
	WHEN 73 THEN 762004938812542466
	WHEN 74 THEN 14808413130408695223
	WHEN 75 THEN 10336584279807992612
	WHEN 76 THEN 94761250603078734
	WHEN 77 THEN 4872975272980325085
	WHEN 78 THEN 9561541190340278632
	WHEN 79 THEN 14285852213486374907
	WHEN 80 THEN 13562725425323287744
	WHEN 81 THEN 18359094313119879763
	WHEN 82 THEN 4168566602445998566
	WHEN 83 THEN 8874722219015798645
	WHEN 84 THEN 17657238303940757535
	WHEN 85 THEN 13257468400305012364
	WHEN 86 THEN 8136561383943382329
	WHEN 87 THEN 3610266854770152362
	WHEN 88 THEN 3341308498700434814
	WHEN 89 THEN 7831293060043656173
	WHEN 90 THEN 12375739730780491864
	WHEN 91 THEN 16811819059476047563
	WHEN 92 THEN 7452841817450123681
	WHEN 93 THEN 2710377314828461874
	WHEN 94 THEN 16324444680414493831
	WHEN 95 THEN 11564384134825822740
	WHEN 96 THEN 1621282580641819377
	WHEN 97 THEN 6093108618008534114
	WHEN 98 THEN 10636992411005044695
	WHEN 99 THEN 15091230119249932612
	WHEN 100 THEN 6867028114005673518
	WHEN 101 THEN 2142715359940571325
	WHEN 102 THEN 15757345747155659528
	WHEN 103 THEN 10979133309658045851
	WHEN 104 THEN 9518708972583580495
	WHEN 105 THEN 14333231979791697372
	WHEN 106 THEN 142141253402664553
	WHEN 107 THEN 4830142882085382394
	WHEN 108 THEN 14783726745893216144
	WHEN 109 THEN 10365800689136969987
	WHEN 110 THEN 5245456557503443638
	WHEN 111 THEN 737318311902463013
	WHEN 112 THEN 8089180804553289502
	WHEN 113 THEN 3653099890976004493
	WHEN 114 THEN 17700070958701396536
	WHEN 115 THEN 13210088128275084459
	WHEN 116 THEN 4139350461810230209
	WHEN 117 THEN 8899408340202190162
	WHEN 118 THEN 13587411233247080167
	WHEN 119 THEN 18329878549100632180
	WHEN 120 THEN 16295228101163185824
	WHEN 121 THEN 11589070762272702515
	WHEN 122 THEN 7477528201428671366
	WHEN 123 THEN 2681160907110034709
	WHEN 124 THEN 12328359726370364031
	WHEN 125 THEN 16854651450907929836
	WHEN 126 THEN 3384140715920324441
	WHEN 127 THEN 7783913295349006794
	WHEN 128 THEN 17796789492404876493
	WHEN 129 THEN 12973186262895182430
	WHEN 130 THEN 8294265019745835499
	WHEN 131 THEN 3597188614796881784
	WHEN 132 THEN 13819721540753725458
	WHEN 133 THEN 18246723593521770113
	WHEN 134 THEN 4190670174747424052
	WHEN 135 THEN 8707887697765516199
	WHEN 136 THEN 7249714899603402099
	WHEN 137 THEN 2768808468102880224
	WHEN 138 THEN 16248400991498780757
	WHEN 139 THEN 11785088403942012614
	WHEN 140 THEN 3291936780352569772
	WHEN 141 THEN 8025325358597240639
	WHEN 142 THEN 12127785706904956042
	WHEN 143 THEN 16915077318774037017
	WHEN 144 THEN 10432479959725633826
	WHEN 145 THEN 15147713122803500977
	WHEN 146 THEN 1524009877625084932
	WHEN 147 THEN 6329456346323069591
	WHEN 148 THEN 15705454305770282493
	WHEN 149 THEN 11170082187107838830
	WHEN 150 THEN 6635271944638132443
	WHEN 151 THEN 2226424485906433608
	WHEN 152 THEN 189522501206157468
	WHEN 153 THEN 4634679410803088911
	WHEN 154 THEN 9745950545960650170
	WHEN 155 THEN 14245012653811987241
	WHEN 156 THEN 5445476407655580739
	WHEN 157 THEN 676338306971005648
	WHEN 158 THEN 14876502445374089573
	WHEN 159 THEN 10124960353263198198
	WHEN 160 THEN 4215391513593610003
	WHEN 161 THEN 8678706776937023872
	WHEN 162 THEN 13790540925671641653
	WHEN 163 THEN 18271444552530207910
	WHEN 164 THEN 8337133204891997132
	WHEN 165 THEN 3549843186494580063
	WHEN 166 THEN 17749444438031597290
	WHEN 167 THEN 13016054137459951737
	WHEN 168 THEN 12170653315997410989
	WHEN 169 THEN 16867732534171963454
	WHEN 170 THEN 3244592164593781643
	WHEN 171 THEN 8068192726900473112
	WHEN 172 THEN 16273122767886764658
	WHEN 173 THEN 11755906975779290337
	WHEN 174 THEN 7220533709540304724
	WHEN 175 THEN 2793530071884239303
	WHEN 176 THEN 6682616997400869628
	WHEN 177 THEN 2183556611878603887
	WHEN 178 THEN 15662586120087312346
	WHEN 179 THEN 11217427617020813641
	WHEN 180 THEN 1553190491096487459
	WHEN 181 THEN 6304735387851432112
	WHEN 182 THEN 10407758620342516485
	WHEN 183 THEN 15176894045242543510
	WHEN 184 THEN 14905683634900247362
	WHEN 185 THEN 10100238751092381137
	WHEN 186 THEN 5420754629656923748
	WHEN 187 THEN 705519735670536439
	WHEN 188 THEN 9793295161182637981
	WHEN 189 THEN 14202145287119436046
	WHEN 190 THEN 146654890503152315
	WHEN 191 THEN 4682024195942093864
	WHEN 192 THEN 3242565161283638754
	WHEN 193 THEN 7930564333232481137
	WHEN 194 THEN 12186217236017068228
	WHEN 195 THEN 17000743249723264599
	WHEN 196 THEN 7335380351123765565
	WHEN 197 THEN 2827240748300537774
	WHEN 198 THEN 16153640314560107547
	WHEN 199 THEN 11735716164790313608
	WHEN 200 THEN 13734056228011347036
	WHEN 201 THEN 18188291445129067215
	WHEN 202 THEN 4285430719881142650
	WHEN 203 THEN 8757259798139230185
	WHEN 204 THEN 17846161249714921603
	WHEN 205 THEN 13067947420601767440
	WHEN 206 THEN 8235833358291897765
	WHEN 207 THEN 3511522545606540086
	WHEN 208 THEN 5387043107155988493
	WHEN 209 THEN 590673871457609374
	WHEN 210 THEN 14925875833148783915
	WHEN 211 THEN 10219719885873843128
	WHEN 212 THEN 284282506805329106
	WHEN 213 THEN 4684052045342640705
	WHEN 214 THEN 9660285764170764788
	WHEN 215 THEN 14186579979835500391
	WHEN 216 THEN 15610694155489642931
	WHEN 217 THEN 11120709418076880672
	WHEN 218 THEN 6720936860919424149
	WHEN 219 THEN 2284857304564388358
	WHEN 220 THEN 10490913115006887276
	WHEN 221 THEN 15233377424362361855
	WHEN 222 THEN 1474636623804926026
	WHEN 223 THEN 6234696958930763481
	WHEN 224 THEN 16178361609106579004
	WHEN 225 THEN 11706535215248140463
	WHEN 226 THEN 7306199781952008986
	WHEN 227 THEN 2851961734412043657
	WHEN 228 THEN 12229085463316509411
	WHEN 229 THEN 16953397843693241456
	WHEN 230 THEN 3195220067441434565
	WHEN 231 THEN 7973432182840617302
	WHEN 232 THEN 8278700923620460418
	WHEN 233 THEN 3464177731752866065
	WHEN 234 THEN 17798816680404380324
	WHEN 235 THEN 13110814815472245815
	WHEN 236 THEN 4310152537884486493
	WHEN 237 THEN 8728078392784608718
	WHEN 238 THEN 13704874997943502459
	WHEN 239 THEN 18213013024491712744
	WHEN 240 THEN 9707630858549910483
	WHEN 241 THEN 14143712128616820032
	WHEN 242 THEN 241414281116563189
	WHEN 243 THEN 4731397450835853414
	WHEN 244 THEN 14955056402857342732
	WHEN 245 THEN 10194998898151653791
	WHEN 246 THEN 5362321814220069418
	WHEN 247 THEN 619854820462849209
	WHEN 248 THEN 1503817855483314797
	WHEN 249 THEN 6209975379031176446
	WHEN 250 THEN 10466191297540353867
	WHEN 251 THEN 15262558828106308056
	WHEN 252 THEN 6768281431840648882
	WHEN 253 THEN 2241989909157107745
	WHEN 254 THEN 15567826590698013588
	WHEN 255 THEN 11168054230320002311
	ELSE 0
	END) INTO $v;

	SET $crc = $v ^ (($crc & 72057594037927935) << 8);
	
	SET $i = $i + 1;
END WHILE;

SELECT ($crc ^ 0xffffffffffffffff);

END






收藏 推荐 打印 | 录入:admin | 阅读:
相关新闻