md5.tcl

Go to the documentation of this file.
00001 /* */
00002 /* */
00003 /*  md5.tcl - MD5 in Tcl*/
00004 /*  Author: Don Libes <libes@nist.gov>, July 1999*/
00005 /*  Version 1.2.0*/
00006 /* */
00007 /*  MD5  defined by RFC 1321, "The MD5 Message-Digest Algorithm"*/
00008 /*  HMAC defined by RFC 2104, "Keyed-Hashing for Message Authentication"*/
00009 /* */
00010 /*  Most of the comments below come right out of RFC 1321; That's why*/
00011 /*  they have such peculiar numbers.  In addition, I have retained*/
00012 /*  original syntax, bugs in documentation (yes, really), etc. from the*/
00013 /*  RFC.  All remaining bugs are mine.*/
00014 /* */
00015 /*  HMAC implementation by D. J. Hagberg <dhagberg@millibits.com> and*/
00016 /*  is based on C code in RFC 2104.*/
00017 /* */
00018 /*  For more info, see: http://expect.nist.gov/md5pure*/
00019 /* */
00020 /*  - Don*/
00021 /* */
00022 /*  Modified by Miguel Sofer to use inlines and simple variables*/
00023 /* */
00024 
00025 /*  @mdgen EXCLUDE: md5c.tcl*/
00026 
00027 package require Tcl 8.2
00028 namespace ::md5 {
00029 }
00030 
00031 if {![catch {package require Trf 2.0}] && ![catch {::md5 -- test}]} {
00032     /*  Trf is available, so implement the functionality provided here*/
00033     /*  in terms of calls to Trf for speed.*/
00034 
00035     ret  ::md5::md5 (type msg) {
00036     string tolower [::hex -mode encode -- [::md5 -- $msg]]
00037     }
00038 
00039     /*  hmac: hash for message authentication*/
00040 
00041     /*  MD5 of Trf and MD5 as defined by this package have slightly*/
00042     /*  different results. Trf returns the digest in binary, here we get*/
00043     /*  it as hex-string. In the computation of the HMAC the latter*/
00044     /*  requires back conversion into binary in some places. With Trf we*/
00045     /*  can use omit these.*/
00046 
00047     ret  ::md5::hmac (type key , type text) {
00048     # if key is longer than 64 bytes, reset it to MD5(key).  If shorter, 
00049     # pad it out with null (\x00) chars.
00050     set keyLen [string length $key]
00051     if {$keyLen > 64} {
00052         #old: set key [binary format H32 [md5 $key]]
00053         set key [::md5 -- $key]
00054         set keyLen [string length $key]
00055     }
00056     
00057     # ensure the key is padded out to 64 chars with nulls.
00058     set padLen [expr {64 - $keyLen}]
00059     append key [binary format "a$padLen" {}]
00060 
00061     # Split apart the key into a list of 16 little-endian words
00062     binary scan $key i16 blocks
00063 
00064     # XOR key with ipad and opad values
00065     set k_ipad {}
00066     set k_opad {}
00067     foreach i $blocks {
00068         append k_ipad [binary format i [expr {$i ^ 0x36363636}]]
00069         append k_opad [binary format i [expr {$i ^ 0x5c5c5c5c}]]
00070     }
00071     
00072     # Perform inner md5, appending its results to the outer key
00073     append k_ipad $text
00074     #old: append k_opad [binary format H* [md5 $k_ipad]]
00075     append k_opad [::md5 -- $k_ipad]
00076 
00077     # Perform outer md5
00078     #old: md5 $k_opad
00079     string tolower [::hex -mode encode -- [::md5 -- $k_opad]]
00080     }
00081 
00082 } else {
00083     /*  Without Trf use the all-tcl implementation by Don Libes.*/
00084 
00085     /*  T will be inlined after the definition of md5body*/
00086 
00087     /*  test md5*/
00088     /* */
00089     /*  This proc is not necessary during runtime and may be omitted if you*/
00090     /*  are simply inserting this file into a production program.*/
00091     /* */
00092     ret  ::md5::test () {
00093     foreach {msg expected} {
00094         ""
00095         "d41d8cd98f00b204e9800998ecf8427e"
00096         "a"
00097         "0cc175b9c0f1b6a831c399e269772661"
00098         "abc"
00099         "900150983cd24fb0d6963f7d28e17f72"
00100         "message digest"
00101         "f96b697d7cb7938d525a2f31aaf161d0"
00102         "abcdefghijklmnopqrstuvwxyz"
00103         "c3fcd3d76192e4007dfb496cca67e13b"
00104         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
00105         "d174ab98d277d9f5a5611c2c9f419d9f"
00106         "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
00107         "57edf4a22be3c955ac49da2e2107b67a"
00108     } {
00109         puts "testing: md5 \"$msg\""
00110         set computed [md5 $msg]
00111         puts "expected: $expected"
00112         puts "computed: $computed"
00113         if {0 != [string compare $computed $expected]} {
00114         puts "FAILED"
00115         } else {
00116         puts "SUCCEEDED"
00117         }
00118     }
00119     }
00120 
00121     /*  time md5*/
00122     /* */
00123     /*  This proc is not necessary during runtime and may be omitted if you*/
00124     /*  are simply inserting this file into a production program.*/
00125     /* */
00126     ret  ::md5::time () {
00127     foreach len {10 50 100 500 1000 5000 10000} {
00128         set time [::time {md5 [format %$len.0s ""]} 100]
00129         set msec [lindex $time 0]
00130         puts "input length $len: [expr {$msec/1000}] milliseconds per interation"
00131     }
00132     }
00133 
00134     /* */
00135     /*  We just define the body of md5pure::md5 here; later we*/
00136     /*  regsub to inline a few function calls for speed*/
00137     /* */
00138 
00139      ::md5 = ::md5body {
00140 
00141     /* */
00142     /*  3.1 Step 1. Append Padding Bits*/
00143     /* */
00144 
00145      msgLen =  [string length $msg]
00146 
00147      padLen =  [expr {56 - $msgLen%64}]
00148     if {$msgLen % 64 > 56} {
00149         incr padLen 64
00150     }
00151 
00152     /*  pad even if no padding required*/
00153     if {$padLen == 0} {
00154         incr padLen 64
00155     }
00156 
00157     /*  append single 1b followed by 0b's*/
00158     append msg [binary format "a$padLen" \200]
00159 
00160     /* */
00161     /*  3.2 Step 2. Append Length*/
00162     /* */
00163 
00164     /*  RFC doesn't say whether to use little- or big-endian*/
00165     /*  code demonstrates little-endian*/
00166     /*  This step limits our input to size 2^32b or 2^24B*/
00167     append msg [binary format "i1i1" [expr {8*$msgLen}] 0]
00168     
00169     /* */
00170     /*  3.3 Step 3. Initialize MD Buffer*/
00171     /* */
00172 
00173      A =  [expr 0x67452301]
00174      B =  [expr 0xefcdab89]
00175      C =  [expr 0x98badcfe]
00176      D =  [expr 0x10325476]
00177 
00178     /* */
00179     /*  3.4 Step 4. Process Message in 16-Word Blocks*/
00180     /* */
00181 
00182     /*  process each 16-word block*/
00183     /*  RFC doesn't say whether to use little- or big-endian*/
00184     /*  code says little-endian*/
00185     binary scan $msg i* blocks
00186 
00187     /*  loop over the message taking 16 blocks at a time*/
00188 
00189     foreach {X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15} $blocks {
00190 
00191         /*  Save A as AA, B as BB, C as CC, and D as DD.*/
00192          AA =  $A
00193          BB =  $B
00194          CC =  $C
00195          DD =  $D
00196 
00197         /*  Round 1.*/
00198         /*  Let [abcd k s i] denote the operation*/
00199         /*       a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s).*/
00200         /*  [ABCD  0  7  1]  [DABC  1 12  2]  [CDAB  2 17  3]  [BCDA  3 22  4]*/
00201          A =  [expr {$B + [<<< [expr {$A + [F $B $C $D] + $X0  + $T01}]  7]}]
00202          D =  [expr {$A + [<<< [expr {$D + [F $A $B $C] + $X1  + $T02}] 12]}]
00203          C =  [expr {$D + [<<< [expr {$C + [F $D $A $B] + $X2  + $T03}] 17]}]
00204          B =  [expr {$C + [<<< [expr {$B + [F $C $D $A] + $X3  + $T04}] 22]}]
00205         /*  [ABCD  4  7  5]  [DABC  5 12  6]  [CDAB  6 17  7]  [BCDA  7 22  8]*/
00206          A =  [expr {$B + [<<< [expr {$A + [F $B $C $D] + $X4  + $T05}]  7]}]
00207          D =  [expr {$A + [<<< [expr {$D + [F $A $B $C] + $X5  + $T06}] 12]}]
00208          C =  [expr {$D + [<<< [expr {$C + [F $D $A $B] + $X6  + $T07}] 17]}]
00209          B =  [expr {$C + [<<< [expr {$B + [F $C $D $A] + $X7  + $T08}] 22]}]
00210         /*  [ABCD  8  7  9]  [DABC  9 12 10]  [CDAB 10 17 11]  [BCDA 11 22 12]*/
00211          A =  [expr {$B + [<<< [expr {$A + [F $B $C $D] + $X8  + $T09}]  7]}]
00212          D =  [expr {$A + [<<< [expr {$D + [F $A $B $C] + $X9  + $T10}] 12]}]
00213          C =  [expr {$D + [<<< [expr {$C + [F $D $A $B] + $X10 + $T11}] 17]}]
00214          B =  [expr {$C + [<<< [expr {$B + [F $C $D $A] + $X11 + $T12}] 22]}]
00215         /*  [ABCD 12  7 13]  [DABC 13 12 14]  [CDAB 14 17 15]  [BCDA 15 22 16]*/
00216          A =  [expr {$B + [<<< [expr {$A + [F $B $C $D] + $X12 + $T13}]  7]}]
00217          D =  [expr {$A + [<<< [expr {$D + [F $A $B $C] + $X13 + $T14}] 12]}]
00218          C =  [expr {$D + [<<< [expr {$C + [F $D $A $B] + $X14 + $T15}] 17]}]
00219          B =  [expr {$C + [<<< [expr {$B + [F $C $D $A] + $X15 + $T16}] 22]}]
00220 
00221         /*  Round 2.*/
00222         /*  Let [abcd k s i] denote the operation*/
00223         /*       a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s).*/
00224         /*  Do the following 16 operations.*/
00225         /*  [ABCD  1  5 17]  [DABC  6  9 18]  [CDAB 11 14 19]  [BCDA  0 20 20]*/
00226          A =  [expr {$B + [<<< [expr {$A + [G $B $C $D] + $X1  + $T17}]  5]}]
00227          D =  [expr {$A + [<<< [expr {$D + [G $A $B $C] + $X6  + $T18}]  9]}]
00228          C =  [expr {$D + [<<< [expr {$C + [G $D $A $B] + $X11 + $T19}] 14]}]
00229          B =  [expr {$C + [<<< [expr {$B + [G $C $D $A] + $X0  + $T20}] 20]}]
00230         /*  [ABCD  5  5 21]  [DABC 10  9 22]  [CDAB 15 14 23]  [BCDA  4 20 24]*/
00231          A =  [expr {$B + [<<< [expr {$A + [G $B $C $D] + $X5  + $T21}]  5]}]
00232          D =  [expr {$A + [<<< [expr {$D + [G $A $B $C] + $X10 + $T22}]  9]}]
00233          C =  [expr {$D + [<<< [expr {$C + [G $D $A $B] + $X15 + $T23}] 14]}]
00234          B =  [expr {$C + [<<< [expr {$B + [G $C $D $A] + $X4  + $T24}] 20]}]
00235         /*  [ABCD  9  5 25]  [DABC 14  9 26]  [CDAB  3 14 27]  [BCDA  8 20 28]*/
00236          A =  [expr {$B + [<<< [expr {$A + [G $B $C $D] + $X9  + $T25}]  5]}]
00237          D =  [expr {$A + [<<< [expr {$D + [G $A $B $C] + $X14 + $T26}]  9]}]
00238          C =  [expr {$D + [<<< [expr {$C + [G $D $A $B] + $X3  + $T27}] 14]}]
00239          B =  [expr {$C + [<<< [expr {$B + [G $C $D $A] + $X8  + $T28}] 20]}]
00240         /*  [ABCD 13  5 29]  [DABC  2  9 30]  [CDAB  7 14 31]  [BCDA 12 20 32]*/
00241          A =  [expr {$B + [<<< [expr {$A + [G $B $C $D] + $X13 + $T29}]  5]}]
00242          D =  [expr {$A + [<<< [expr {$D + [G $A $B $C] + $X2  + $T30}]  9]}]
00243          C =  [expr {$D + [<<< [expr {$C + [G $D $A $B] + $X7  + $T31}] 14]}]
00244          B =  [expr {$C + [<<< [expr {$B + [G $C $D $A] + $X12 + $T32}] 20]}]
00245 
00246         /*  Round 3.*/
00247         /*  Let [abcd k s t] [sic] denote the operation*/
00248         /*      a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s).*/
00249         /*  Do the following 16 operations.*/
00250         /*  [ABCD  5  4 33]  [DABC  8 11 34]  [CDAB 11 16 35]  [BCDA 14 23 36]*/
00251          A =  [expr {$B + [<<< [expr {$A + [H $B $C $D] + $X5  + $T33}]  4]}]
00252          D =  [expr {$A + [<<< [expr {$D + [H $A $B $C] + $X8  + $T34}] 11]}]
00253          C =  [expr {$D + [<<< [expr {$C + [H $D $A $B] + $X11 + $T35}] 16]}]
00254          B =  [expr {$C + [<<< [expr {$B + [H $C $D $A] + $X14 + $T36}] 23]}]
00255         /*  [ABCD  1  4 37]  [DABC  4 11 38]  [CDAB  7 16 39]  [BCDA 10 23 40]*/
00256          A =  [expr {$B + [<<< [expr {$A + [H $B $C $D] + $X1  + $T37}]  4]}]
00257          D =  [expr {$A + [<<< [expr {$D + [H $A $B $C] + $X4  + $T38}] 11]}]
00258          C =  [expr {$D + [<<< [expr {$C + [H $D $A $B] + $X7  + $T39}] 16]}]
00259          B =  [expr {$C + [<<< [expr {$B + [H $C $D $A] + $X10 + $T40}] 23]}]
00260         /*  [ABCD 13  4 41]  [DABC  0 11 42]  [CDAB  3 16 43]  [BCDA  6 23 44]*/
00261          A =  [expr {$B + [<<< [expr {$A + [H $B $C $D] + $X13 + $T41}]  4]}]
00262          D =  [expr {$A + [<<< [expr {$D + [H $A $B $C] + $X0  + $T42}] 11]}]
00263          C =  [expr {$D + [<<< [expr {$C + [H $D $A $B] + $X3  + $T43}] 16]}]
00264          B =  [expr {$C + [<<< [expr {$B + [H $C $D $A] + $X6  + $T44}] 23]}]
00265         /*  [ABCD  9  4 45]  [DABC 12 11 46]  [CDAB 15 16 47]  [BCDA  2 23 48]*/
00266          A =  [expr {$B + [<<< [expr {$A + [H $B $C $D] + $X9  + $T45}]  4]}]
00267          D =  [expr {$A + [<<< [expr {$D + [H $A $B $C] + $X12 + $T46}] 11]}]
00268          C =  [expr {$D + [<<< [expr {$C + [H $D $A $B] + $X15 + $T47}] 16]}]
00269          B =  [expr {$C + [<<< [expr {$B + [H $C $D $A] + $X2  + $T48}] 23]}]
00270 
00271         /*  Round 4.*/
00272         /*  Let [abcd k s t] [sic] denote the operation*/
00273         /*      a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s).*/
00274         /*  Do the following 16 operations.*/
00275         /*  [ABCD  0  6 49]  [DABC  7 10 50]  [CDAB 14 15 51]  [BCDA  5 21 52]*/
00276          A =  [expr {$B + [<<< [expr {$A + [I $B $C $D] + $X0  + $T49}]  6]}]
00277          D =  [expr {$A + [<<< [expr {$D + [I $A $B $C] + $X7  + $T50}] 10]}]
00278          C =  [expr {$D + [<<< [expr {$C + [I $D $A $B] + $X14 + $T51}] 15]}]
00279          B =  [expr {$C + [<<< [expr {$B + [I $C $D $A] + $X5  + $T52}] 21]}]
00280         /*  [ABCD 12  6 53]  [DABC  3 10 54]  [CDAB 10 15 55]  [BCDA  1 21 56]*/
00281          A =  [expr {$B + [<<< [expr {$A + [I $B $C $D] + $X12 + $T53}]  6]}]
00282          D =  [expr {$A + [<<< [expr {$D + [I $A $B $C] + $X3  + $T54}] 10]}]
00283          C =  [expr {$D + [<<< [expr {$C + [I $D $A $B] + $X10 + $T55}] 15]}]
00284          B =  [expr {$C + [<<< [expr {$B + [I $C $D $A] + $X1  + $T56}] 21]}]
00285         /*  [ABCD  8  6 57]  [DABC 15 10 58]  [CDAB  6 15 59]  [BCDA 13 21 60]*/
00286          A =  [expr {$B + [<<< [expr {$A + [I $B $C $D] + $X8  + $T57}]  6]}]
00287          D =  [expr {$A + [<<< [expr {$D + [I $A $B $C] + $X15 + $T58}] 10]}]
00288          C =  [expr {$D + [<<< [expr {$C + [I $D $A $B] + $X6  + $T59}] 15]}]
00289          B =  [expr {$C + [<<< [expr {$B + [I $C $D $A] + $X13 + $T60}] 21]}]
00290         /*  [ABCD  4  6 61]  [DABC 11 10 62]  [CDAB  2 15 63]  [BCDA  9 21 64]*/
00291          A =  [expr {$B + [<<< [expr {$A + [I $B $C $D] + $X4  + $T61}]  6]}]
00292          D =  [expr {$A + [<<< [expr {$D + [I $A $B $C] + $X11 + $T62}] 10]}]
00293          C =  [expr {$D + [<<< [expr {$C + [I $D $A $B] + $X2  + $T63}] 15]}]
00294          B =  [expr {$C + [<<< [expr {$B + [I $C $D $A] + $X9  + $T64}] 21]}]
00295 
00296         /*  Then perform the following additions. (That is increment each*/
00297         /*    of the four registers by the value it had before this block*/
00298         /*    was started.)*/
00299         incr A $AA
00300         incr B $BB
00301         incr C $CC
00302         incr D $DD
00303     }
00304     /*  3.5 Step 5. Output*/
00305 
00306     /*  ... begin with the low-order byte of A, and end with the high-order byte*/
00307     /*  of D.*/
00308 
00309     return [bytes $A][bytes $B][bytes $C][bytes $D]
00310     }
00311 
00312     /* */
00313     /*  Here we inline/regsub the functions F, G, H, I and <<< */
00314     /* */
00315 
00316     namespace ::md5 {
00317     /* proc md5pure::F {x y z} {expr {(($x & $y) | ((~$x) & $z))}}*/
00318     regsub -all -- {\[ *F +(\$.) +(\$.) +(\$.) *\]} $md5body {((\1 \& \2) | ((~\1) \& \3))} md5body
00319 
00320     /* proc md5pure::G {x y z} {expr {(($x & $z) | ($y & (~$z)))}}*/
00321     regsub -all -- {\[ *G +(\$.) +(\$.) +(\$.) *\]} $md5body {((\1 \& \3) | (\2 \& (~\3)))} md5body
00322 
00323     /* proc md5pure::H {x y z} {expr {$x ^ $y ^ $z}}*/
00324     regsub -all -- {\[ *H +(\$.) +(\$.) +(\$.) *\]} $md5body {(\1 ^ \2 ^ \3)} md5body
00325 
00326     /* proc md5pure::I {x y z} {expr {$y ^ ($x | (~$z))}}*/
00327     regsub -all -- {\[ *I +(\$.) +(\$.) +(\$.) *\]} $md5body {(\2 ^ (\1 | (~\3)))} md5body
00328 
00329     /*  bitwise left-rotate*/
00330     if {0} {
00331         ret  md5pure::<<< (type x , type i) {
00332         # This works by bitwise-ORing together right piece and left
00333         # piece so that the (original) right piece becomes the left
00334         # piece and vice versa.
00335         #
00336         # The (original) right piece is a simple left shift.
00337         # The (original) left piece should be a simple right shift
00338         # but Tcl does sign extension on right shifts so we
00339         # shift it 1 bit, mask off the sign, and finally shift
00340         # it the rest of the way.
00341         
00342         # expr {($x << $i) | ((($x >> 1) & 0x7fffffff) >> (31-$i))}
00343 
00344         #
00345         # New version, faster when inlining
00346         # We replace inline (computing at compile time):
00347         #   R$i -> (32 - $i)
00348         #   S$i -> (0x7fffffff >> (31-$i))
00349         #
00350 
00351         expr { ($x << $i) | (($x >> [set R$i]) & [set S$i])}
00352         }
00353     }
00354     /*  inline <<<*/
00355     regsub -all -- {\[ *<<< +\[ *expr +({[^\}]*})\] +([0-9]+) *\]} $md5body {(([ x =  [expr \1]] << \2) |  (($x >> R\2) \& S\2))} md5body
00356 
00357     /*  now replace the R and S*/
00358      map =  {}
00359     foreach i { 
00360         7 12 17 22
00361         5  9 14 20
00362         4 11 16 23
00363         6 10 15 21 
00364     } {
00365         lappend map R$i [expr {32 - $i}] S$i [expr {0x7fffffff >> (31-$i)}]
00366     }
00367     
00368     /*  inline the values of T*/
00369     foreach \
00370         tName {
00371         T01 T02 T03 T04 T05 T06 T07 T08 T09 T10 
00372         T11 T12 T13 T14 T15 T16 T17 T18 T19 T20 
00373         T21 T22 T23 T24 T25 T26 T27 T28 T29 T30 
00374         T31 T32 T33 T34 T35 T36 T37 T38 T39 T40 
00375         T41 T42 T43 T44 T45 T46 T47 T48 T49 T50 
00376         T51 T52 T53 T54 T55 T56 T57 T58 T59 T60 
00377         T61 T62 T63 T64 } \
00378         tVal {
00379         0xd76aa478 0xe8c7b756 0x242070db 0xc1bdceee
00380         0xf57c0faf 0x4787c62a 0xa8304613 0xfd469501
00381         0x698098d8 0x8b44f7af 0xffff5bb1 0x895cd7be
00382         0x6b901122 0xfd987193 0xa679438e 0x49b40821
00383 
00384         0xf61e2562 0xc040b340 0x265e5a51 0xe9b6c7aa
00385         0xd62f105d 0x2441453  0xd8a1e681 0xe7d3fbc8
00386         0x21e1cde6 0xc33707d6 0xf4d50d87 0x455a14ed
00387         0xa9e3e905 0xfcefa3f8 0x676f02d9 0x8d2a4c8a
00388 
00389         0xfffa3942 0x8771f681 0x6d9d6122 0xfde5380c
00390         0xa4beea44 0x4bdecfa9 0xf6bb4b60 0xbebfbc70
00391         0x289b7ec6 0xeaa127fa 0xd4ef3085 0x4881d05
00392         0xd9d4d039 0xe6db99e5 0x1fa27cf8 0xc4ac5665
00393 
00394         0xf4292244 0x432aff97 0xab9423a7 0xfc93a039
00395         0x655b59c3 0x8f0ccc92 0xffeff47d 0x85845dd1
00396         0x6fa87e4f 0xfe2ce6e0 0xa3014314 0x4e0811a1
00397         0xf7537e82 0xbd3af235 0x2ad7d2bb 0xeb86d391
00398     } {
00399         lappend map \$$tName $tVal
00400     }
00401      md5body =  [string map $map $md5body]
00402     
00403 
00404     /*  Finally, define the proc*/
00405     ret  md5 (type msg) $md5body
00406 
00407     # unset auxiliary variables
00408     unset md5body tName tVal map
00409     }
00410 
00411     proc ::md5::byte0 {i} {expr {0xff & $i}}
00412     ret  ::md5::byte1 (type i) {expr {(0xff00 & $i) >> 8}}
00413     ret  ::md5::byte2 (type i) {expr {(0xff0000 & $i) >> 16}}
00414     ret  ::md5::byte3 (type i) {expr {((0xff000000 & $i) >> 24) & 0xff}}
00415 
00416     ret  ::md5::bytes (type i) {
00417     format %0.2x%0.2x%0.2x%0.2x [byte0 $i] [byte1 $i] [byte2 $i] [byte3 $i]
00418     }
00419 
00420     /*  hmac: hash for message authentication*/
00421     ret  ::md5::hmac (type key , type text) {
00422     # if key is longer than 64 bytes, reset it to MD5(key).  If shorter, 
00423     # pad it out with null (\x00) chars.
00424     set keyLen [string length $key]
00425     if {$keyLen > 64} {
00426         set key [binary format H32 [md5 $key]]
00427         set keyLen [string length $key]
00428     }
00429 
00430     # ensure the key is padded out to 64 chars with nulls.
00431     set padLen [expr {64 - $keyLen}]
00432     append key [binary format "a$padLen" {}]
00433     
00434     # Split apart the key into a list of 16 little-endian words
00435     binary scan $key i16 blocks
00436 
00437     # XOR key with ipad and opad values
00438     set k_ipad {}
00439     set k_opad {}
00440     foreach i $blocks {
00441         append k_ipad [binary format i [expr {$i ^ 0x36363636}]]
00442         append k_opad [binary format i [expr {$i ^ 0x5c5c5c5c}]]
00443     }
00444     
00445     # Perform inner md5, appending its results to the outer key
00446     append k_ipad $text
00447     append k_opad [binary format H* [md5 $k_ipad]]
00448 
00449     # Perform outer md5
00450     md5 $k_opad
00451     }
00452 }
00453 
00454 package provide md5 1.4.4
00455 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1