synacode.pas 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 002.002.003 |
  3. |==============================================================================|
  4. | Content: Coding and decoding support |
  5. |==============================================================================|
  6. | Copyright (c)1999-2013, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2000-2013. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract(Various encoding and decoding support)}
  45. {$IFDEF FPC}
  46. {$MODE DELPHI}
  47. {$ENDIF}
  48. {$Q-}
  49. {$R-}
  50. {$H+}
  51. {$TYPEDADDRESS OFF}
  52. {$IFDEF CIL}
  53. {$DEFINE SYNACODE_NATIVE}
  54. {$ENDIF}
  55. {$IFDEF FPC_BIG_ENDIAN}
  56. {$DEFINE SYNACODE_NATIVE}
  57. {$ENDIF}
  58. {$IFDEF UNICODE}
  59. {$WARN IMPLICIT_STRING_CAST OFF}
  60. {$WARN IMPLICIT_STRING_CAST_LOSS OFF}
  61. {$WARN SUSPICIOUS_TYPECAST OFF}
  62. {$ENDIF}
  63. unit synacode;
  64. interface
  65. uses
  66. SysUtils;
  67. type
  68. TSpecials = TSysCharSet;
  69. const
  70. SPACE_CHAR: AnsiChar = #32;
  71. HMAC_MD5_IPAD_CHAR: AnsiChar = #$36;
  72. HMAC_MD5_OPAD_CHAR: AnsiChar = #$5C;
  73. HMAC_SHA1_IPAD_CHAR: AnsiChar = #$36;
  74. HMAC_SHA1_OPAD_CHAR: AnsiChar = #$5C;
  75. SpecialChar: TSpecials =
  76. ['=', '(', ')', '[', ']', '<', '>', ':', ';', ',', '@', '/', '?', '\',
  77. '"', '_'];
  78. NonAsciiChar: TSpecials =
  79. [#0..#31, #127..#255];
  80. URLFullSpecialChar: TSpecials =
  81. [';', '/', '?', ':', '@', '=', '&', '#', '+'];
  82. URLSpecialChar: TSpecials =
  83. [#$00..#$20, '<', '>', '"', '%', '{', '}', '|', '\', '^', '[', ']', '`', #$7F..#$FF];
  84. TableBase64 =
  85. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  86. TableBase64mod =
  87. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,=';
  88. TableUU =
  89. '`!"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_';
  90. TableXX =
  91. '+-0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
  92. ReTablebase64 =
  93. #$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$3E +#$40
  94. +#$40 +#$40 +#$3F +#$34 +#$35 +#$36 +#$37 +#$38 +#$39 +#$3A +#$3B +#$3C
  95. +#$3D +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$00 +#$01 +#$02 +#$03
  96. +#$04 +#$05 +#$06 +#$07 +#$08 +#$09 +#$0A +#$0B +#$0C +#$0D +#$0E +#$0F
  97. +#$10 +#$11 +#$12 +#$13 +#$14 +#$15 +#$16 +#$17 +#$18 +#$19 +#$40 +#$40
  98. +#$40 +#$40 +#$40 +#$40 +#$1A +#$1B +#$1C +#$1D +#$1E +#$1F +#$20 +#$21
  99. +#$22 +#$23 +#$24 +#$25 +#$26 +#$27 +#$28 +#$29 +#$2A +#$2B +#$2C +#$2D
  100. +#$2E +#$2F +#$30 +#$31 +#$32 +#$33 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40;
  101. ReTableUU =
  102. #$01 +#$02 +#$03 +#$04 +#$05 +#$06 +#$07 +#$08 +#$09 +#$0A +#$0B +#$0C
  103. +#$0D +#$0E +#$0F +#$10 +#$11 +#$12 +#$13 +#$14 +#$15 +#$16 +#$17 +#$18
  104. +#$19 +#$1A +#$1B +#$1C +#$1D +#$1E +#$1F +#$20 +#$21 +#$22 +#$23 +#$24
  105. +#$25 +#$26 +#$27 +#$28 +#$29 +#$2A +#$2B +#$2C +#$2D +#$2E +#$2F +#$30
  106. +#$31 +#$32 +#$33 +#$34 +#$35 +#$36 +#$37 +#$38 +#$39 +#$3A +#$3B +#$3C
  107. +#$3D +#$3E +#$3F +#$00 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40
  108. +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40
  109. +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40;
  110. ReTableXX =
  111. #$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$00 +#$40
  112. +#$01 +#$40 +#$40 +#$02 +#$03 +#$04 +#$05 +#$06 +#$07 +#$08 +#$09 +#$0A
  113. +#$0B +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$40 +#$0C +#$0D +#$0E +#$0F
  114. +#$10 +#$11 +#$12 +#$13 +#$14 +#$15 +#$16 +#$17 +#$18 +#$19 +#$1A +#$1B
  115. +#$1C +#$1D +#$1E +#$1F +#$20 +#$21 +#$22 +#$23 +#$24 +#$25 +#$40 +#$40
  116. +#$40 +#$40 +#$40 +#$40 +#$26 +#$27 +#$28 +#$29 +#$2A +#$2B +#$2C +#$2D
  117. +#$2E +#$2F +#$30 +#$31 +#$32 +#$33 +#$34 +#$35 +#$36 +#$37 +#$38 +#$39
  118. +#$3A +#$3B +#$3C +#$3D +#$3E +#$3F +#$40 +#$40 +#$40 +#$40 +#$40 +#$40;
  119. {:Decodes triplet encoding with a given character delimiter. It is used for
  120. decoding quoted-printable or URL encoding.}
  121. function DecodeTriplet(const Value: AnsiString; Delimiter: AnsiChar): AnsiString;
  122. {:Decodes a string from quoted printable form. (also decodes triplet sequences
  123. like '=7F')}
  124. function DecodeQuotedPrintable(const Value: AnsiString): AnsiString;
  125. {:Decodes a string of URL encoding. (also decodes triplet sequences like '%7F')}
  126. function DecodeURL(const Value: AnsiString): AnsiString;
  127. {:Performs triplet encoding with a given character delimiter. Used for encoding
  128. quoted-printable or URL encoding.}
  129. function EncodeTriplet(const Value: AnsiString; Delimiter: AnsiChar;
  130. Specials: TSpecials): AnsiString;
  131. {:Encodes a string to triplet quoted printable form. All @link(NonAsciiChar)
  132. are encoded.}
  133. function EncodeQuotedPrintable(const Value: AnsiString): AnsiString;
  134. {:Encodes a string to triplet quoted printable form. All @link(NonAsciiChar) and
  135. @link(SpecialChar) are encoded.}
  136. function EncodeSafeQuotedPrintable(const Value: AnsiString): AnsiString;
  137. {:Encodes a string to URL format. Used for encoding data from a form field in
  138. HTTP, etc. (Encodes all critical characters including characters used as URL
  139. delimiters ('/',':', etc.)}
  140. function EncodeURLElement(const Value: AnsiString): AnsiString;
  141. {:Encodes a string to URL format. Used to encode critical characters in all
  142. URLs.}
  143. function EncodeURL(const Value: AnsiString): AnsiString;
  144. {:Decode 4to3 encoding with given table. If some element is not found in table,
  145. first item from table is used. This is good for buggy coded items by Microsoft
  146. Outlook. This software sometimes using wrong table for UUcode, where is used
  147. ' ' instead '`'.}
  148. function Decode4to3(const Value, Table: AnsiString): AnsiString;
  149. {:Decode 4to3 encoding with given REVERSE table. Using this function with
  150. reverse table is much faster then @link(Decode4to3). This function is used
  151. internally for Base64, UU or XX decoding.}
  152. function Decode4to3Ex(const Value, Table: AnsiString): AnsiString;
  153. {:Encode by system 3to4 (used by Base64, UU coding, etc) by given table.}
  154. function Encode3to4(const Value, Table: AnsiString): AnsiString;
  155. {:Decode string from base64 format.}
  156. function DecodeBase64(const Value: AnsiString): AnsiString;
  157. {:Encodes a string to base64 format.}
  158. function EncodeBase64(const Value: AnsiString): AnsiString;
  159. {:Decode string from modified base64 format. (used in IMAP, for example.)}
  160. function DecodeBase64mod(const Value: AnsiString): AnsiString;
  161. {:Encodes a string to modified base64 format. (used in IMAP, for example.)}
  162. function EncodeBase64mod(const Value: AnsiString): AnsiString;
  163. {:Decodes a string from UUcode format.}
  164. function DecodeUU(const Value: AnsiString): AnsiString;
  165. {:encode UUcode. it encode only datas, you must also add header and footer for
  166. proper encode.}
  167. function EncodeUU(const Value: AnsiString): AnsiString;
  168. {:Decodes a string from XXcode format.}
  169. function DecodeXX(const Value: AnsiString): AnsiString;
  170. {:decode line with Yenc code. This code is sometimes used in newsgroups.}
  171. function DecodeYEnc(const Value: AnsiString): AnsiString;
  172. {:Returns a new CRC32 value after adding a new byte of data.}
  173. function UpdateCrc32(Value: Byte; Crc32: Cardinal): Cardinal;
  174. {:return CRC32 from a value string.}
  175. function Crc32(const Value: AnsiString): Cardinal;
  176. {:Returns a new CRC16 value after adding a new byte of data.}
  177. function UpdateCrc16(Value: Byte; Crc16: Word): Word;
  178. {:return CRC16 from a value string.}
  179. function Crc16(const Value: AnsiString): Word;
  180. {:Returns a binary string with a RSA-MD5 hashing of "Value" string.}
  181. function MD5(const Value: AnsiString): AnsiString;
  182. {:Returns a binary string with HMAC-MD5 hash.}
  183. function HMAC_MD5(Text, Key: AnsiString): AnsiString;
  184. {:Returns a binary string with a RSA-MD5 hashing of string what is constructed
  185. by repeating "value" until length is "Len".}
  186. function MD5LongHash(const Value: AnsiString; Len: integer): AnsiString;
  187. {:Returns a binary string with a SHA-1 hashing of "Value" string.}
  188. function SHA1(const Value: AnsiString): AnsiString;
  189. {:Returns a binary string with HMAC-SHA1 hash.}
  190. function HMAC_SHA1(Text, Key: AnsiString): AnsiString;
  191. {:Returns a binary string with a SHA-1 hashing of string what is constructed
  192. by repeating "value" until length is "Len".}
  193. function SHA1LongHash(const Value: AnsiString; Len: integer): AnsiString;
  194. {:Returns a binary string with a RSA-MD4 hashing of "Value" string.}
  195. function MD4(const Value: AnsiString): AnsiString;
  196. implementation
  197. const
  198. Crc32Tab: array[0..255] of Integer = (
  199. Integer($00000000), Integer($77073096), Integer($EE0E612C), Integer($990951BA),
  200. Integer($076DC419), Integer($706AF48F), Integer($E963A535), Integer($9E6495A3),
  201. Integer($0EDB8832), Integer($79DCB8A4), Integer($E0D5E91E), Integer($97D2D988),
  202. Integer($09B64C2B), Integer($7EB17CBD), Integer($E7B82D07), Integer($90BF1D91),
  203. Integer($1DB71064), Integer($6AB020F2), Integer($F3B97148), Integer($84BE41DE),
  204. Integer($1ADAD47D), Integer($6DDDE4EB), Integer($F4D4B551), Integer($83D385C7),
  205. Integer($136C9856), Integer($646BA8C0), Integer($FD62F97A), Integer($8A65C9EC),
  206. Integer($14015C4F), Integer($63066CD9), Integer($FA0F3D63), Integer($8D080DF5),
  207. Integer($3B6E20C8), Integer($4C69105E), Integer($D56041E4), Integer($A2677172),
  208. Integer($3C03E4D1), Integer($4B04D447), Integer($D20D85FD), Integer($A50AB56B),
  209. Integer($35B5A8FA), Integer($42B2986C), Integer($DBBBC9D6), Integer($ACBCF940),
  210. Integer($32D86CE3), Integer($45DF5C75), Integer($DCD60DCF), Integer($ABD13D59),
  211. Integer($26D930AC), Integer($51DE003A), Integer($C8D75180), Integer($BFD06116),
  212. Integer($21B4F4B5), Integer($56B3C423), Integer($CFBA9599), Integer($B8BDA50F),
  213. Integer($2802B89E), Integer($5F058808), Integer($C60CD9B2), Integer($B10BE924),
  214. Integer($2F6F7C87), Integer($58684C11), Integer($C1611DAB), Integer($B6662D3D),
  215. Integer($76DC4190), Integer($01DB7106), Integer($98D220BC), Integer($EFD5102A),
  216. Integer($71B18589), Integer($06B6B51F), Integer($9FBFE4A5), Integer($E8B8D433),
  217. Integer($7807C9A2), Integer($0F00F934), Integer($9609A88E), Integer($E10E9818),
  218. Integer($7F6A0DBB), Integer($086D3D2D), Integer($91646C97), Integer($E6635C01),
  219. Integer($6B6B51F4), Integer($1C6C6162), Integer($856530D8), Integer($F262004E),
  220. Integer($6C0695ED), Integer($1B01A57B), Integer($8208F4C1), Integer($F50FC457),
  221. Integer($65B0D9C6), Integer($12B7E950), Integer($8BBEB8EA), Integer($FCB9887C),
  222. Integer($62DD1DDF), Integer($15DA2D49), Integer($8CD37CF3), Integer($FBD44C65),
  223. Integer($4DB26158), Integer($3AB551CE), Integer($A3BC0074), Integer($D4BB30E2),
  224. Integer($4ADFA541), Integer($3DD895D7), Integer($A4D1C46D), Integer($D3D6F4FB),
  225. Integer($4369E96A), Integer($346ED9FC), Integer($AD678846), Integer($DA60B8D0),
  226. Integer($44042D73), Integer($33031DE5), Integer($AA0A4C5F), Integer($DD0D7CC9),
  227. Integer($5005713C), Integer($270241AA), Integer($BE0B1010), Integer($C90C2086),
  228. Integer($5768B525), Integer($206F85B3), Integer($B966D409), Integer($CE61E49F),
  229. Integer($5EDEF90E), Integer($29D9C998), Integer($B0D09822), Integer($C7D7A8B4),
  230. Integer($59B33D17), Integer($2EB40D81), Integer($B7BD5C3B), Integer($C0BA6CAD),
  231. Integer($EDB88320), Integer($9ABFB3B6), Integer($03B6E20C), Integer($74B1D29A),
  232. Integer($EAD54739), Integer($9DD277AF), Integer($04DB2615), Integer($73DC1683),
  233. Integer($E3630B12), Integer($94643B84), Integer($0D6D6A3E), Integer($7A6A5AA8),
  234. Integer($E40ECF0B), Integer($9309FF9D), Integer($0A00AE27), Integer($7D079EB1),
  235. Integer($F00F9344), Integer($8708A3D2), Integer($1E01F268), Integer($6906C2FE),
  236. Integer($F762575D), Integer($806567CB), Integer($196C3671), Integer($6E6B06E7),
  237. Integer($FED41B76), Integer($89D32BE0), Integer($10DA7A5A), Integer($67DD4ACC),
  238. Integer($F9B9DF6F), Integer($8EBEEFF9), Integer($17B7BE43), Integer($60B08ED5),
  239. Integer($D6D6A3E8), Integer($A1D1937E), Integer($38D8C2C4), Integer($4FDFF252),
  240. Integer($D1BB67F1), Integer($A6BC5767), Integer($3FB506DD), Integer($48B2364B),
  241. Integer($D80D2BDA), Integer($AF0A1B4C), Integer($36034AF6), Integer($41047A60),
  242. Integer($DF60EFC3), Integer($A867DF55), Integer($316E8EEF), Integer($4669BE79),
  243. Integer($CB61B38C), Integer($BC66831A), Integer($256FD2A0), Integer($5268E236),
  244. Integer($CC0C7795), Integer($BB0B4703), Integer($220216B9), Integer($5505262F),
  245. Integer($C5BA3BBE), Integer($B2BD0B28), Integer($2BB45A92), Integer($5CB36A04),
  246. Integer($C2D7FFA7), Integer($B5D0CF31), Integer($2CD99E8B), Integer($5BDEAE1D),
  247. Integer($9B64C2B0), Integer($EC63F226), Integer($756AA39C), Integer($026D930A),
  248. Integer($9C0906A9), Integer($EB0E363F), Integer($72076785), Integer($05005713),
  249. Integer($95BF4A82), Integer($E2B87A14), Integer($7BB12BAE), Integer($0CB61B38),
  250. Integer($92D28E9B), Integer($E5D5BE0D), Integer($7CDCEFB7), Integer($0BDBDF21),
  251. Integer($86D3D2D4), Integer($F1D4E242), Integer($68DDB3F8), Integer($1FDA836E),
  252. Integer($81BE16CD), Integer($F6B9265B), Integer($6FB077E1), Integer($18B74777),
  253. Integer($88085AE6), Integer($FF0F6A70), Integer($66063BCA), Integer($11010B5C),
  254. Integer($8F659EFF), Integer($F862AE69), Integer($616BFFD3), Integer($166CCF45),
  255. Integer($A00AE278), Integer($D70DD2EE), Integer($4E048354), Integer($3903B3C2),
  256. Integer($A7672661), Integer($D06016F7), Integer($4969474D), Integer($3E6E77DB),
  257. Integer($AED16A4A), Integer($D9D65ADC), Integer($40DF0B66), Integer($37D83BF0),
  258. Integer($A9BCAE53), Integer($DEBB9EC5), Integer($47B2CF7F), Integer($30B5FFE9),
  259. Integer($BDBDF21C), Integer($CABAC28A), Integer($53B39330), Integer($24B4A3A6),
  260. Integer($BAD03605), Integer($CDD70693), Integer($54DE5729), Integer($23D967BF),
  261. Integer($B3667A2E), Integer($C4614AB8), Integer($5D681B02), Integer($2A6F2B94),
  262. Integer($B40BBE37), Integer($C30C8EA1), Integer($5A05DF1B), Integer($2D02EF8D)
  263. );
  264. Crc16Tab: array[0..255] of Word = (
  265. $0000, $1189, $2312, $329B, $4624, $57AD, $6536, $74BF,
  266. $8C48, $9DC1, $AF5A, $BED3, $CA6C, $DBE5, $E97E, $F8F7,
  267. $1081, $0108, $3393, $221A, $56A5, $472C, $75B7, $643E,
  268. $9CC9, $8D40, $BFDB, $AE52, $DAED, $CB64, $F9FF, $E876,
  269. $2102, $308B, $0210, $1399, $6726, $76AF, $4434, $55BD,
  270. $AD4A, $BCC3, $8E58, $9FD1, $EB6E, $FAE7, $C87C, $D9F5,
  271. $3183, $200A, $1291, $0318, $77A7, $662E, $54B5, $453C,
  272. $BDCB, $AC42, $9ED9, $8F50, $FBEF, $EA66, $D8FD, $C974,
  273. $4204, $538D, $6116, $709F, $0420, $15A9, $2732, $36BB,
  274. $CE4C, $DFC5, $ED5E, $FCD7, $8868, $99E1, $AB7A, $BAF3,
  275. $5285, $430C, $7197, $601E, $14A1, $0528, $37B3, $263A,
  276. $DECD, $CF44, $FDDF, $EC56, $98E9, $8960, $BBFB, $AA72,
  277. $6306, $728F, $4014, $519D, $2522, $34AB, $0630, $17B9,
  278. $EF4E, $FEC7, $CC5C, $DDD5, $A96A, $B8E3, $8A78, $9BF1,
  279. $7387, $620E, $5095, $411C, $35A3, $242A, $16B1, $0738,
  280. $FFCF, $EE46, $DCDD, $CD54, $B9EB, $A862, $9AF9, $8B70,
  281. $8408, $9581, $A71A, $B693, $C22C, $D3A5, $E13E, $F0B7,
  282. $0840, $19C9, $2B52, $3ADB, $4E64, $5FED, $6D76, $7CFF,
  283. $9489, $8500, $B79B, $A612, $D2AD, $C324, $F1BF, $E036,
  284. $18C1, $0948, $3BD3, $2A5A, $5EE5, $4F6C, $7DF7, $6C7E,
  285. $A50A, $B483, $8618, $9791, $E32E, $F2A7, $C03C, $D1B5,
  286. $2942, $38CB, $0A50, $1BD9, $6F66, $7EEF, $4C74, $5DFD,
  287. $B58B, $A402, $9699, $8710, $F3AF, $E226, $D0BD, $C134,
  288. $39C3, $284A, $1AD1, $0B58, $7FE7, $6E6E, $5CF5, $4D7C,
  289. $C60C, $D785, $E51E, $F497, $8028, $91A1, $A33A, $B2B3,
  290. $4A44, $5BCD, $6956, $78DF, $0C60, $1DE9, $2F72, $3EFB,
  291. $D68D, $C704, $F59F, $E416, $90A9, $8120, $B3BB, $A232,
  292. $5AC5, $4B4C, $79D7, $685E, $1CE1, $0D68, $3FF3, $2E7A,
  293. $E70E, $F687, $C41C, $D595, $A12A, $B0A3, $8238, $93B1,
  294. $6B46, $7ACF, $4854, $59DD, $2D62, $3CEB, $0E70, $1FF9,
  295. $F78F, $E606, $D49D, $C514, $B1AB, $A022, $92B9, $8330,
  296. $7BC7, $6A4E, $58D5, $495C, $3DE3, $2C6A, $1EF1, $0F78
  297. );
  298. procedure ArrByteToLong(var ArByte: Array of byte; var ArLong: Array of Integer);
  299. {$IFDEF SYNACODE_NATIVE}
  300. var
  301. n: integer;
  302. {$ENDIF}
  303. begin
  304. if (High(ArByte) + 1) > ((High(ArLong) + 1) * 4) then
  305. Exit;
  306. {$IFDEF SYNACODE_NATIVE}
  307. for n := 0 to ((high(ArByte) + 1) div 4) - 1 do
  308. ArLong[n] := ArByte[n * 4 + 0]
  309. + (ArByte[n * 4 + 1] shl 8)
  310. + (ArByte[n * 4 + 2] shl 16)
  311. + (ArByte[n * 4 + 3] shl 24);
  312. {$ELSE}
  313. Move(ArByte[0], ArLong[0], High(ArByte) + 1);
  314. {$ENDIF}
  315. end;
  316. procedure ArrLongToByte(var ArLong: Array of Integer; var ArByte: Array of byte);
  317. {$IFDEF SYNACODE_NATIVE}
  318. var
  319. n: integer;
  320. {$ENDIF}
  321. begin
  322. if (High(ArByte) + 1) < ((High(ArLong) + 1) * 4) then
  323. Exit;
  324. {$IFDEF SYNACODE_NATIVE}
  325. for n := 0 to high(ArLong) do
  326. begin
  327. ArByte[n * 4 + 0] := ArLong[n] and $000000FF;
  328. ArByte[n * 4 + 1] := (ArLong[n] shr 8) and $000000FF;
  329. ArByte[n * 4 + 2] := (ArLong[n] shr 16) and $000000FF;
  330. ArByte[n * 4 + 3] := (ArLong[n] shr 24) and $000000FF;
  331. end;
  332. {$ELSE}
  333. Move(ArLong[0], ArByte[0], High(ArByte) + 1);
  334. {$ENDIF}
  335. end;
  336. type
  337. TMDCtx = record
  338. State: array[0..3] of Integer;
  339. Count: array[0..1] of Integer;
  340. BufAnsiChar: array[0..63] of Byte;
  341. BufLong: array[0..15] of Integer;
  342. end;
  343. TSHA1Ctx= record
  344. Hi, Lo: integer;
  345. Buffer: array[0..63] of byte;
  346. Index: integer;
  347. Hash: array[0..4] of Integer;
  348. HashByte: array[0..19] of byte;
  349. end;
  350. TMDTransform = procedure(var Buf: array of Integer; const Data: array of Integer);
  351. {==============================================================================}
  352. function DecodeTriplet(const Value: AnsiString; Delimiter: AnsiChar): AnsiString;
  353. var
  354. x, l, lv: Integer;
  355. c: AnsiChar;
  356. b: Byte;
  357. bad: Boolean;
  358. begin
  359. lv := Length(Value);
  360. SetLength(Result, lv);
  361. x := 1;
  362. l := 1;
  363. while x <= lv do
  364. begin
  365. c := Value[x];
  366. Inc(x);
  367. if c <> Delimiter then
  368. begin
  369. Result[l] := c;
  370. Inc(l);
  371. end
  372. else
  373. if x < lv then
  374. begin
  375. Case Value[x] Of
  376. #13:
  377. if (Value[x + 1] = #10) then
  378. Inc(x, 2)
  379. else
  380. Inc(x);
  381. #10:
  382. if (Value[x + 1] = #13) then
  383. Inc(x, 2)
  384. else
  385. Inc(x);
  386. else
  387. begin
  388. bad := False;
  389. Case Value[x] Of
  390. '0'..'9': b := (Byte(Value[x]) - 48) Shl 4;
  391. 'a'..'f', 'A'..'F': b := ((Byte(Value[x]) And 7) + 9) shl 4;
  392. else
  393. begin
  394. b := 0;
  395. bad := True;
  396. end;
  397. end;
  398. Case Value[x + 1] Of
  399. '0'..'9': b := b Or (Byte(Value[x + 1]) - 48);
  400. 'a'..'f', 'A'..'F': b := b Or ((Byte(Value[x + 1]) And 7) + 9);
  401. else
  402. bad := True;
  403. end;
  404. if bad then
  405. begin
  406. Result[l] := c;
  407. Inc(l);
  408. end
  409. else
  410. begin
  411. Inc(x, 2);
  412. Result[l] := AnsiChar(b);
  413. Inc(l);
  414. end;
  415. end;
  416. end;
  417. end
  418. else
  419. break;
  420. end;
  421. Dec(l);
  422. SetLength(Result, l);
  423. end;
  424. {==============================================================================}
  425. function DecodeQuotedPrintable(const Value: AnsiString): AnsiString;
  426. begin
  427. Result := DecodeTriplet(Value, '=');
  428. end;
  429. {==============================================================================}
  430. function DecodeURL(const Value: AnsiString): AnsiString;
  431. begin
  432. Result := DecodeTriplet(Value, '%');
  433. end;
  434. {==============================================================================}
  435. function EncodeTriplet(const Value: AnsiString; Delimiter: AnsiChar;
  436. Specials: TSpecials): AnsiString;
  437. var
  438. n, l: Integer;
  439. s: AnsiString;
  440. c: AnsiChar;
  441. begin
  442. SetLength(Result, Length(Value) * 3);
  443. l := 1;
  444. for n := 1 to Length(Value) do
  445. begin
  446. c := Value[n];
  447. if CharInSet(c, Specials) then
  448. begin
  449. Result[l] := Delimiter;
  450. Inc(l);
  451. s := IntToHex(Ord(c), 2);
  452. Result[l] := s[1];
  453. Inc(l);
  454. Result[l] := s[2];
  455. Inc(l);
  456. end
  457. else
  458. begin
  459. Result[l] := c;
  460. Inc(l);
  461. end;
  462. end;
  463. Dec(l);
  464. SetLength(Result, l);
  465. end;
  466. {==============================================================================}
  467. function EncodeQuotedPrintable(const Value: AnsiString): AnsiString;
  468. begin
  469. Result := EncodeTriplet(Value, '=', ['='] + NonAsciiChar);
  470. end;
  471. {==============================================================================}
  472. function EncodeSafeQuotedPrintable(const Value: AnsiString): AnsiString;
  473. begin
  474. Result := EncodeTriplet(Value, '=', SpecialChar + NonAsciiChar);
  475. end;
  476. {==============================================================================}
  477. function EncodeURLElement(const Value: AnsiString): AnsiString;
  478. begin
  479. Result := EncodeTriplet(Value, '%', URLSpecialChar + URLFullSpecialChar);
  480. end;
  481. {==============================================================================}
  482. function EncodeURL(const Value: AnsiString): AnsiString;
  483. begin
  484. Result := EncodeTriplet(Value, '%', URLSpecialChar);
  485. end;
  486. {==============================================================================}
  487. function Decode4to3(const Value, Table: AnsiString): AnsiString;
  488. var
  489. x, y, n, l: Integer;
  490. d: array[0..3] of Byte;
  491. begin
  492. SetLength(Result, Length(Value));
  493. x := 1;
  494. l := 1;
  495. while x <= Length(Value) do
  496. begin
  497. for n := 0 to 3 do
  498. begin
  499. if x > Length(Value) then
  500. d[n] := 64
  501. else
  502. begin
  503. y := Pos(Value[x], Table);
  504. if y < 1 then
  505. y := 1;
  506. d[n] := y - 1;
  507. end;
  508. Inc(x);
  509. end;
  510. Result[l] := AnsiChar((D[0] and $3F) shl 2 + (D[1] and $30) shr 4);
  511. Inc(l);
  512. if d[2] <> 64 then
  513. begin
  514. Result[l] := AnsiChar((D[1] and $0F) shl 4 + (D[2] and $3C) shr 2);
  515. Inc(l);
  516. if d[3] <> 64 then
  517. begin
  518. Result[l] := AnsiChar((D[2] and $03) shl 6 + (D[3] and $3F));
  519. Inc(l);
  520. end;
  521. end;
  522. end;
  523. Dec(l);
  524. SetLength(Result, l);
  525. end;
  526. {==============================================================================}
  527. function Decode4to3Ex(const Value, Table: AnsiString): AnsiString;
  528. var
  529. x, y, lv: Integer;
  530. d: integer;
  531. dl: integer;
  532. c: byte;
  533. p: integer;
  534. begin
  535. lv := Length(Value);
  536. SetLength(Result, lv);
  537. x := 1;
  538. dl := 4;
  539. d := 0;
  540. p := 1;
  541. while x <= lv do
  542. begin
  543. y := Ord(Value[x]);
  544. if y in [33..127] then
  545. c := Ord(Table[y - 32])
  546. else
  547. c := 64;
  548. Inc(x);
  549. if c > 63 then
  550. continue;
  551. d := (d shl 6) or c;
  552. dec(dl);
  553. if dl <> 0 then
  554. continue;
  555. Result[p] := AnsiChar((d shr 16) and $ff);
  556. inc(p);
  557. Result[p] := AnsiChar((d shr 8) and $ff);
  558. inc(p);
  559. Result[p] := AnsiChar(d and $ff);
  560. inc(p);
  561. d := 0;
  562. dl := 4;
  563. end;
  564. case dl of
  565. 1:
  566. begin
  567. d := d shr 2;
  568. Result[p] := AnsiChar((d shr 8) and $ff);
  569. inc(p);
  570. Result[p] := AnsiChar(d and $ff);
  571. inc(p);
  572. end;
  573. 2:
  574. begin
  575. d := d shr 4;
  576. Result[p] := AnsiChar(d and $ff);
  577. inc(p);
  578. end;
  579. end;
  580. SetLength(Result, p - 1);
  581. end;
  582. {==============================================================================}
  583. function Encode3to4(const Value, Table: AnsiString): AnsiString;
  584. var
  585. c: Byte;
  586. n, l: Integer;
  587. Count: Integer;
  588. DOut: array[0..3] of Byte;
  589. begin
  590. setlength(Result, ((Length(Value) + 2) div 3) * 4);
  591. l := 1;
  592. Count := 1;
  593. while Count <= Length(Value) do
  594. begin
  595. c := Ord(Value[Count]);
  596. Inc(Count);
  597. DOut[0] := (c and $FC) shr 2;
  598. DOut[1] := (c and $03) shl 4;
  599. if Count <= Length(Value) then
  600. begin
  601. c := Ord(Value[Count]);
  602. Inc(Count);
  603. DOut[1] := DOut[1] + (c and $F0) shr 4;
  604. DOut[2] := (c and $0F) shl 2;
  605. if Count <= Length(Value) then
  606. begin
  607. c := Ord(Value[Count]);
  608. Inc(Count);
  609. DOut[2] := DOut[2] + (c and $C0) shr 6;
  610. DOut[3] := (c and $3F);
  611. end
  612. else
  613. begin
  614. DOut[3] := $40;
  615. end;
  616. end
  617. else
  618. begin
  619. DOut[2] := $40;
  620. DOut[3] := $40;
  621. end;
  622. for n := 0 to 3 do
  623. begin
  624. if (DOut[n] + 1) <= Length(Table) then
  625. begin
  626. Result[l] := Table[DOut[n] + 1];
  627. Inc(l);
  628. end;
  629. end;
  630. end;
  631. SetLength(Result, l - 1);
  632. end;
  633. {==============================================================================}
  634. function DecodeBase64(const Value: AnsiString): AnsiString;
  635. begin
  636. Result := Decode4to3Ex(Value, ReTableBase64);
  637. end;
  638. {==============================================================================}
  639. function EncodeBase64(const Value: AnsiString): AnsiString;
  640. begin
  641. Result := Encode3to4(Value, TableBase64);
  642. end;
  643. {==============================================================================}
  644. function DecodeBase64mod(const Value: AnsiString): AnsiString;
  645. begin
  646. Result := Decode4to3(Value, TableBase64mod);
  647. end;
  648. {==============================================================================}
  649. function EncodeBase64mod(const Value: AnsiString): AnsiString;
  650. begin
  651. Result := Encode3to4(Value, TableBase64mod);
  652. end;
  653. {==============================================================================}
  654. function DecodeUU(const Value: AnsiString): AnsiString;
  655. var
  656. s: AnsiString;
  657. uut: AnsiString;
  658. x: Integer;
  659. begin
  660. Result := '';
  661. uut := TableUU;
  662. s := trim(UpperCase(Value));
  663. if s = '' then Exit;
  664. if Pos('BEGIN', s) = 1 then
  665. Exit;
  666. if Pos('END', s) = 1 then
  667. Exit;
  668. if Pos('TABLE', s) = 1 then
  669. Exit; //ignore Table yet (set custom UUT)
  670. //begin decoding
  671. x := Pos(Value[1], uut) - 1;
  672. case (x mod 3) of
  673. 0: x :=(x div 3)* 4;
  674. 1: x :=((x div 3) * 4) + 2;
  675. 2: x :=((x div 3) * 4) + 3;
  676. end;
  677. //x - lenght UU line
  678. s := Copy(Value, 2, x);
  679. if s = '' then
  680. Exit;
  681. s := s + StringOfChar(SPACE_CHAR, x - length(s));
  682. Result := Decode4to3(s, uut);
  683. end;
  684. {==============================================================================}
  685. function EncodeUU(const Value: AnsiString): AnsiString;
  686. begin
  687. Result := '';
  688. if Length(Value) < Length(TableUU) then
  689. Result := TableUU[Length(Value) + 1] + Encode3to4(Value, TableUU);
  690. end;
  691. {==============================================================================}
  692. function DecodeXX(const Value: AnsiString): AnsiString;
  693. var
  694. s: AnsiString;
  695. x: Integer;
  696. begin
  697. Result := '';
  698. s := trim(UpperCase(Value));
  699. if s = '' then
  700. Exit;
  701. if Pos('BEGIN', s) = 1 then
  702. Exit;
  703. if Pos('END', s) = 1 then
  704. Exit;
  705. //begin decoding
  706. x := Pos(Value[1], TableXX) - 1;
  707. case (x mod 3) of
  708. 0: x :=(x div 3)* 4;
  709. 1: x :=((x div 3) * 4) + 2;
  710. 2: x :=((x div 3) * 4) + 3;
  711. end;
  712. //x - lenght XX line
  713. s := Copy(Value, 2, x);
  714. if s = '' then
  715. Exit;
  716. s := s + StringOfChar(SPACE_CHAR, x - length(s));
  717. Result := Decode4to3(s, TableXX);
  718. end;
  719. {==============================================================================}
  720. function DecodeYEnc(const Value: AnsiString): AnsiString;
  721. var
  722. C : Byte;
  723. i: integer;
  724. begin
  725. Result := '';
  726. i := 1;
  727. while i <= Length(Value) do
  728. begin
  729. c := Ord(Value[i]);
  730. Inc(i);
  731. if c = Ord('=') then
  732. begin
  733. c := Ord(Value[i]);
  734. Inc(i);
  735. Dec(c, 64);
  736. end;
  737. Dec(C, 42);
  738. Result := Result + AnsiChar(C);
  739. end;
  740. end;
  741. {==============================================================================}
  742. function UpdateCrc32(Value: Byte; Crc32: Cardinal): Cardinal;
  743. begin
  744. Result := (Crc32 shr 8)
  745. xor Cardinal(crc32tab[Byte(Value xor (Crc32 and Cardinal($000000FF)))]);
  746. end;
  747. {==============================================================================}
  748. function Crc32(const Value: AnsiString): Cardinal;
  749. var
  750. n: Integer;
  751. begin
  752. Result := $FFFFFFFF;
  753. for n := 1 to Length(Value) do
  754. Result := UpdateCrc32(Ord(Value[n]), Result);
  755. Result := not Result;
  756. end;
  757. {==============================================================================}
  758. function UpdateCrc16(Value: Byte; Crc16: Word): Word;
  759. begin
  760. Result := ((Crc16 shr 8) and $00FF) xor
  761. crc16tab[Byte(Crc16 xor (Word(Value)) and $00FF)];
  762. end;
  763. {==============================================================================}
  764. function Crc16(const Value: AnsiString): Word;
  765. var
  766. n: Integer;
  767. begin
  768. Result := $FFFF;
  769. for n := 1 to Length(Value) do
  770. Result := UpdateCrc16(Ord(Value[n]), Result);
  771. end;
  772. {==============================================================================}
  773. procedure MDInit(var MDContext: TMDCtx);
  774. var
  775. n: integer;
  776. begin
  777. MDContext.Count[0] := 0;
  778. MDContext.Count[1] := 0;
  779. for n := 0 to high(MDContext.BufAnsiChar) do
  780. MDContext.BufAnsiChar[n] := 0;
  781. for n := 0 to high(MDContext.BufLong) do
  782. MDContext.BufLong[n] := 0;
  783. MDContext.State[0] := Integer($67452301);
  784. MDContext.State[1] := Integer($EFCDAB89);
  785. MDContext.State[2] := Integer($98BADCFE);
  786. MDContext.State[3] := Integer($10325476);
  787. end;
  788. procedure MD5Transform(var Buf: array of Integer; const Data: array of Integer);
  789. var
  790. A, B, C, D: LongInt;
  791. procedure Round1(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
  792. begin
  793. Inc(W, (Z xor (X and (Y xor Z))) + Data);
  794. W := (W shl S) or (W shr (32 - S));
  795. Inc(W, X);
  796. end;
  797. procedure Round2(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
  798. begin
  799. Inc(W, (Y xor (Z and (X xor Y))) + Data);
  800. W := (W shl S) or (W shr (32 - S));
  801. Inc(W, X);
  802. end;
  803. procedure Round3(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
  804. begin
  805. Inc(W, (X xor Y xor Z) + Data);
  806. W := (W shl S) or (W shr (32 - S));
  807. Inc(W, X);
  808. end;
  809. procedure Round4(var W: LongInt; X, Y, Z, Data: LongInt; S: Byte);
  810. begin
  811. Inc(W, (Y xor (X or not Z)) + Data);
  812. W := (W shl S) or (W shr (32 - S));
  813. Inc(W, X);
  814. end;
  815. begin
  816. A := Buf[0];
  817. B := Buf[1];
  818. C := Buf[2];
  819. D := Buf[3];
  820. Round1(A, B, C, D, Data[0] + Longint($D76AA478), 7);
  821. Round1(D, A, B, C, Data[1] + Longint($E8C7B756), 12);
  822. Round1(C, D, A, B, Data[2] + Longint($242070DB), 17);
  823. Round1(B, C, D, A, Data[3] + Longint($C1BDCEEE), 22);
  824. Round1(A, B, C, D, Data[4] + Longint($F57C0FAF), 7);
  825. Round1(D, A, B, C, Data[5] + Longint($4787C62A), 12);
  826. Round1(C, D, A, B, Data[6] + Longint($A8304613), 17);
  827. Round1(B, C, D, A, Data[7] + Longint($FD469501), 22);
  828. Round1(A, B, C, D, Data[8] + Longint($698098D8), 7);
  829. Round1(D, A, B, C, Data[9] + Longint($8B44F7AF), 12);
  830. Round1(C, D, A, B, Data[10] + Longint($FFFF5BB1), 17);
  831. Round1(B, C, D, A, Data[11] + Longint($895CD7BE), 22);
  832. Round1(A, B, C, D, Data[12] + Longint($6B901122), 7);
  833. Round1(D, A, B, C, Data[13] + Longint($FD987193), 12);
  834. Round1(C, D, A, B, Data[14] + Longint($A679438E), 17);
  835. Round1(B, C, D, A, Data[15] + Longint($49B40821), 22);
  836. Round2(A, B, C, D, Data[1] + Longint($F61E2562), 5);
  837. Round2(D, A, B, C, Data[6] + Longint($C040B340), 9);
  838. Round2(C, D, A, B, Data[11] + Longint($265E5A51), 14);
  839. Round2(B, C, D, A, Data[0] + Longint($E9B6C7AA), 20);
  840. Round2(A, B, C, D, Data[5] + Longint($D62F105D), 5);
  841. Round2(D, A, B, C, Data[10] + Longint($02441453), 9);
  842. Round2(C, D, A, B, Data[15] + Longint($D8A1E681), 14);
  843. Round2(B, C, D, A, Data[4] + Longint($E7D3FBC8), 20);
  844. Round2(A, B, C, D, Data[9] + Longint($21E1CDE6), 5);
  845. Round2(D, A, B, C, Data[14] + Longint($C33707D6), 9);
  846. Round2(C, D, A, B, Data[3] + Longint($F4D50D87), 14);
  847. Round2(B, C, D, A, Data[8] + Longint($455A14ED), 20);
  848. Round2(A, B, C, D, Data[13] + Longint($A9E3E905), 5);
  849. Round2(D, A, B, C, Data[2] + Longint($FCEFA3F8), 9);
  850. Round2(C, D, A, B, Data[7] + Longint($676F02D9), 14);
  851. Round2(B, C, D, A, Data[12] + Longint($8D2A4C8A), 20);
  852. Round3(A, B, C, D, Data[5] + Longint($FFFA3942), 4);
  853. Round3(D, A, B, C, Data[8] + Longint($8771F681), 11);
  854. Round3(C, D, A, B, Data[11] + Longint($6D9D6122), 16);
  855. Round3(B, C, D, A, Data[14] + Longint($FDE5380C), 23);
  856. Round3(A, B, C, D, Data[1] + Longint($A4BEEA44), 4);
  857. Round3(D, A, B, C, Data[4] + Longint($4BDECFA9), 11);
  858. Round3(C, D, A, B, Data[7] + Longint($F6BB4B60), 16);
  859. Round3(B, C, D, A, Data[10] + Longint($BEBFBC70), 23);
  860. Round3(A, B, C, D, Data[13] + Longint($289B7EC6), 4);
  861. Round3(D, A, B, C, Data[0] + Longint($EAA127FA), 11);
  862. Round3(C, D, A, B, Data[3] + Longint($D4EF3085), 16);
  863. Round3(B, C, D, A, Data[6] + Longint($04881D05), 23);
  864. Round3(A, B, C, D, Data[9] + Longint($D9D4D039), 4);
  865. Round3(D, A, B, C, Data[12] + Longint($E6DB99E5), 11);
  866. Round3(C, D, A, B, Data[15] + Longint($1FA27CF8), 16);
  867. Round3(B, C, D, A, Data[2] + Longint($C4AC5665), 23);
  868. Round4(A, B, C, D, Data[0] + Longint($F4292244), 6);
  869. Round4(D, A, B, C, Data[7] + Longint($432AFF97), 10);
  870. Round4(C, D, A, B, Data[14] + Longint($AB9423A7), 15);
  871. Round4(B, C, D, A, Data[5] + Longint($FC93A039), 21);
  872. Round4(A, B, C, D, Data[12] + Longint($655B59C3), 6);
  873. Round4(D, A, B, C, Data[3] + Longint($8F0CCC92), 10);
  874. Round4(C, D, A, B, Data[10] + Longint($FFEFF47D), 15);
  875. Round4(B, C, D, A, Data[1] + Longint($85845DD1), 21);
  876. Round4(A, B, C, D, Data[8] + Longint($6FA87E4F), 6);
  877. Round4(D, A, B, C, Data[15] + Longint($FE2CE6E0), 10);
  878. Round4(C, D, A, B, Data[6] + Longint($A3014314), 15);
  879. Round4(B, C, D, A, Data[13] + Longint($4E0811A1), 21);
  880. Round4(A, B, C, D, Data[4] + Longint($F7537E82), 6);
  881. Round4(D, A, B, C, Data[11] + Longint($BD3AF235), 10);
  882. Round4(C, D, A, B, Data[2] + Longint($2AD7D2BB), 15);
  883. Round4(B, C, D, A, Data[9] + Longint($EB86D391), 21);
  884. Inc(Buf[0], A);
  885. Inc(Buf[1], B);
  886. Inc(Buf[2], C);
  887. Inc(Buf[3], D);
  888. end;
  889. //fixed by James McAdams
  890. procedure MDUpdate(var MDContext: TMDCtx; const Data: AnsiString; transform: TMDTransform);
  891. var
  892. Index, partLen, InputLen, I: integer;
  893. {$IFDEF SYNACODE_NATIVE}
  894. n: integer;
  895. {$ENDIF}
  896. begin
  897. InputLen := Length(Data);
  898. with MDContext do
  899. begin
  900. Index := (Count[0] shr 3) and $3F;
  901. Inc(Count[0], InputLen shl 3);
  902. if Count[0] < (InputLen shl 3) then
  903. Inc(Count[1]);
  904. Inc(Count[1], InputLen shr 29);
  905. partLen := 64 - Index;
  906. if InputLen >= partLen then
  907. begin
  908. ArrLongToByte(BufLong, BufAnsiChar);
  909. {$IFDEF SYNACODE_NATIVE}
  910. for n := 1 to partLen do
  911. BufAnsiChar[index - 1 + n] := Ord(Data[n]);
  912. {$ELSE}
  913. Move(Data[1], BufAnsiChar[Index], partLen);
  914. {$ENDIF}
  915. ArrByteToLong(BufAnsiChar, BufLong);
  916. Transform(State, Buflong);
  917. I := partLen;
  918. while I + 63 < InputLen do
  919. begin
  920. ArrLongToByte(BufLong, BufAnsiChar);
  921. {$IFDEF SYNACODE_NATIVE}
  922. for n := 1 to 64 do
  923. BufAnsiChar[n - 1] := Ord(Data[i + n]);
  924. {$ELSE}
  925. Move(Data[I+1], BufAnsiChar, 64);
  926. {$ENDIF}
  927. ArrByteToLong(BufAnsiChar, BufLong);
  928. Transform(State, Buflong);
  929. inc(I, 64);
  930. end;
  931. Index := 0;
  932. end
  933. else
  934. I := 0;
  935. ArrLongToByte(BufLong, BufAnsiChar);
  936. {$IFDEF SYNACODE_NATIVE}
  937. for n := 1 to InputLen-I do
  938. BufAnsiChar[Index + n - 1] := Ord(Data[i + n]);
  939. {$ELSE}
  940. Move(Data[I+1], BufAnsiChar[Index], InputLen-I);
  941. {$ENDIF}
  942. ArrByteToLong(BufAnsiChar, BufLong);
  943. end
  944. end;
  945. function MDFinal(var MDContext: TMDCtx; transform: TMDTransform): AnsiString;
  946. var
  947. Cnt: Word;
  948. P: Byte;
  949. digest: array[0..15] of Byte;
  950. i: Integer;
  951. n: integer;
  952. begin
  953. for I := 0 to 15 do
  954. Digest[I] := I + 1;
  955. with MDContext do
  956. begin
  957. Cnt := (Count[0] shr 3) and $3F;
  958. P := Cnt;
  959. BufAnsiChar[P] := $80;
  960. Inc(P);
  961. Cnt := 64 - 1 - Cnt;
  962. if Cnt < 8 then
  963. begin
  964. for n := 0 to cnt - 1 do
  965. BufAnsiChar[P + n] := 0;
  966. ArrByteToLong(BufAnsiChar, BufLong);
  967. // FillChar(BufAnsiChar[P], Cnt, #0);
  968. Transform(State, BufLong);
  969. ArrLongToByte(BufLong, BufAnsiChar);
  970. for n := 0 to 55 do
  971. BufAnsiChar[n] := 0;
  972. ArrByteToLong(BufAnsiChar, BufLong);
  973. // FillChar(BufAnsiChar, 56, #0);
  974. end
  975. else
  976. begin
  977. for n := 0 to Cnt - 8 - 1 do
  978. BufAnsiChar[p + n] := 0;
  979. ArrByteToLong(BufAnsiChar, BufLong);
  980. // FillChar(BufAnsiChar[P], Cnt - 8, #0);
  981. end;
  982. BufLong[14] := Count[0];
  983. BufLong[15] := Count[1];
  984. Transform(State, BufLong);
  985. ArrLongToByte(State, Digest);
  986. // Move(State, Digest, 16);
  987. Result := '';
  988. for i := 0 to 15 do
  989. Result := Result + AnsiChar(digest[i]);
  990. end;
  991. // FillChar(MD5Context, SizeOf(TMD5Ctx), #0)
  992. end;
  993. {==============================================================================}
  994. function MD5(const Value: AnsiString): AnsiString;
  995. var
  996. MDContext: TMDCtx;
  997. begin
  998. MDInit(MDContext);
  999. MDUpdate(MDContext, Value, @MD5Transform);
  1000. Result := MDFinal(MDContext, @MD5Transform);
  1001. end;
  1002. {==============================================================================}
  1003. function HMAC_MD5(Text, Key: AnsiString): AnsiString;
  1004. var
  1005. ipad, opad, s: AnsiString;
  1006. n: Integer;
  1007. MDContext: TMDCtx;
  1008. begin
  1009. if Length(Key) > 64 then
  1010. Key := md5(Key);
  1011. ipad := StringOfChar(HMAC_MD5_IPAD_CHAR, 64);
  1012. opad := StringOfChar(HMAC_MD5_OPAD_CHAR, 64);
  1013. for n := 1 to Length(Key) do
  1014. begin
  1015. ipad[n] := AnsiChar(Byte(ipad[n]) xor Byte(Key[n]));
  1016. opad[n] := AnsiChar(Byte(opad[n]) xor Byte(Key[n]));
  1017. end;
  1018. MDInit(MDContext);
  1019. MDUpdate(MDContext, ipad, @MD5Transform);
  1020. MDUpdate(MDContext, Text, @MD5Transform);
  1021. s := MDFinal(MDContext, @MD5Transform);
  1022. MDInit(MDContext);
  1023. MDUpdate(MDContext, opad, @MD5Transform);
  1024. MDUpdate(MDContext, s, @MD5Transform);
  1025. Result := MDFinal(MDContext, @MD5Transform);
  1026. end;
  1027. {==============================================================================}
  1028. function MD5LongHash(const Value: AnsiString; Len: integer): AnsiString;
  1029. var
  1030. cnt, rest: integer;
  1031. l: integer;
  1032. n: integer;
  1033. MDContext: TMDCtx;
  1034. begin
  1035. l := length(Value);
  1036. cnt := Len div l;
  1037. rest := Len mod l;
  1038. MDInit(MDContext);
  1039. for n := 1 to cnt do
  1040. MDUpdate(MDContext, Value, @MD5Transform);
  1041. if rest > 0 then
  1042. MDUpdate(MDContext, Copy(Value, 1, rest), @MD5Transform);
  1043. Result := MDFinal(MDContext, @MD5Transform);
  1044. end;
  1045. {==============================================================================}
  1046. // SHA1 is based on sources by Dave Barton ([email protected])
  1047. procedure SHA1init( var SHA1Context: TSHA1Ctx );
  1048. var
  1049. n: integer;
  1050. begin
  1051. SHA1Context.Hi := 0;
  1052. SHA1Context.Lo := 0;
  1053. SHA1Context.Index := 0;
  1054. for n := 0 to High(SHA1Context.Buffer) do
  1055. SHA1Context.Buffer[n] := 0;
  1056. for n := 0 to High(SHA1Context.HashByte) do
  1057. SHA1Context.HashByte[n] := 0;
  1058. // FillChar(SHA1Context, SizeOf(TSHA1Ctx), #0);
  1059. SHA1Context.Hash[0] := integer($67452301);
  1060. SHA1Context.Hash[1] := integer($EFCDAB89);
  1061. SHA1Context.Hash[2] := integer($98BADCFE);
  1062. SHA1Context.Hash[3] := integer($10325476);
  1063. SHA1Context.Hash[4] := integer($C3D2E1F0);
  1064. end;
  1065. //******************************************************************************
  1066. function RB(A: integer): integer;
  1067. begin
  1068. Result := (A shr 24) or ((A shr 8) and $FF00) or ((A shl 8) and $FF0000) or (A shl 24);
  1069. end;
  1070. procedure SHA1Compress(var Data: TSHA1Ctx);
  1071. var
  1072. A, B, C, D, E, T: integer;
  1073. W: array[0..79] of integer;
  1074. i: integer;
  1075. n: integer;
  1076. function F1(x, y, z: integer): integer;
  1077. begin
  1078. Result := z xor (x and (y xor z));
  1079. end;
  1080. function F2(x, y, z: integer): integer;
  1081. begin
  1082. Result := x xor y xor z;
  1083. end;
  1084. function F3(x, y, z: integer): integer;
  1085. begin
  1086. Result := (x and y) or (z and (x or y));
  1087. end;
  1088. function LRot32(X: integer; c: integer): integer;
  1089. begin
  1090. result := (x shl c) or (x shr (32 - c));
  1091. end;
  1092. begin
  1093. ArrByteToLong(Data.Buffer, W);
  1094. // Move(Data.Buffer, W, Sizeof(Data.Buffer));
  1095. for i := 0 to 15 do
  1096. W[i] := RB(W[i]);
  1097. for i := 16 to 79 do
  1098. W[i] := LRot32(W[i-3] xor W[i-8] xor W[i-14] xor W[i-16], 1);
  1099. A := Data.Hash[0];
  1100. B := Data.Hash[1];
  1101. C := Data.Hash[2];
  1102. D := Data.Hash[3];
  1103. E := Data.Hash[4];
  1104. for i := 0 to 19 do
  1105. begin
  1106. T := LRot32(A, 5) + F1(B, C, D) + E + W[i] + integer($5A827999);
  1107. E := D;
  1108. D := C;
  1109. C := LRot32(B, 30);
  1110. B := A;
  1111. A := T;
  1112. end;
  1113. for i := 20 to 39 do
  1114. begin
  1115. T := LRot32(A, 5) + F2(B, C, D) + E + W[i] + integer($6ED9EBA1);
  1116. E := D;
  1117. D := C;
  1118. C := LRot32(B, 30);
  1119. B := A;
  1120. A := T;
  1121. end;
  1122. for i := 40 to 59 do
  1123. begin
  1124. T := LRot32(A, 5) + F3(B, C, D) + E + W[i] + integer($8F1BBCDC);
  1125. E := D;
  1126. D := C;
  1127. C := LRot32(B, 30);
  1128. B := A;
  1129. A := T;
  1130. end;
  1131. for i := 60 to 79 do
  1132. begin
  1133. T := LRot32(A, 5) + F2(B, C, D) + E + W[i] + integer($CA62C1D6);
  1134. E := D;
  1135. D := C;
  1136. C := LRot32(B, 30);
  1137. B := A;
  1138. A := T;
  1139. end;
  1140. Data.Hash[0] := Data.Hash[0] + A;
  1141. Data.Hash[1] := Data.Hash[1] + B;
  1142. Data.Hash[2] := Data.Hash[2] + C;
  1143. Data.Hash[3] := Data.Hash[3] + D;
  1144. Data.Hash[4] := Data.Hash[4] + E;
  1145. for n := 0 to high(w) do
  1146. w[n] := 0;
  1147. // FillChar(W, Sizeof(W), 0);
  1148. for n := 0 to high(Data.Buffer) do
  1149. Data.Buffer[n] := 0;
  1150. // FillChar(Data.Buffer, Sizeof(Data.Buffer), 0);
  1151. end;
  1152. //******************************************************************************
  1153. procedure SHA1Update(var Context: TSHA1Ctx; const Data: AnsiString);
  1154. var
  1155. Len: integer;
  1156. n: integer;
  1157. i, k: integer;
  1158. begin
  1159. Len := Length(data);
  1160. for k := 0 to 7 do
  1161. begin
  1162. i := Context.Lo;
  1163. Inc(Context.Lo, Len);
  1164. if Context.Lo < i then
  1165. Inc(Context.Hi);
  1166. end;
  1167. for n := 1 to len do
  1168. begin
  1169. Context.Buffer[Context.Index] := byte(Data[n]);
  1170. Inc(Context.Index);
  1171. if Context.Index = 64 then
  1172. begin
  1173. Context.Index := 0;
  1174. SHA1Compress(Context);
  1175. end;
  1176. end;
  1177. end;
  1178. //******************************************************************************
  1179. function SHA1Final(var Context: TSHA1Ctx): AnsiString;
  1180. type
  1181. Pinteger = ^integer;
  1182. var
  1183. i: integer;
  1184. procedure ItoArr(var Ar: Array of byte; I, value: Integer);
  1185. begin
  1186. Ar[i + 0] := Value and $000000FF;
  1187. Ar[i + 1] := (Value shr 8) and $000000FF;
  1188. Ar[i + 2] := (Value shr 16) and $000000FF;
  1189. Ar[i + 3] := (Value shr 24) and $000000FF;
  1190. end;
  1191. begin
  1192. Context.Buffer[Context.Index] := $80;
  1193. if Context.Index >= 56 then
  1194. SHA1Compress(Context);
  1195. ItoArr(Context.Buffer, 56, RB(Context.Hi));
  1196. ItoArr(Context.Buffer, 60, RB(Context.Lo));
  1197. // Pinteger(@Context.Buffer[56])^ := RB(Context.Hi);
  1198. // Pinteger(@Context.Buffer[60])^ := RB(Context.Lo);
  1199. SHA1Compress(Context);
  1200. Context.Hash[0] := RB(Context.Hash[0]);
  1201. Context.Hash[1] := RB(Context.Hash[1]);
  1202. Context.Hash[2] := RB(Context.Hash[2]);
  1203. Context.Hash[3] := RB(Context.Hash[3]);
  1204. Context.Hash[4] := RB(Context.Hash[4]);
  1205. ArrLongToByte(Context.Hash, Context.HashByte);
  1206. Result := '';
  1207. for i := 0 to 19 do
  1208. Result := Result + AnsiChar(Context.HashByte[i]);
  1209. end;
  1210. function SHA1(const Value: AnsiString): AnsiString;
  1211. var
  1212. SHA1Context: TSHA1Ctx;
  1213. begin
  1214. SHA1Init(SHA1Context);
  1215. SHA1Update(SHA1Context, Value);
  1216. Result := SHA1Final(SHA1Context);
  1217. end;
  1218. {==============================================================================}
  1219. function HMAC_SHA1(Text, Key: AnsiString): AnsiString;
  1220. var
  1221. ipad, opad, s: AnsiString;
  1222. n: Integer;
  1223. SHA1Context: TSHA1Ctx;
  1224. begin
  1225. if Length(Key) > 64 then
  1226. Key := SHA1(Key);
  1227. ipad := StringOfChar(HMAC_SHA1_IPAD_CHAR, 64);
  1228. opad := StringOfChar(HMAC_SHA1_OPAD_CHAR, 64);
  1229. for n := 1 to Length(Key) do
  1230. begin
  1231. ipad[n] := AnsiChar(Byte(ipad[n]) xor Byte(Key[n]));
  1232. opad[n] := AnsiChar(Byte(opad[n]) xor Byte(Key[n]));
  1233. end;
  1234. SHA1Init(SHA1Context);
  1235. SHA1Update(SHA1Context, ipad);
  1236. SHA1Update(SHA1Context, Text);
  1237. s := SHA1Final(SHA1Context);
  1238. SHA1Init(SHA1Context);
  1239. SHA1Update(SHA1Context, opad);
  1240. SHA1Update(SHA1Context, s);
  1241. Result := SHA1Final(SHA1Context);
  1242. end;
  1243. {==============================================================================}
  1244. function SHA1LongHash(const Value: AnsiString; Len: integer): AnsiString;
  1245. var
  1246. cnt, rest: integer;
  1247. l: integer;
  1248. n: integer;
  1249. SHA1Context: TSHA1Ctx;
  1250. begin
  1251. l := length(Value);
  1252. cnt := Len div l;
  1253. rest := Len mod l;
  1254. SHA1Init(SHA1Context);
  1255. for n := 1 to cnt do
  1256. SHA1Update(SHA1Context, Value);
  1257. if rest > 0 then
  1258. SHA1Update(SHA1Context, Copy(Value, 1, rest));
  1259. Result := SHA1Final(SHA1Context);
  1260. end;
  1261. {==============================================================================}
  1262. procedure MD4Transform(var Buf: array of Integer; const Data: array of Integer);
  1263. var
  1264. A, B, C, D: LongInt;
  1265. function LRot32(a, b: longint): longint;
  1266. begin
  1267. Result:= (a shl b) or (a shr (32 - b));
  1268. end;
  1269. begin
  1270. A := Buf[0];
  1271. B := Buf[1];
  1272. C := Buf[2];
  1273. D := Buf[3];
  1274. A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 0], 3);
  1275. D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 1], 7);
  1276. C:= LRot32(C + (B xor (D and (A xor B))) + Data[ 2], 11);
  1277. B:= LRot32(B + (A xor (C and (D xor A))) + Data[ 3], 19);
  1278. A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 4], 3);
  1279. D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 5], 7);
  1280. C:= LRot32(C + (B xor (D and (A xor B))) + Data[ 6], 11);
  1281. B:= LRot32(B + (A xor (C and (D xor A))) + Data[ 7], 19);
  1282. A:= LRot32(A + (D xor (B and (C xor D))) + Data[ 8], 3);
  1283. D:= LRot32(D + (C xor (A and (B xor C))) + Data[ 9], 7);
  1284. C:= LRot32(C + (B xor (D and (A xor B))) + Data[10], 11);
  1285. B:= LRot32(B + (A xor (C and (D xor A))) + Data[11], 19);
  1286. A:= LRot32(A + (D xor (B and (C xor D))) + Data[12], 3);
  1287. D:= LRot32(D + (C xor (A and (B xor C))) + Data[13], 7);
  1288. C:= LRot32(C + (B xor (D and (A xor B))) + Data[14], 11);
  1289. B:= LRot32(B + (A xor (C and (D xor A))) + Data[15], 19);
  1290. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 0] + longint($5a827999), 3);
  1291. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 4] + longint($5a827999), 5);
  1292. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[ 8] + longint($5a827999), 9);
  1293. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[12] + longint($5a827999), 13);
  1294. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 1] + longint($5a827999), 3);
  1295. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 5] + longint($5a827999), 5);
  1296. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[ 9] + longint($5a827999), 9);
  1297. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[13] + longint($5a827999), 13);
  1298. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 2] + longint($5a827999), 3);
  1299. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 6] + longint($5a827999), 5);
  1300. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[10] + longint($5a827999), 9);
  1301. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[14] + longint($5a827999), 13);
  1302. A:= LRot32(A + ((B and C) or (B and D) or (C and D)) + Data[ 3] + longint($5a827999), 3);
  1303. D:= LRot32(D + ((A and B) or (A and C) or (B and C)) + Data[ 7] + longint($5a827999), 5);
  1304. C:= LRot32(C + ((D and A) or (D and B) or (A and B)) + Data[11] + longint($5a827999), 9);
  1305. B:= LRot32(B + ((C and D) or (C and A) or (D and A)) + Data[15] + longint($5a827999), 13);
  1306. A:= LRot32(A + (B xor C xor D) + Data[ 0] + longint($6ed9eba1), 3);
  1307. D:= LRot32(D + (A xor B xor C) + Data[ 8] + longint($6ed9eba1), 9);
  1308. C:= LRot32(C + (D xor A xor B) + Data[ 4] + longint($6ed9eba1), 11);
  1309. B:= LRot32(B + (C xor D xor A) + Data[12] + longint($6ed9eba1), 15);
  1310. A:= LRot32(A + (B xor C xor D) + Data[ 2] + longint($6ed9eba1), 3);
  1311. D:= LRot32(D + (A xor B xor C) + Data[10] + longint($6ed9eba1), 9);
  1312. C:= LRot32(C + (D xor A xor B) + Data[ 6] + longint($6ed9eba1), 11);
  1313. B:= LRot32(B + (C xor D xor A) + Data[14] + longint($6ed9eba1), 15);
  1314. A:= LRot32(A + (B xor C xor D) + Data[ 1] + longint($6ed9eba1), 3);
  1315. D:= LRot32(D + (A xor B xor C) + Data[ 9] + longint($6ed9eba1), 9);
  1316. C:= LRot32(C + (D xor A xor B) + Data[ 5] + longint($6ed9eba1), 11);
  1317. B:= LRot32(B + (C xor D xor A) + Data[13] + longint($6ed9eba1), 15);
  1318. A:= LRot32(A + (B xor C xor D) + Data[ 3] + longint($6ed9eba1), 3);
  1319. D:= LRot32(D + (A xor B xor C) + Data[11] + longint($6ed9eba1), 9);
  1320. C:= LRot32(C + (D xor A xor B) + Data[ 7] + longint($6ed9eba1), 11);
  1321. B:= LRot32(B + (C xor D xor A) + Data[15] + longint($6ed9eba1), 15);
  1322. Inc(Buf[0], A);
  1323. Inc(Buf[1], B);
  1324. Inc(Buf[2], C);
  1325. Inc(Buf[3], D);
  1326. end;
  1327. {==============================================================================}
  1328. function MD4(const Value: AnsiString): AnsiString;
  1329. var
  1330. MDContext: TMDCtx;
  1331. begin
  1332. MDInit(MDContext);
  1333. MDUpdate(MDContext, Value, @MD4Transform);
  1334. Result := MDFinal(MDContext, @MD4Transform);
  1335. end;
  1336. {==============================================================================}
  1337. end.