home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / rexx_md5.zip / testmd5.cmd < prev    next >
OS/2 REXX Batch file  |  1998-05-10  |  10KB  |  341 lines

  1. /* Test the "rexx only" MD5 and the "dll" MD5
  2.    This uses the test strings found in rfc131 (that defines the MD5 digest
  3.    algorithim  */
  4.  
  5. signal on error name foo1 ; signal on syntax name foo1 ;
  6.  
  7. /* Load all functions in the SRXFUNC dll. There's only one now (SRX_MD5),
  8.    but maybe more will be added someday */
  9.  
  10. if rxfuncquery('srx_md5')=1  then do
  11.   call RXFuncAdd 'SRXLoadFuncs', 'SRXFUNC', 'SRXLoadFuncs'
  12.   call SRXLoadFuncs
  13. end
  14. if rxfuncquery('srx_md5')=1  then do
  15.   say " Could not load SRX_MD5 from SRXFUNC.DLL"
  16.   exit
  17. end
  18.  
  19. /* Begin Tests */
  20.  
  21.  
  22. say ' For: "" (empty string) '
  23. say "   MD5 should be: d41d8cd98f00b204e9800998ecf8427e"
  24. a1=srx_md5('')
  25. say "        From dll: "a1
  26. a2=rexx_md5('')
  27. say "       From rexx: "a2
  28.  
  29. say
  30. say ' For: "a" (just the character "a", not including the "s) '
  31. say "   MD5 should be: 0cc175b9c0f1b6a831c399e269772661" 
  32. a1=srx_md5('a')
  33. say "        From dll: "a1
  34. a2=rexx_md5('a')
  35. say "       From rexx: "a2
  36.  
  37. say
  38. say ' For: "abc" '
  39. say "   MD5 should be: 900150983cd24fb0d6963f7d28e17f72"
  40. a1=srx_md5('abc')
  41. say "        From dll: "a1
  42. a2=rexx_md5('abc')
  43. say "       From rexx: "a2
  44.  
  45. say
  46. test='message digest'
  47. say '  For: "'test'"'
  48. say "   MD5 should be: f96b697d7cb7938d525a2f31aaf161d0"
  49. a1=srx_md5(test)
  50. say "        From dll: "a1
  51. a2=rexx_md5(test)
  52. say "       From rexx: "a2
  53.  
  54. say
  55. test="abcdefghijklmnopqrstuvwxyz" 
  56. say '  For: "'test'"'
  57. say "   MD5 should be: c3fcd3d76192e4007dfb496cca67e13b"
  58. a1=srx_md5(test)
  59. say "        From dll: "a1
  60. a2=rexx_md5(test)
  61. say "       From rexx: "a2
  62.  
  63. say
  64. test="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  65. say '  For: "'test'"'
  66. say "   MD5 should be: d174ab98d277d9f5a5611c2c9f419d9f"
  67. a1=srx_md5(test)
  68. say "        From dll: "a1
  69. a2=rexx_md5(test)
  70. say "       From rexx: "a2
  71.  
  72. say
  73. test="12345678901234567890123456789012345678901234567890123456789012345678901234567890"
  74. say '  For: "'test'"'
  75. say "   MD5 should be: 57edf4a22be3c955ac49da2e2107b67a"
  76. a1=srx_md5(test)
  77. say "        From dll: "a1
  78. a2=rexx_md5(test)
  79. say "       From rexx: "a2
  80.  
  81.  
  82. if stream('testmd5.cmd','c','query exists')<>'' then do
  83.   test=charin('testmd5.cmd',1,chars('testmd5.cmd'))
  84.   say
  85.   say " For file: testmd5.CMD"
  86.   a1=srx_md5(test)
  87.   say "        From dll: "a1
  88.   a2=rexx_md5(test)
  89.   say "       From rexx: "a2
  90. end
  91.  
  92.  
  93.  
  94. call SRXDropFuncs
  95. exit
  96.  
  97. foo1: 
  98. say " error " rc sigl
  99. exit
  100.  
  101.  
  102.  
  103. /*  ------------------------------ */
  104. /* this is an "all rexx" md5 procedure. It works, but it is slow */
  105. rexx_md5:procedure
  106. parse arg stuff
  107. numeric digits 11
  108. lenstuff=length(stuff)
  109.  
  110. c0=d2c(0)
  111. c1=d2c(128)
  112. c1a=d2c(255)
  113. c1111=c1a||c1a||c1a||c1a
  114. slen=length(stuff)*8
  115. slen512=slen//512
  116.  
  117. /* pad message to multiple of 512 bits.  Last 2 words are 64 bit # bits in message*/
  118. if slen512=448 then  addme=512
  119. if slen512<448 then addme=448-slen512
  120. if slen512>448 then addme=960-slen512
  121. addwords=addme/8
  122.  
  123. apad=c1||copies(c0,addwords-1)
  124.  
  125. xlen=reverse(right(d2c(lenstuff*8),4,c0))||c0||c0||c0||c0  /* 2**32 max bytes in message */
  126.  
  127. /* NEWSTUFF is the message to be md5'ed */
  128. newstuff=stuff||apad||xlen
  129.  
  130. /* starting values of registers */
  131.  a ='67452301'x;
  132.  b ='efcdab89'x;
  133.  c ='98badcfe'x;
  134.  d ='10325476'x;
  135.  
  136. lennews=length(newstuff)/4
  137.  
  138. /* loop through entire message */
  139. do i1 = 0 to ((lennews/16)-1)
  140.   i16=i1*64
  141.   do j=1 to 16
  142.      j4=((j-1)*4)+1
  143.      jj=i16+j4
  144.      m.j=reverse(substr(newstuff,jj,4))
  145.   end /* do */
  146.  
  147. /* transform this block of 16 chars to 4 values. Save prior values first */
  148.  aa=a;bb=b;cc=c;dd=d
  149.  
  150. /* do 4 rounds, 16 operations per round (rounds differ in bit'ing functions */
  151. S11=7
  152. S12=12
  153. S13=17
  154. S14=22
  155.   a=round1( a, b, c, d,   0 , S11, 3614090360); /* 1 */
  156.   d=round1( d, a, b, c,   1 , S12, 3905402710); /* 2 */
  157.   c=round1( c, d, a, b,   2 , S13,  606105819); /* 3 */
  158.   b=round1( b, c, d, a,   3 , S14, 3250441966); /* 4 */
  159.   a=round1( a, b, c, d,   4 , S11, 4118548399); /* 5 */
  160.   d=round1( d, a, b, c,   5 , S12, 1200080426); /* 6 */
  161.   c=round1( c, d, a, b,   6 , S13, 2821735955); /* 7 */
  162.   b=round1( b, c, d, a,   7 , S14, 4249261313); /* 8 */
  163.   a=round1( a, b, c, d,   8 , S11, 1770035416); /* 9 */
  164.   d=round1( d, a, b, c,   9 , S12, 2336552879); /* 10 */
  165.   c=round1( c, d, a, b,  10 , S13, 4294925233); /* 11 */
  166.   b=round1( b, c, d, a,  11 , S14, 2304563134); /* 12 */
  167.   a=round1( a, b, c, d,  12 , S11, 1804603682); /* 13 */
  168.   d=round1( d, a, b, c,  13 , S12, 4254626195); /* 14 */
  169.   c=round1( c, d, a, b,  14 , S13, 2792965006); /* 15 */
  170.   b=round1( b, c, d, a,  15 , S14, 1236535329); /* 16 */
  171.  
  172.   /* Round 2 */
  173. S21=5
  174. S22=9
  175. S23=14
  176. S24=20
  177. a= round2( a, b, c, d,   1 , S21, 4129170786); /* 17 */
  178. d= round2( d, a, b, c,   6 , S22, 3225465664); /* 18 */
  179. c=  round2( c, d, a, b,  11 , S23,  643717713); /* 19 */
  180. b=  round2( b, c, d, a,   0 , S24, 3921069994); /* 20 */
  181. a=  round2( a, b, c, d,   5 , S21, 3593408605); /* 21 */
  182. d=  round2( d, a, b, c,  10 , S22,   38016083); /* 22 */
  183. c=  round2( c, d, a, b,  15 , S23, 3634488961); /* 23 */
  184. b= round2( b, c, d, a,   4 , S24, 3889429448); /* 24 */
  185. a= round2( a, b, c, d,   9 , S21,  568446438); /* 25 */
  186. d= round2( d, a, b, c,  14 , S22, 3275163606); /* 26 */
  187. c=  round2( c, d, a, b,   3 , S23, 4107603335); /* 27 */
  188. b=  round2( b, c, d, a,   8 , S24, 1163531501); /* 28 */
  189. a=  round2( a, b, c, d,  13 , S21, 2850285829); /* 29 */
  190. d=  round2( d, a, b, c,   2 , S22, 4243563512); /* 30 */
  191. c=  round2( c, d, a, b,   7 , S23, 1735328473); /* 31 */
  192. b= round2( b, c, d, a,  12 , S24, 2368359562); /* 32 */
  193.  
  194.   /* Round 3 */
  195. S31= 4
  196. S32= 11
  197. S33= 16
  198. S34= 23
  199. a= round3( a, b, c, d,   5 , S31, 4294588738); /* 33 */
  200. d=  round3( d, a, b, c,   8 , S32, 2272392833); /* 34 */
  201. c=  round3( c, d, a, b,  11 , S33, 1839030562); /* 35 */
  202. b=  round3( b, c, d, a,  14 , S34, 4259657740); /* 36 */
  203. a=  round3( a, b, c, d,   1 , S31, 2763975236); /* 37 */
  204. d=  round3( d, a, b, c,   4 , S32, 1272893353); /* 38 */
  205. c=  round3( c, d, a, b,   7 , S33, 4139469664); /* 39 */
  206. b=  round3( b, c, d, a,  10 , S34, 3200236656); /* 40 */
  207. a=  round3( a, b, c, d,  13 , S31,  681279174); /* 41 */
  208. d=  round3( d, a, b, c,   0 , S32, 3936430074); /* 42 */
  209. c=  round3( c, d, a, b,   3 , S33, 3572445317); /* 43 */
  210. b=  round3( b, c, d, a,   6 , S34,   76029189); /* 44 */
  211. a=  round3( a, b, c, d,   9 , S31, 3654602809); /* 45 */
  212. d=  round3( d, a, b, c,  12 , S32, 3873151461); /* 46 */
  213. c=  round3( c, d, a, b,  15 , S33,  530742520); /* 47 */
  214. b=  round3( b, c, d, a,   2 , S34, 3299628645); /* 48 */
  215.  
  216.   /* Round 4 */
  217. S41=6
  218. S42=10
  219. S43=15
  220. s44=21
  221. a=round4( a, b, c, d,   0 , S41, 4096336452); /* 49 */
  222. d=round4( d, a, b, c,   7 , S42, 1126891415); /* 50 */
  223. c=round4( c, d, a, b,  14 , S43, 2878612391); /* 51 */
  224. b=round4( b, c, d, a,   5 , s44, 4237533241); /* 52 */
  225. a=round4( a, b, c, d,  12 , S41, 1700485571); /* 53 */
  226. d=round4( d, a, b, c,   3 , S42, 2399980690); /* 54 */
  227. c=round4( c, d, a, b,  10 , S43, 4293915773); /* 55 */
  228. b=round4( b, c, d, a,   1 , s44,  2240044497); /* 56 */
  229. a=round4( a, b, c, d,   8 , S41, 1873313359); /* 57 */
  230. d=round4( d, a, b, c,  15 , S42, 4264355552); /* 58 */
  231. c=round4( c, d, a, b,   6 , S43, 2734768916); /* 59 */
  232. b=round4( b, c, d, a,  13 , s44, 1309151649); /* 60 */
  233. a=round4( a, b, c, d,   4 , S41, 4149444226); /* 61 */
  234. d=round4( d, a, b, c,  11 , S42, 3174756917); /* 62 */
  235. c=round4( c, d, a, b,   2 , S43,  718787259); /* 63 */
  236. b=round4( b, c, d, a,   9 , s44, 3951481745); /* 64 */
  237.  
  238.  
  239. a=m32add(aa,a) ; b=m32add(bb,b) ; c=m32add(cc,c) ; d=m32add(dd,d)
  240.  
  241. end
  242.  
  243. aa=c2x(reverse(a))||c2x(reverse(b))||c2x(reverse(C))||c2x(reverse(D))
  244. return aa
  245.  
  246. /* round 1 to 4 functins */
  247.  
  248. round1:procedure expose m. c1111 c0 c1
  249. parse arg a1,b1,c1,d1,kth,shift,sini
  250. kth=kth+1
  251. t1=c2d(a1)+c2d(f(b1,c1,d1))+ c2d(m.kth) + sini
  252. t1a=right(d2c(t1),4,c0)
  253. t2=rotleft(t1a,shift)
  254. t3=m32add(t2,b1)
  255. return t3
  256.  
  257. round2:procedure expose m. c1111 c0 c1
  258. parse arg a1,b1,c1,d1,kth,shift,sini
  259. kth=kth+1
  260. t1=c2d(a1)+c2d(g(b1,c1,d1))+ c2d(m.kth) + sini
  261. t1a=right(d2c(t1),4,c0)
  262. t2=rotleft(t1a,shift)
  263. t3=m32add(t2,b1)
  264. return t3
  265.  
  266. round3:procedure expose m. c1111 c0 c1
  267. parse arg a1,b1,c1,d1,kth,shift,sini
  268. kth=kth+1
  269. t1=c2d(a1)+c2d(h(b1,c1,d1))+ c2d(m.kth) + sini
  270. t1a=right(d2c(t1),4,c0)
  271. t2=rotleft(t1a,shift)
  272. t3=m32add(t2,b1)
  273. return t3
  274.  
  275. round4:procedure expose m. c1111 c0 c1
  276. parse arg a1,b1,c1,d1,kth,shift,sini
  277. kth=kth+1
  278. t1=c2d(a1)+c2d(i(b1,c1,d1))+ c2d(m.kth) + sini
  279. t1a=right(d2c(t1),4,c0)
  280. t2=rotleft(t1a,shift)
  281. t3=m32add(t2,b1)
  282. return t3
  283.  
  284. /* add to "char" numbers, modulo 2**32, return as char */
  285. m32add:procedure expose c0 c1 c1111
  286. parse arg v1,v2
  287. t1=c2d(v1)+c2d(v2)
  288. t2=d2c(t1)
  289. t3=right(t2,4,c0)
  290. return t3
  291.  
  292.  
  293.  
  294. /*********** Basic functions */
  295. /* F(x, y, z) == (((x) & (y)) | ((~x) & (z))) */
  296. f:procedure expose c0 c1 c1111 
  297. parse arg x,y,z
  298. t1=bitand(x,y)
  299. notx=bitxor(x,c1111)
  300. t2=bitand(notx,z)
  301. return bitor(t1,t2)
  302.  
  303.  
  304. /* G(x, y, z) == (((x) & (z)) | ((y) & (~z)))*/
  305. g:procedure expose c0 c1 c1111
  306. parse arg x,y,z
  307. t1=bitand(x,z)
  308. notz=bitxor(z,c1111)
  309. t2=bitand(y,notz)
  310. return bitor(t1,t2)
  311.  
  312. /* H(x, y, z) == ((x) ^ (y) ^ (z)) */
  313. h:procedure expose c0 c1 c1111
  314. parse arg x,y,z
  315. t1=bitxor(x,y)
  316. return bitxor(t1,z)
  317.  
  318. /* I(x, y, z) == ((y) ^ ((x) | (~z))) */
  319. i:procedure expose c0 c1 c1111
  320. parse arg x,y,z
  321. notz=bitxor(z,c1111)
  322. t2=bitor(x,notz)
  323. return bitxor(y,t2)
  324.  
  325. /* bit rotate to the left by s positions */
  326. rotleft:procedure 
  327. parse arg achar,s
  328. if s=0 then return achar
  329.  
  330. bits=x2b(c2x(achar))
  331. lb=length(bits)
  332. t1=left(bits,s)
  333. t2=bits||t1
  334. yib=right(t2,lb)
  335. return x2c(b2x(yib))
  336.  
  337.  
  338.  
  339.  
  340.  
  341.