1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2008-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19%% 20 21%% 22%% Reference implementations of crc32, adler32 and md5 in erlang. Used 23%% by crypto_SUITE. 24%% 25 26-module(crypto_reference). 27 28-export([adler32/1, crc32/1, md5_init/0, md5_update/2, md5_final/1]). 29-export([crc32_table/0, reflect8_table/0]). 30 31-define(BASE, 65521). 32-define(NMAX, 5552). 33 34-define(AINIT,<<0,0,0,1>>). 35 36adler32(Bin) when is_binary(Bin) -> 37 B2 = adler32(Bin,0,1), 38 <<Sum:32/big>> = B2, 39 Sum; 40 41adler32(IoList) -> 42 adler32(erlang:iolist_to_binary(IoList)). 43 44adler32(<<>>,B,A) -> 45 <<B:16/big,A:16/big>>; 46 47adler32(<<CH:8,T/binary>>,B,A) -> 48 NewA = (A+CH) rem ?BASE, 49 NewB = (B+NewA) rem ?BASE, 50 adler32(T,NewB,NewA). 51 52-define(FINAL_XOR_VALUE,16#FFFFFFFF). 53-define(INITIAL_REMAINDER,16#FFFFFFFF). 54 55reflect_bin(<<>>,Res) -> 56 Res; 57reflect_bin(<<X:1,Rest/bitstring>>,BS) -> 58 reflect_bin(Rest,<<X:1,BS/bitstring>>). 59reflect(Data,8) -> 60 reflect8(Data); 61reflect(Data,32) -> 62 <<A:8,B:8,C:8,D:8>> = <<Data:32>>, 63 ND = reflect8(D), 64 NC = reflect8(C), 65 NB = reflect8(B), 66 NA = reflect8(A), 67 <<Result:32>> = <<ND:8,NC:8,NB:8,NA:8>>, 68 Result; 69reflect(Data,Size) -> 70 <<NewData:Size>> = reflect_bin(<<Data:Size>>,<<>>), 71 NewData. 72crc32(<<>>,Remainder) -> 73 reflect(Remainder,32) bxor ?FINAL_XOR_VALUE; 74crc32(<<CH:8,T/binary>>,Remainder) -> 75 Data = reflect(CH,8) bxor (Remainder bsr 24), 76 NewRem = crcTab32(Data) bxor ((Remainder bsl 8) band 16#FFFFFFFF), 77% io:format("CH = ~p (~p)~n",[CH,reflect(CH,8)]), 78% io:format("No reflect = ~p~n",[(CH bxor (Remainder bsr 24))]), 79% io:format("Data = ~p, NewRem = ~p~n",[Data,NewRem]), 80 crc32(T,NewRem). 81 82crc32(Bin) when is_binary(Bin) -> 83 crc32(Bin,?INITIAL_REMAINDER); 84crc32(L) -> 85 crc32(erlang:iolist_to_binary(L)). 86 87bitmod2(0,Remainder,_Topbit,_Polynomial,_Mask) -> 88 %io:format("~p ",[Remainder]), 89 Remainder; 90bitmod2(N,Remainder,Topbit,Polynomial,Mask) -> 91 %io:format("~p ",[Remainder]), 92 case (Remainder band Topbit) of 93 0 -> 94 bitmod2(N-1,(Remainder bsl 1) band Mask,Topbit,Polynomial,Mask); 95 _ -> 96 bitmod2(N-1,((Remainder bsl 1) bxor Polynomial) band Mask,Topbit,Polynomial,Mask) 97 end. 98 99mask(CrcSize) -> 100 16#FFFFFFFF bsr (32 - CrcSize). 101 102calc_crc_table(256,_CrcSize,_Polynomial) -> 103 ok; 104calc_crc_table(Dividend,CrcSize,Polynomial) -> 105 Mask = mask(CrcSize), 106 Remainder = (Dividend bsl (CrcSize - 8)) band Mask, 107 TopBit = 1 bsl (CrcSize - 1), 108 Rem2 = bitmod2(8,Remainder,TopBit,Polynomial,Mask), 109 io:format("crcTab~p(~p) -> ~p;~n",[CrcSize,Dividend,Rem2]), 110 calc_crc_table(Dividend+1,CrcSize,Polynomial). 111 112crc32_table() -> 113 calc_crc_table(0,32,16#04C11DB7). 114 115reflect8_table(256) -> 116 ok; 117reflect8_table(N) -> 118 X = reflect(N,8), 119 io:format("reflect8(~p) -> ~p;~n",[N,X]), 120 reflect8_table(N+1). 121reflect8_table() -> 122 reflect8_table(0). 123 124%CRC32 table calculated with crc32_table above 125crcTab32(0) -> 0; 126crcTab32(1) -> 79764919; 127crcTab32(2) -> 159529838; 128crcTab32(3) -> 222504665; 129crcTab32(4) -> 319059676; 130crcTab32(5) -> 398814059; 131crcTab32(6) -> 445009330; 132crcTab32(7) -> 507990021; 133crcTab32(8) -> 638119352; 134crcTab32(9) -> 583659535; 135crcTab32(10) -> 797628118; 136crcTab32(11) -> 726387553; 137crcTab32(12) -> 890018660; 138crcTab32(13) -> 835552979; 139crcTab32(14) -> 1015980042; 140crcTab32(15) -> 944750013; 141crcTab32(16) -> 1276238704; 142crcTab32(17) -> 1221641927; 143crcTab32(18) -> 1167319070; 144crcTab32(19) -> 1095957929; 145crcTab32(20) -> 1595256236; 146crcTab32(21) -> 1540665371; 147crcTab32(22) -> 1452775106; 148crcTab32(23) -> 1381403509; 149crcTab32(24) -> 1780037320; 150crcTab32(25) -> 1859660671; 151crcTab32(26) -> 1671105958; 152crcTab32(27) -> 1733955601; 153crcTab32(28) -> 2031960084; 154crcTab32(29) -> 2111593891; 155crcTab32(30) -> 1889500026; 156crcTab32(31) -> 1952343757; 157crcTab32(32) -> 2552477408; 158crcTab32(33) -> 2632100695; 159crcTab32(34) -> 2443283854; 160crcTab32(35) -> 2506133561; 161crcTab32(36) -> 2334638140; 162crcTab32(37) -> 2414271883; 163crcTab32(38) -> 2191915858; 164crcTab32(39) -> 2254759653; 165crcTab32(40) -> 3190512472; 166crcTab32(41) -> 3135915759; 167crcTab32(42) -> 3081330742; 168crcTab32(43) -> 3009969537; 169crcTab32(44) -> 2905550212; 170crcTab32(45) -> 2850959411; 171crcTab32(46) -> 2762807018; 172crcTab32(47) -> 2691435357; 173crcTab32(48) -> 3560074640; 174crcTab32(49) -> 3505614887; 175crcTab32(50) -> 3719321342; 176crcTab32(51) -> 3648080713; 177crcTab32(52) -> 3342211916; 178crcTab32(53) -> 3287746299; 179crcTab32(54) -> 3467911202; 180crcTab32(55) -> 3396681109; 181crcTab32(56) -> 4063920168; 182crcTab32(57) -> 4143685023; 183crcTab32(58) -> 4223187782; 184crcTab32(59) -> 4286162673; 185crcTab32(60) -> 3779000052; 186crcTab32(61) -> 3858754371; 187crcTab32(62) -> 3904687514; 188crcTab32(63) -> 3967668269; 189crcTab32(64) -> 881225847; 190crcTab32(65) -> 809987520; 191crcTab32(66) -> 1023691545; 192crcTab32(67) -> 969234094; 193crcTab32(68) -> 662832811; 194crcTab32(69) -> 591600412; 195crcTab32(70) -> 771767749; 196crcTab32(71) -> 717299826; 197crcTab32(72) -> 311336399; 198crcTab32(73) -> 374308984; 199crcTab32(74) -> 453813921; 200crcTab32(75) -> 533576470; 201crcTab32(76) -> 25881363; 202crcTab32(77) -> 88864420; 203crcTab32(78) -> 134795389; 204crcTab32(79) -> 214552010; 205crcTab32(80) -> 2023205639; 206crcTab32(81) -> 2086057648; 207crcTab32(82) -> 1897238633; 208crcTab32(83) -> 1976864222; 209crcTab32(84) -> 1804852699; 210crcTab32(85) -> 1867694188; 211crcTab32(86) -> 1645340341; 212crcTab32(87) -> 1724971778; 213crcTab32(88) -> 1587496639; 214crcTab32(89) -> 1516133128; 215crcTab32(90) -> 1461550545; 216crcTab32(91) -> 1406951526; 217crcTab32(92) -> 1302016099; 218crcTab32(93) -> 1230646740; 219crcTab32(94) -> 1142491917; 220crcTab32(95) -> 1087903418; 221crcTab32(96) -> 2896545431; 222crcTab32(97) -> 2825181984; 223crcTab32(98) -> 2770861561; 224crcTab32(99) -> 2716262478; 225crcTab32(100) -> 3215044683; 226crcTab32(101) -> 3143675388; 227crcTab32(102) -> 3055782693; 228crcTab32(103) -> 3001194130; 229crcTab32(104) -> 2326604591; 230crcTab32(105) -> 2389456536; 231crcTab32(106) -> 2200899649; 232crcTab32(107) -> 2280525302; 233crcTab32(108) -> 2578013683; 234crcTab32(109) -> 2640855108; 235crcTab32(110) -> 2418763421; 236crcTab32(111) -> 2498394922; 237crcTab32(112) -> 3769900519; 238crcTab32(113) -> 3832873040; 239crcTab32(114) -> 3912640137; 240crcTab32(115) -> 3992402750; 241crcTab32(116) -> 4088425275; 242crcTab32(117) -> 4151408268; 243crcTab32(118) -> 4197601365; 244crcTab32(119) -> 4277358050; 245crcTab32(120) -> 3334271071; 246crcTab32(121) -> 3263032808; 247crcTab32(122) -> 3476998961; 248crcTab32(123) -> 3422541446; 249crcTab32(124) -> 3585640067; 250crcTab32(125) -> 3514407732; 251crcTab32(126) -> 3694837229; 252crcTab32(127) -> 3640369242; 253crcTab32(128) -> 1762451694; 254crcTab32(129) -> 1842216281; 255crcTab32(130) -> 1619975040; 256crcTab32(131) -> 1682949687; 257crcTab32(132) -> 2047383090; 258crcTab32(133) -> 2127137669; 259crcTab32(134) -> 1938468188; 260crcTab32(135) -> 2001449195; 261crcTab32(136) -> 1325665622; 262crcTab32(137) -> 1271206113; 263crcTab32(138) -> 1183200824; 264crcTab32(139) -> 1111960463; 265crcTab32(140) -> 1543535498; 266crcTab32(141) -> 1489069629; 267crcTab32(142) -> 1434599652; 268crcTab32(143) -> 1363369299; 269crcTab32(144) -> 622672798; 270crcTab32(145) -> 568075817; 271crcTab32(146) -> 748617968; 272crcTab32(147) -> 677256519; 273crcTab32(148) -> 907627842; 274crcTab32(149) -> 853037301; 275crcTab32(150) -> 1067152940; 276crcTab32(151) -> 995781531; 277crcTab32(152) -> 51762726; 278crcTab32(153) -> 131386257; 279crcTab32(154) -> 177728840; 280crcTab32(155) -> 240578815; 281crcTab32(156) -> 269590778; 282crcTab32(157) -> 349224269; 283crcTab32(158) -> 429104020; 284crcTab32(159) -> 491947555; 285crcTab32(160) -> 4046411278; 286crcTab32(161) -> 4126034873; 287crcTab32(162) -> 4172115296; 288crcTab32(163) -> 4234965207; 289crcTab32(164) -> 3794477266; 290crcTab32(165) -> 3874110821; 291crcTab32(166) -> 3953728444; 292crcTab32(167) -> 4016571915; 293crcTab32(168) -> 3609705398; 294crcTab32(169) -> 3555108353; 295crcTab32(170) -> 3735388376; 296crcTab32(171) -> 3664026991; 297crcTab32(172) -> 3290680682; 298crcTab32(173) -> 3236090077; 299crcTab32(174) -> 3449943556; 300crcTab32(175) -> 3378572211; 301crcTab32(176) -> 3174993278; 302crcTab32(177) -> 3120533705; 303crcTab32(178) -> 3032266256; 304crcTab32(179) -> 2961025959; 305crcTab32(180) -> 2923101090; 306crcTab32(181) -> 2868635157; 307crcTab32(182) -> 2813903052; 308crcTab32(183) -> 2742672763; 309crcTab32(184) -> 2604032198; 310crcTab32(185) -> 2683796849; 311crcTab32(186) -> 2461293480; 312crcTab32(187) -> 2524268063; 313crcTab32(188) -> 2284983834; 314crcTab32(189) -> 2364738477; 315crcTab32(190) -> 2175806836; 316crcTab32(191) -> 2238787779; 317crcTab32(192) -> 1569362073; 318crcTab32(193) -> 1498123566; 319crcTab32(194) -> 1409854455; 320crcTab32(195) -> 1355396672; 321crcTab32(196) -> 1317987909; 322crcTab32(197) -> 1246755826; 323crcTab32(198) -> 1192025387; 324crcTab32(199) -> 1137557660; 325crcTab32(200) -> 2072149281; 326crcTab32(201) -> 2135122070; 327crcTab32(202) -> 1912620623; 328crcTab32(203) -> 1992383480; 329crcTab32(204) -> 1753615357; 330crcTab32(205) -> 1816598090; 331crcTab32(206) -> 1627664531; 332crcTab32(207) -> 1707420964; 333crcTab32(208) -> 295390185; 334crcTab32(209) -> 358241886; 335crcTab32(210) -> 404320391; 336crcTab32(211) -> 483945776; 337crcTab32(212) -> 43990325; 338crcTab32(213) -> 106832002; 339crcTab32(214) -> 186451547; 340crcTab32(215) -> 266083308; 341crcTab32(216) -> 932423249; 342crcTab32(217) -> 861060070; 343crcTab32(218) -> 1041341759; 344crcTab32(219) -> 986742920; 345crcTab32(220) -> 613929101; 346crcTab32(221) -> 542559546; 347crcTab32(222) -> 756411363; 348crcTab32(223) -> 701822548; 349crcTab32(224) -> 3316196985; 350crcTab32(225) -> 3244833742; 351crcTab32(226) -> 3425377559; 352crcTab32(227) -> 3370778784; 353crcTab32(228) -> 3601682597; 354crcTab32(229) -> 3530312978; 355crcTab32(230) -> 3744426955; 356crcTab32(231) -> 3689838204; 357crcTab32(232) -> 3819031489; 358crcTab32(233) -> 3881883254; 359crcTab32(234) -> 3928223919; 360crcTab32(235) -> 4007849240; 361crcTab32(236) -> 4037393693; 362crcTab32(237) -> 4100235434; 363crcTab32(238) -> 4180117107; 364crcTab32(239) -> 4259748804; 365crcTab32(240) -> 2310601993; 366crcTab32(241) -> 2373574846; 367crcTab32(242) -> 2151335527; 368crcTab32(243) -> 2231098320; 369crcTab32(244) -> 2596047829; 370crcTab32(245) -> 2659030626; 371crcTab32(246) -> 2470359227; 372crcTab32(247) -> 2550115596; 373crcTab32(248) -> 2947551409; 374crcTab32(249) -> 2876312838; 375crcTab32(250) -> 2788305887; 376crcTab32(251) -> 2733848168; 377crcTab32(252) -> 3165939309; 378crcTab32(253) -> 3094707162; 379crcTab32(254) -> 3040238851; 380crcTab32(255) -> 2985771188; 381crcTab32(_) -> exit(not_a_byte). 382 383%% 384%% Reflect8 table generated with code above crcTab32 385%% 386 387reflect8(0) -> 0; 388reflect8(1) -> 128; 389reflect8(2) -> 64; 390reflect8(3) -> 192; 391reflect8(4) -> 32; 392reflect8(5) -> 160; 393reflect8(6) -> 96; 394reflect8(7) -> 224; 395reflect8(8) -> 16; 396reflect8(9) -> 144; 397reflect8(10) -> 80; 398reflect8(11) -> 208; 399reflect8(12) -> 48; 400reflect8(13) -> 176; 401reflect8(14) -> 112; 402reflect8(15) -> 240; 403reflect8(16) -> 8; 404reflect8(17) -> 136; 405reflect8(18) -> 72; 406reflect8(19) -> 200; 407reflect8(20) -> 40; 408reflect8(21) -> 168; 409reflect8(22) -> 104; 410reflect8(23) -> 232; 411reflect8(24) -> 24; 412reflect8(25) -> 152; 413reflect8(26) -> 88; 414reflect8(27) -> 216; 415reflect8(28) -> 56; 416reflect8(29) -> 184; 417reflect8(30) -> 120; 418reflect8(31) -> 248; 419reflect8(32) -> 4; 420reflect8(33) -> 132; 421reflect8(34) -> 68; 422reflect8(35) -> 196; 423reflect8(36) -> 36; 424reflect8(37) -> 164; 425reflect8(38) -> 100; 426reflect8(39) -> 228; 427reflect8(40) -> 20; 428reflect8(41) -> 148; 429reflect8(42) -> 84; 430reflect8(43) -> 212; 431reflect8(44) -> 52; 432reflect8(45) -> 180; 433reflect8(46) -> 116; 434reflect8(47) -> 244; 435reflect8(48) -> 12; 436reflect8(49) -> 140; 437reflect8(50) -> 76; 438reflect8(51) -> 204; 439reflect8(52) -> 44; 440reflect8(53) -> 172; 441reflect8(54) -> 108; 442reflect8(55) -> 236; 443reflect8(56) -> 28; 444reflect8(57) -> 156; 445reflect8(58) -> 92; 446reflect8(59) -> 220; 447reflect8(60) -> 60; 448reflect8(61) -> 188; 449reflect8(62) -> 124; 450reflect8(63) -> 252; 451reflect8(64) -> 2; 452reflect8(65) -> 130; 453reflect8(66) -> 66; 454reflect8(67) -> 194; 455reflect8(68) -> 34; 456reflect8(69) -> 162; 457reflect8(70) -> 98; 458reflect8(71) -> 226; 459reflect8(72) -> 18; 460reflect8(73) -> 146; 461reflect8(74) -> 82; 462reflect8(75) -> 210; 463reflect8(76) -> 50; 464reflect8(77) -> 178; 465reflect8(78) -> 114; 466reflect8(79) -> 242; 467reflect8(80) -> 10; 468reflect8(81) -> 138; 469reflect8(82) -> 74; 470reflect8(83) -> 202; 471reflect8(84) -> 42; 472reflect8(85) -> 170; 473reflect8(86) -> 106; 474reflect8(87) -> 234; 475reflect8(88) -> 26; 476reflect8(89) -> 154; 477reflect8(90) -> 90; 478reflect8(91) -> 218; 479reflect8(92) -> 58; 480reflect8(93) -> 186; 481reflect8(94) -> 122; 482reflect8(95) -> 250; 483reflect8(96) -> 6; 484reflect8(97) -> 134; 485reflect8(98) -> 70; 486reflect8(99) -> 198; 487reflect8(100) -> 38; 488reflect8(101) -> 166; 489reflect8(102) -> 102; 490reflect8(103) -> 230; 491reflect8(104) -> 22; 492reflect8(105) -> 150; 493reflect8(106) -> 86; 494reflect8(107) -> 214; 495reflect8(108) -> 54; 496reflect8(109) -> 182; 497reflect8(110) -> 118; 498reflect8(111) -> 246; 499reflect8(112) -> 14; 500reflect8(113) -> 142; 501reflect8(114) -> 78; 502reflect8(115) -> 206; 503reflect8(116) -> 46; 504reflect8(117) -> 174; 505reflect8(118) -> 110; 506reflect8(119) -> 238; 507reflect8(120) -> 30; 508reflect8(121) -> 158; 509reflect8(122) -> 94; 510reflect8(123) -> 222; 511reflect8(124) -> 62; 512reflect8(125) -> 190; 513reflect8(126) -> 126; 514reflect8(127) -> 254; 515reflect8(128) -> 1; 516reflect8(129) -> 129; 517reflect8(130) -> 65; 518reflect8(131) -> 193; 519reflect8(132) -> 33; 520reflect8(133) -> 161; 521reflect8(134) -> 97; 522reflect8(135) -> 225; 523reflect8(136) -> 17; 524reflect8(137) -> 145; 525reflect8(138) -> 81; 526reflect8(139) -> 209; 527reflect8(140) -> 49; 528reflect8(141) -> 177; 529reflect8(142) -> 113; 530reflect8(143) -> 241; 531reflect8(144) -> 9; 532reflect8(145) -> 137; 533reflect8(146) -> 73; 534reflect8(147) -> 201; 535reflect8(148) -> 41; 536reflect8(149) -> 169; 537reflect8(150) -> 105; 538reflect8(151) -> 233; 539reflect8(152) -> 25; 540reflect8(153) -> 153; 541reflect8(154) -> 89; 542reflect8(155) -> 217; 543reflect8(156) -> 57; 544reflect8(157) -> 185; 545reflect8(158) -> 121; 546reflect8(159) -> 249; 547reflect8(160) -> 5; 548reflect8(161) -> 133; 549reflect8(162) -> 69; 550reflect8(163) -> 197; 551reflect8(164) -> 37; 552reflect8(165) -> 165; 553reflect8(166) -> 101; 554reflect8(167) -> 229; 555reflect8(168) -> 21; 556reflect8(169) -> 149; 557reflect8(170) -> 85; 558reflect8(171) -> 213; 559reflect8(172) -> 53; 560reflect8(173) -> 181; 561reflect8(174) -> 117; 562reflect8(175) -> 245; 563reflect8(176) -> 13; 564reflect8(177) -> 141; 565reflect8(178) -> 77; 566reflect8(179) -> 205; 567reflect8(180) -> 45; 568reflect8(181) -> 173; 569reflect8(182) -> 109; 570reflect8(183) -> 237; 571reflect8(184) -> 29; 572reflect8(185) -> 157; 573reflect8(186) -> 93; 574reflect8(187) -> 221; 575reflect8(188) -> 61; 576reflect8(189) -> 189; 577reflect8(190) -> 125; 578reflect8(191) -> 253; 579reflect8(192) -> 3; 580reflect8(193) -> 131; 581reflect8(194) -> 67; 582reflect8(195) -> 195; 583reflect8(196) -> 35; 584reflect8(197) -> 163; 585reflect8(198) -> 99; 586reflect8(199) -> 227; 587reflect8(200) -> 19; 588reflect8(201) -> 147; 589reflect8(202) -> 83; 590reflect8(203) -> 211; 591reflect8(204) -> 51; 592reflect8(205) -> 179; 593reflect8(206) -> 115; 594reflect8(207) -> 243; 595reflect8(208) -> 11; 596reflect8(209) -> 139; 597reflect8(210) -> 75; 598reflect8(211) -> 203; 599reflect8(212) -> 43; 600reflect8(213) -> 171; 601reflect8(214) -> 107; 602reflect8(215) -> 235; 603reflect8(216) -> 27; 604reflect8(217) -> 155; 605reflect8(218) -> 91; 606reflect8(219) -> 219; 607reflect8(220) -> 59; 608reflect8(221) -> 187; 609reflect8(222) -> 123; 610reflect8(223) -> 251; 611reflect8(224) -> 7; 612reflect8(225) -> 135; 613reflect8(226) -> 71; 614reflect8(227) -> 199; 615reflect8(228) -> 39; 616reflect8(229) -> 167; 617reflect8(230) -> 103; 618reflect8(231) -> 231; 619reflect8(232) -> 23; 620reflect8(233) -> 151; 621reflect8(234) -> 87; 622reflect8(235) -> 215; 623reflect8(236) -> 55; 624reflect8(237) -> 183; 625reflect8(238) -> 119; 626reflect8(239) -> 247; 627reflect8(240) -> 15; 628reflect8(241) -> 143; 629reflect8(242) -> 79; 630reflect8(243) -> 207; 631reflect8(244) -> 47; 632reflect8(245) -> 175; 633reflect8(246) -> 111; 634reflect8(247) -> 239; 635reflect8(248) -> 31; 636reflect8(249) -> 159; 637reflect8(250) -> 95; 638reflect8(251) -> 223; 639reflect8(252) -> 63; 640reflect8(253) -> 191; 641reflect8(254) -> 127; 642reflect8(255) -> 255; 643reflect8(_) -> exit(not_a_byte). 644 645%%% 646%%% Old MD5 implementation by Tony, modified to fit testing 647%%% 648 649-record(md5_ctx, 650 { 651 state = { 16#67452301, 16#efcdab89, 16#98badcfe, 16#10325476 }, 652 count = 0, %% number of bits (64 bit) 653 buffer = <<>> %% input buffer (16 bytes) 654 }). 655 656-define(S11, 7). 657-define(S12, 12). 658-define(S13, 17). 659-define(S14, 22). 660-define(S21, 5). 661-define(S22, 9). 662-define(S23, 14). 663-define(S24, 20). 664-define(S31, 4). 665-define(S32, 11). 666-define(S33, 16). 667-define(S34, 23). 668-define(S41, 6). 669-define(S42, 10). 670-define(S43, 15). 671-define(S44, 21). 672 673%% F, G, H and I are basic MD5 functions. 674 675-define(F(X, Y, Z), (((X) band (Y)) bor ((bnot (X)) band (Z)))). 676-define(G(X, Y, Z), (((X) band (Z)) bor ((Y) band (bnot (Z))))). 677-define(H(X, Y, Z), ((X) bxor (Y) bxor (Z))). 678-define(I(X, Y, Z), ((Y) bxor ((X) bor (bnot (Z))))). 679 680-define(U32(X), ((X) band 16#ffffffff)). 681 682-define(ROTATE_LEFT(X,N), rotate_left(X,N)). 683 684%% FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4. 685%% Rotation is separate from addition to prevent recomputation. 686%% 687-define(FF(A, B, C, D, X, S, AC), 688 ?ROTATE_LEFT(A + ?F((B), (C), (D)) + (X) + (AC),(S)) + (B)). 689 690-define(GG(A, B, C, D, X, S, AC), 691 ?ROTATE_LEFT(A + ?G((B), (C), (D)) + (X) + (AC),(S)) + (B)). 692 693-define( HH(A, B, C, D, X, S, AC), 694 ?ROTATE_LEFT(A + ?H((B), (C), (D)) + (X) + (AC),(S)) + (B)). 695 696-define(II(A, B, C, D, X, S, AC), 697 ?ROTATE_LEFT(A + ?I((B), (C), (D)) + (X) + (AC),(S)) + (B)). 698 699md5_init() -> 700 #md5_ctx {}. 701 702md5_update(CTX, Input) when is_list(Input) -> 703 md5_update(CTX,iolist_to_binary(Input)); 704md5_update(CTX, Input) when is_binary(Input) -> 705 Buffer = CTX#md5_ctx.buffer, 706 LenI = size(Input), 707 Len = LenI + size(Buffer), 708 md5_update(<<Buffer/binary,Input/binary>>, Len,CTX#md5_ctx.state, 709 CTX#md5_ctx.count+(LenI bsl 3)). 710 711%% 712%% update state, count reflects number of bytes 713%% including bytes in buffer 714%% 715md5_update(Buf0, Len0, State0, Count) when Len0 >= 64 -> 716 {Xs,Buf1} = decode(Buf0, 64), 717 State1 = transform(State0, Xs), 718 md5_update(Buf1, Len0 - 64, State1, Count); 719md5_update(Buf0, _Len0, State0, Count) -> 720 #md5_ctx { state = State0, count = Count, buffer = Buf0 }. 721 722%% produce a digest 723md5_final(CTX) -> 724 %% pad out to a length 56 (we later add a count that makes 64) 725 Count = CTX#md5_ctx.count, %% number of bits 726 Index = (Count bsr 3) rem 64, %% number of bytes 727 PadLen = if Index < 56 -> 728 56 - Index; 729 true -> 120 - Index 730 end, 731 CTX1 = md5_update(CTX, list_to_binary(padding(PadLen,[]))), 732 CTX2 = md5_update(CTX1, list_to_binary(encode([?U32(Count), ?U32(Count bsr 32)]))), 733 list_to_binary(encode(tuple_to_list(CTX2#md5_ctx.state))). 734 735%% generate padding info to final 736padding(0,Acc) -> Acc; 737padding(1,Acc) -> [16#80 | Acc]; 738padding(N,Acc) -> padding(N-1, [0 | Acc]). 739 740%% rotate X as 32-bit unsigned left N bits 741rotate_left(X, N) -> 742 ?U32(X bsl N) bor (?U32(X) bsr (32 - N)). 743 744%% 745%% decodes Len number of bytes into 32 bit integers 746%% returns {Xs, Tail} 747%% 748decode(Buf, Len) -> 749 decode(Buf, Len, []). 750 751decode(Buf, 0, Acc) -> 752 {lists:reverse(Acc), Buf}; 753decode(<<A:32/little,Buf/binary>>, N, Acc) -> 754 decode(Buf, N-4, [ A | Acc]). 755 756%% 757%% Encodes input 32-bit ints into byte buffer output. 758%% 759encode(Xs) -> encode(Xs, []). 760 761encode([X | Xs], Acc) -> 762 encode(Xs, [(X bsr 24) band 16#ff, 763 (X bsr 16) band 16#ff, 764 (X bsr 8) band 16#ff, 765 X band 16#ff | Acc]); 766encode([], Acc) -> lists:reverse(Acc). 767 768 769transform({A0,B0,C0,D0}, Xs) -> 770 [X0,X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,X11,X12,X13,X14,X15] = Xs, 771 772 %% Round 1 773 A1 = ?FF (A0, B0, C0, D0, X0, ?S11, 16#d76aa478), 774 D1 = ?FF (D0, A1, B0, C0, X1, ?S12, 16#e8c7b756), 775 C1 = ?FF (C0, D1, A1, B0, X2, ?S13, 16#242070db), 776 B1 = ?FF (B0, C1, D1, A1, X3, ?S14, 16#c1bdceee), 777 778 A2 = ?FF (A1, B1, C1, D1, X4, ?S11, 16#f57c0faf), 779 D2 = ?FF (D1, A2, B1, C1, X5, ?S12, 16#4787c62a), 780 C2 = ?FF (C1, D2, A2, B1, X6, ?S13, 16#a8304613), 781 B2 = ?FF (B1, C2, D2, A2, X7, ?S14, 16#fd469501), 782 783 A3 = ?FF (A2, B2, C2, D2, X8, ?S11, 16#698098d8), 784 D3 = ?FF (D2, A3, B2, C2, X9, ?S12, 16#8b44f7af), 785 C3 = ?FF (C2, D3, A3, B2, X10, ?S13, 16#ffff5bb1), 786 B3 = ?FF (B2, C3, D3, A3, X11, ?S14, 16#895cd7be), 787 788 A4 = ?FF (A3, B3, C3, D3, X12, ?S11, 16#6b901122), 789 D4 = ?FF (D3, A4, B3, C3, X13, ?S12, 16#fd987193), 790 C4 = ?FF (C3, D4, A4, B3, X14, ?S13, 16#a679438e), 791 B4 = ?FF (B3, C4, D4, A4, X15, ?S14, 16#49b40821), 792 793 %% Round 2 794 A5 = ?GG (A4, B4, C4, D4, X1, ?S21, 16#f61e2562), 795 D5 = ?GG (D4, A5, B4, C4, X6, ?S22, 16#c040b340), 796 C5 = ?GG (C4, D5, A5, B4, X11, ?S23, 16#265e5a51), 797 B5 = ?GG (B4, C5, D5, A5, X0, ?S24, 16#e9b6c7aa), 798 799 A6 = ?GG (A5, B5, C5, D5, X5, ?S21, 16#d62f105d), 800 D6 = ?GG (D5, A6, B5, C5, X10, ?S22, 16#2441453), 801 C6 = ?GG (C5, D6, A6, B5, X15, ?S23, 16#d8a1e681), 802 B6 = ?GG (B5, C6, D6, A6, X4, ?S24, 16#e7d3fbc8), 803 804 A7 = ?GG (A6, B6, C6, D6, X9, ?S21, 16#21e1cde6), 805 D7 = ?GG (D6, A7, B6, C6, X14, ?S22, 16#c33707d6), 806 C7 = ?GG (C6, D7, A7, B6, X3, ?S23, 16#f4d50d87), 807 B7 = ?GG (B6, C7, D7, A7, X8, ?S24, 16#455a14ed), 808 809 A8 = ?GG (A7, B7, C7, D7, X13, ?S21, 16#a9e3e905), 810 D8 = ?GG (D7, A8, B7, C7, X2, ?S22, 16#fcefa3f8), 811 C8 = ?GG (C7, D8, A8, B7, X7, ?S23, 16#676f02d9), 812 B8 = ?GG (B7, C8, D8, A8, X12, ?S24, 16#8d2a4c8a), 813 814 %% Round 3 815 A9 = ?HH (A8, B8, C8, D8, X5, ?S31, 16#fffa3942), 816 D9 = ?HH (D8, A9, B8, C8, X8, ?S32, 16#8771f681), 817 C9 = ?HH (C8, D9, A9, B8, X11, ?S33, 16#6d9d6122), 818 B9 = ?HH (B8, C9, D9, A9, X14, ?S34, 16#fde5380c), 819 820 A10 = ?HH (A9, B9, C9, D9, X1, ?S31, 16#a4beea44), 821 D10 = ?HH (D9, A10, B9, C9, X4, ?S32, 16#4bdecfa9), 822 C10 = ?HH (C9, D10, A10, B9, X7, ?S33, 16#f6bb4b60), 823 B10 = ?HH (B9, C10, D10, A10, X10, ?S34, 16#bebfbc70), 824 825 A11 = ?HH (A10, B10, C10, D10, X13, ?S31, 16#289b7ec6), 826 D11 = ?HH (D10, A11, B10, C10, X0, ?S32, 16#eaa127fa), 827 C11 = ?HH (C10, D11, A11, B10, X3, ?S33, 16#d4ef3085), 828 B11 = ?HH (B10, C11, D11, A11, X6, ?S34, 16#4881d05), 829 830 A12 = ?HH (A11, B11, C11, D11, X9, ?S31, 16#d9d4d039), 831 D12 = ?HH (D11, A12, B11, C11, X12, ?S32, 16#e6db99e5), 832 C12 = ?HH (C11, D12, A12, B11, X15, ?S33, 16#1fa27cf8), 833 B12 = ?HH (B11, C12, D12, A12, X2, ?S34, 16#c4ac5665), 834 835 %% Round 4 836 A13 = ?II (A12, B12, C12, D12, X0, ?S41, 16#f4292244), 837 D13 = ?II (D12, A13, B12, C12, X7, ?S42, 16#432aff97), 838 C13 = ?II (C12, D13, A13, B12, X14, ?S43, 16#ab9423a7), 839 B13 = ?II (B12, C13, D13, A13, X5, ?S44, 16#fc93a039), 840 841 A14 = ?II (A13, B13, C13, D13, X12, ?S41, 16#655b59c3), 842 D14 = ?II (D13, A14, B13, C13, X3, ?S42, 16#8f0ccc92), 843 C14 = ?II (C13, D14, A14, B13, X10, ?S43, 16#ffeff47d), 844 B14 = ?II (B13, C14, D14, A14, X1, ?S44, 16#85845dd1), 845 846 A15 = ?II (A14, B14, C14, D14, X8, ?S41, 16#6fa87e4f), 847 D15 = ?II (D14, A15, B14, C14, X15, ?S42, 16#fe2ce6e0), 848 C15 = ?II (C14, D15, A15, B14, X6, ?S43, 16#a3014314), 849 B15 = ?II (B14, C15, D15, A15, X13, ?S44, 16#4e0811a1), 850 851 A16 = ?II (A15, B15, C15, D15, X4, ?S41, 16#f7537e82), 852 D16 = ?II (D15, A16, B15, C15, X11, ?S42, 16#bd3af235), 853 C16 = ?II (C15, D16, A16, B15, X2, ?S43, 16#2ad7d2bb), 854 B16 = ?II (B15, C16, D16, A16, X9, ?S44, 16#eb86d391), 855 856 {?U32(A0+A16), ?U32(B0+B16), ?U32(C0+C16), ?U32(D0+D16)}. 857 858