| 1 | /* 7 May 1998. Daniel Hellerstein, danielh@crosslink.net
|
|---|
| 2 |
|
|---|
| 3 | REXX_MD5:A "fully rexx" md5 digest procedure.
|
|---|
| 4 |
|
|---|
| 5 | Usage:
|
|---|
| 6 | anmd5=REXX_MD5(a_string)
|
|---|
| 7 | where
|
|---|
| 8 | a_string is any string
|
|---|
| 9 | anmd5 will be the 32 character (hex character) MD5 digest.
|
|---|
| 10 |
|
|---|
| 11 | Example (there are more below):
|
|---|
| 12 |
|
|---|
| 13 | a1=rexx_md5('abc')
|
|---|
| 14 | say " md5 of abc should be: 900150983cd24fb0d6963f7d28e17f72"
|
|---|
| 15 | say " the value we got is:" a1
|
|---|
| 16 |
|
|---|
| 17 | Notes:
|
|---|
| 18 |
|
|---|
| 19 | * As an "all REXX" procedure (no dll's), this uses REXX math.
|
|---|
| 20 | Thus -- it is NOT FAST. For small strings it is
|
|---|
| 21 | toleable (0.15 seconds on a p166 for 50 character strings),
|
|---|
| 22 | but for larger strings (or files) it can take many seconds --
|
|---|
| 23 | you should instead use a DLL product (such as MD5_OS2 at
|
|---|
| 24 | hobbes.nmsu.edu)
|
|---|
| 25 |
|
|---|
| 26 | * For details on the MD5 digest, see the Internet RFC #1321
|
|---|
| 27 | (try http://theory.lcs.mit.edu/~rivest/rfc1321.txt7; or
|
|---|
| 28 | look for RFC1321 using your favorite search engine)
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /* BEGIN eXAMPLES ------ (from rfc1375)
|
|---|
| 34 |
|
|---|
| 35 | a1=rexx_md5('')
|
|---|
| 36 | if lower(a1)="d41d8cd98f00b204e9800998ecf8427e" then say " ok space "
|
|---|
| 37 |
|
|---|
| 38 | a1=lower(rexx_md5('a'))
|
|---|
| 39 | if a1="0cc175b9c0f1b6a831c399e269772661" then say " ok a "
|
|---|
| 40 |
|
|---|
| 41 | a1=rexx_md5('abc')
|
|---|
| 42 | if lower(a1)='900150983cd24fb0d6963f7d28e17f72' then say " ok abc"
|
|---|
| 43 |
|
|---|
| 44 | a1=rexx_md5('message digest')
|
|---|
| 45 | if lower(a1)='f96b697d7cb7938d525a2f31aaf161d0' then say "ok messgae digest "
|
|---|
| 46 |
|
|---|
| 47 | if "c3fcd3d76192e4007dfb496cca67e13b"=lower(rexx_md5("abcdefghijklmnopqrstuvwxyz")) then
|
|---|
| 48 | say " ok ab..z"
|
|---|
| 49 |
|
|---|
| 50 | if "d174ab98d277d9f5a5611c2c9f419d9f"= lower(rexx_md5( ,
|
|---|
| 51 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")) then
|
|---|
| 52 | say " ok ABC ... 89"
|
|---|
| 53 |
|
|---|
| 54 | if "57edf4a22be3c955ac49da2e2107b67a"= ,
|
|---|
| 55 | lower(rexx_md5("12345678901234567890123456789012345678901234567890123456789012345678901234567890")) ,
|
|---|
| 56 | then
|
|---|
| 57 | say " ok 123....123..."
|
|---|
| 58 |
|
|---|
| 59 | --------- END OF EXAMPLES */
|
|---|
| 60 |
|
|---|
| 61 | /* ------------------------------ */
|
|---|
| 62 | rexx_md5:procedure /* if called externally, remove the "procedure" */
|
|---|
| 63 | parse arg stuff
|
|---|
| 64 |
|
|---|
| 65 | numeric digits 11
|
|---|
| 66 | lenstuff=length(stuff)
|
|---|
| 67 |
|
|---|
| 68 | c0=d2c(0)
|
|---|
| 69 | c1=d2c(128)
|
|---|
| 70 | c1a=d2c(255)
|
|---|
| 71 | c1111=c1a||c1a||c1a||c1a
|
|---|
| 72 | slen=length(stuff)*8
|
|---|
| 73 | slen512=slen//512
|
|---|
| 74 |
|
|---|
| 75 | /* pad message to multiple of 512 bits. Last 2 words are 64 bit # bits in message*/
|
|---|
| 76 | if slen512=448 then addme=512
|
|---|
| 77 | if slen512<448 then addme=448-slen512
|
|---|
| 78 | if slen512>448 then addme=960-slen512
|
|---|
| 79 | addwords=addme/8
|
|---|
| 80 |
|
|---|
| 81 | apad=c1||copies(c0,addwords-1)
|
|---|
| 82 |
|
|---|
| 83 | xlen=reverse(right(d2c(lenstuff*8),4,c0))||c0||c0||c0||c0 /* 2**32 max bytes in message */
|
|---|
| 84 |
|
|---|
| 85 | /* NEWSTUFF is the message to be md5'ed */
|
|---|
| 86 | newstuff=stuff||apad||xlen
|
|---|
| 87 |
|
|---|
| 88 | /* starting values of registers */
|
|---|
| 89 | a ='67452301'x;
|
|---|
| 90 | b ='efcdab89'x;
|
|---|
| 91 | c ='98badcfe'x;
|
|---|
| 92 | d ='10325476'x;
|
|---|
| 93 |
|
|---|
| 94 | lennews=length(newstuff)/4
|
|---|
| 95 |
|
|---|
| 96 | /* loop through entire message */
|
|---|
| 97 | do i1 = 0 to ((lennews/16)-1)
|
|---|
| 98 | i16=i1*64
|
|---|
| 99 | do j=1 to 16
|
|---|
| 100 | j4=((j-1)*4)+1
|
|---|
| 101 | jj=i16+j4
|
|---|
| 102 | m.j=reverse(substr(newstuff,jj,4))
|
|---|
| 103 | end /* do */
|
|---|
| 104 |
|
|---|
| 105 | /* transform this block of 16 chars to 4 values. Save prior values first */
|
|---|
| 106 | aa=a;bb=b;cc=c;dd=d
|
|---|
| 107 |
|
|---|
| 108 | /* do 4 rounds, 16 operations per round (rounds differ in bit'ing functions */
|
|---|
| 109 | S11=7
|
|---|
| 110 | S12=12
|
|---|
| 111 | S13=17
|
|---|
| 112 | S14=22
|
|---|
| 113 | a=round1( a, b, c, d, 0 , S11, 3614090360); /* 1 */
|
|---|
| 114 | d=round1( d, a, b, c, 1 , S12, 3905402710); /* 2 */
|
|---|
| 115 | c=round1( c, d, a, b, 2 , S13, 606105819); /* 3 */
|
|---|
| 116 | b=round1( b, c, d, a, 3 , S14, 3250441966); /* 4 */
|
|---|
| 117 | a=round1( a, b, c, d, 4 , S11, 4118548399); /* 5 */
|
|---|
| 118 | d=round1( d, a, b, c, 5 , S12, 1200080426); /* 6 */
|
|---|
| 119 | c=round1( c, d, a, b, 6 , S13, 2821735955); /* 7 */
|
|---|
| 120 | b=round1( b, c, d, a, 7 , S14, 4249261313); /* 8 */
|
|---|
| 121 | a=round1( a, b, c, d, 8 , S11, 1770035416); /* 9 */
|
|---|
| 122 | d=round1( d, a, b, c, 9 , S12, 2336552879); /* 10 */
|
|---|
| 123 | c=round1( c, d, a, b, 10 , S13, 4294925233); /* 11 */
|
|---|
| 124 | b=round1( b, c, d, a, 11 , S14, 2304563134); /* 12 */
|
|---|
| 125 | a=round1( a, b, c, d, 12 , S11, 1804603682); /* 13 */
|
|---|
| 126 | d=round1( d, a, b, c, 13 , S12, 4254626195); /* 14 */
|
|---|
| 127 | c=round1( c, d, a, b, 14 , S13, 2792965006); /* 15 */
|
|---|
| 128 | b=round1( b, c, d, a, 15 , S14, 1236535329); /* 16 */
|
|---|
| 129 |
|
|---|
| 130 | /* Round 2 */
|
|---|
| 131 | S21=5
|
|---|
| 132 | S22=9
|
|---|
| 133 | S23=14
|
|---|
| 134 | S24=20
|
|---|
| 135 | a= round2( a, b, c, d, 1 , S21, 4129170786); /* 17 */
|
|---|
| 136 | d= round2( d, a, b, c, 6 , S22, 3225465664); /* 18 */
|
|---|
| 137 | c= round2( c, d, a, b, 11 , S23, 643717713); /* 19 */
|
|---|
| 138 | b= round2( b, c, d, a, 0 , S24, 3921069994); /* 20 */
|
|---|
| 139 | a= round2( a, b, c, d, 5 , S21, 3593408605); /* 21 */
|
|---|
| 140 | d= round2( d, a, b, c, 10 , S22, 38016083); /* 22 */
|
|---|
| 141 | c= round2( c, d, a, b, 15 , S23, 3634488961); /* 23 */
|
|---|
| 142 | b= round2( b, c, d, a, 4 , S24, 3889429448); /* 24 */
|
|---|
| 143 | a= round2( a, b, c, d, 9 , S21, 568446438); /* 25 */
|
|---|
| 144 | d= round2( d, a, b, c, 14 , S22, 3275163606); /* 26 */
|
|---|
| 145 | c= round2( c, d, a, b, 3 , S23, 4107603335); /* 27 */
|
|---|
| 146 | b= round2( b, c, d, a, 8 , S24, 1163531501); /* 28 */
|
|---|
| 147 | a= round2( a, b, c, d, 13 , S21, 2850285829); /* 29 */
|
|---|
| 148 | d= round2( d, a, b, c, 2 , S22, 4243563512); /* 30 */
|
|---|
| 149 | c= round2( c, d, a, b, 7 , S23, 1735328473); /* 31 */
|
|---|
| 150 | b= round2( b, c, d, a, 12 , S24, 2368359562); /* 32 */
|
|---|
| 151 |
|
|---|
| 152 | /* Round 3 */
|
|---|
| 153 | S31= 4
|
|---|
| 154 | S32= 11
|
|---|
| 155 | S33= 16
|
|---|
| 156 | S34= 23
|
|---|
| 157 | a= round3( a, b, c, d, 5 , S31, 4294588738); /* 33 */
|
|---|
| 158 | d= round3( d, a, b, c, 8 , S32, 2272392833); /* 34 */
|
|---|
| 159 | c= round3( c, d, a, b, 11 , S33, 1839030562); /* 35 */
|
|---|
| 160 | b= round3( b, c, d, a, 14 , S34, 4259657740); /* 36 */
|
|---|
| 161 | a= round3( a, b, c, d, 1 , S31, 2763975236); /* 37 */
|
|---|
| 162 | d= round3( d, a, b, c, 4 , S32, 1272893353); /* 38 */
|
|---|
| 163 | c= round3( c, d, a, b, 7 , S33, 4139469664); /* 39 */
|
|---|
| 164 | b= round3( b, c, d, a, 10 , S34, 3200236656); /* 40 */
|
|---|
| 165 | a= round3( a, b, c, d, 13 , S31, 681279174); /* 41 */
|
|---|
| 166 | d= round3( d, a, b, c, 0 , S32, 3936430074); /* 42 */
|
|---|
| 167 | c= round3( c, d, a, b, 3 , S33, 3572445317); /* 43 */
|
|---|
| 168 | b= round3( b, c, d, a, 6 , S34, 76029189); /* 44 */
|
|---|
| 169 | a= round3( a, b, c, d, 9 , S31, 3654602809); /* 45 */
|
|---|
| 170 | d= round3( d, a, b, c, 12 , S32, 3873151461); /* 46 */
|
|---|
| 171 | c= round3( c, d, a, b, 15 , S33, 530742520); /* 47 */
|
|---|
| 172 | b= round3( b, c, d, a, 2 , S34, 3299628645); /* 48 */
|
|---|
| 173 |
|
|---|
| 174 | /* Round 4 */
|
|---|
| 175 | S41=6
|
|---|
| 176 | S42=10
|
|---|
| 177 | S43=15
|
|---|
| 178 | s44=21
|
|---|
| 179 | a=round4( a, b, c, d, 0 , S41, 4096336452); /* 49 */
|
|---|
| 180 | d=round4( d, a, b, c, 7 , S42, 1126891415); /* 50 */
|
|---|
| 181 | c=round4( c, d, a, b, 14 , S43, 2878612391); /* 51 */
|
|---|
| 182 | b=round4( b, c, d, a, 5 , s44, 4237533241); /* 52 */
|
|---|
| 183 | a=round4( a, b, c, d, 12 , S41, 1700485571); /* 53 */
|
|---|
| 184 | d=round4( d, a, b, c, 3 , S42, 2399980690); /* 54 */
|
|---|
| 185 | c=round4( c, d, a, b, 10 , S43, 4293915773); /* 55 */
|
|---|
| 186 | b=round4( b, c, d, a, 1 , s44, 2240044497); /* 56 */
|
|---|
| 187 | a=round4( a, b, c, d, 8 , S41, 1873313359); /* 57 */
|
|---|
| 188 | d=round4( d, a, b, c, 15 , S42, 4264355552); /* 58 */
|
|---|
| 189 | c=round4( c, d, a, b, 6 , S43, 2734768916); /* 59 */
|
|---|
| 190 | b=round4( b, c, d, a, 13 , s44, 1309151649); /* 60 */
|
|---|
| 191 | a=round4( a, b, c, d, 4 , S41, 4149444226); /* 61 */
|
|---|
| 192 | d=round4( d, a, b, c, 11 , S42, 3174756917); /* 62 */
|
|---|
| 193 | c=round4( c, d, a, b, 2 , S43, 718787259); /* 63 */
|
|---|
| 194 | b=round4( b, c, d, a, 9 , s44, 3951481745); /* 64 */
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | a=m32add(aa,a) ; b=m32add(bb,b) ; c=m32add(cc,c) ; d=m32add(dd,d)
|
|---|
| 198 |
|
|---|
| 199 | end
|
|---|
| 200 |
|
|---|
| 201 | aa=c2x(reverse(a))||c2x(reverse(b))||c2x(reverse(C))||c2x(reverse(D))
|
|---|
| 202 | return aa
|
|---|
| 203 |
|
|---|
| 204 | /* round 1 to 4 functins */
|
|---|
| 205 |
|
|---|
| 206 | round1:procedure expose m. c1111 c0 c1
|
|---|
| 207 | parse arg a1,b1,c1,d1,kth,shift,sini
|
|---|
| 208 | kth=kth+1
|
|---|
| 209 | t1=c2d(a1)+c2d(f(b1,c1,d1))+ c2d(m.kth) + sini
|
|---|
| 210 | t1a=right(d2c(t1),4,c0)
|
|---|
| 211 | t2=rotleft(t1a,shift)
|
|---|
| 212 | t3=m32add(t2,b1)
|
|---|
| 213 | return t3
|
|---|
| 214 |
|
|---|
| 215 | round2:procedure expose m. c1111 c0 c1
|
|---|
| 216 | parse arg a1,b1,c1,d1,kth,shift,sini
|
|---|
| 217 | kth=kth+1
|
|---|
| 218 | t1=c2d(a1)+c2d(g(b1,c1,d1))+ c2d(m.kth) + sini
|
|---|
| 219 | t1a=right(d2c(t1),4,c0)
|
|---|
| 220 | t2=rotleft(t1a,shift)
|
|---|
| 221 | t3=m32add(t2,b1)
|
|---|
| 222 | return t3
|
|---|
| 223 |
|
|---|
| 224 | round3:procedure expose m. c1111 c0 c1
|
|---|
| 225 | parse arg a1,b1,c1,d1,kth,shift,sini
|
|---|
| 226 | kth=kth+1
|
|---|
| 227 | t1=c2d(a1)+c2d(h(b1,c1,d1))+ c2d(m.kth) + sini
|
|---|
| 228 | t1a=right(d2c(t1),4,c0)
|
|---|
| 229 | t2=rotleft(t1a,shift)
|
|---|
| 230 | t3=m32add(t2,b1)
|
|---|
| 231 | return t3
|
|---|
| 232 |
|
|---|
| 233 | round4:procedure expose m. c1111 c0 c1
|
|---|
| 234 | parse arg a1,b1,c1,d1,kth,shift,sini
|
|---|
| 235 | kth=kth+1
|
|---|
| 236 | t1=c2d(a1)+c2d(i(b1,c1,d1))+ c2d(m.kth) + sini
|
|---|
| 237 | t1a=right(d2c(t1),4,c0)
|
|---|
| 238 | t2=rotleft(t1a,shift)
|
|---|
| 239 | t3=m32add(t2,b1)
|
|---|
| 240 | return t3
|
|---|
| 241 |
|
|---|
| 242 | /* add to "char" numbers, modulo 2**32, return as char */
|
|---|
| 243 | m32add:procedure expose c0 c1 c1111
|
|---|
| 244 | parse arg v1,v2
|
|---|
| 245 | t1=c2d(v1)+c2d(v2)
|
|---|
| 246 | t2=d2c(t1)
|
|---|
| 247 | t3=right(t2,4,c0)
|
|---|
| 248 | return t3
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 | /*********** Basic functions */
|
|---|
| 253 | /* F(x, y, z) == (((x) & (y)) | ((~x) & (z))) */
|
|---|
| 254 | f:procedure expose c0 c1 c1111
|
|---|
| 255 | parse arg x,y,z
|
|---|
| 256 | t1=bitand(x,y)
|
|---|
| 257 | notx=bitxor(x,c1111)
|
|---|
| 258 | t2=bitand(notx,z)
|
|---|
| 259 | return bitor(t1,t2)
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 | /* G(x, y, z) == (((x) & (z)) | ((y) & (~z)))*/
|
|---|
| 263 | g:procedure expose c0 c1 c1111
|
|---|
| 264 | parse arg x,y,z
|
|---|
| 265 | t1=bitand(x,z)
|
|---|
| 266 | notz=bitxor(z,c1111)
|
|---|
| 267 | t2=bitand(y,notz)
|
|---|
| 268 | return bitor(t1,t2)
|
|---|
| 269 |
|
|---|
| 270 | /* H(x, y, z) == ((x) ^ (y) ^ (z)) */
|
|---|
| 271 | h:procedure expose c0 c1 c1111
|
|---|
| 272 | parse arg x,y,z
|
|---|
| 273 | t1=bitxor(x,y)
|
|---|
| 274 | return bitxor(t1,z)
|
|---|
| 275 |
|
|---|
| 276 | /* I(x, y, z) == ((y) ^ ((x) | (~z))) */
|
|---|
| 277 | i:procedure expose c0 c1 c1111
|
|---|
| 278 | parse arg x,y,z
|
|---|
| 279 | notz=bitxor(z,c1111)
|
|---|
| 280 | t2=bitor(x,notz)
|
|---|
| 281 | return bitxor(y,t2)
|
|---|
| 282 |
|
|---|
| 283 | /* bit rotate to the left by s positions */
|
|---|
| 284 | rotleft:procedure
|
|---|
| 285 | parse arg achar,s
|
|---|
| 286 | if s=0 then return achar
|
|---|
| 287 |
|
|---|
| 288 | bits=x2b(c2x(achar))
|
|---|
| 289 | lb=length(bits)
|
|---|
| 290 | t1=left(bits,s)
|
|---|
| 291 | t2=bits||t1
|
|---|
| 292 | yib=right(t2,lb)
|
|---|
| 293 | return x2c(b2x(yib))
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|