您的位置:程序门 -> vb ->



有个md5的类在检测文件时比较慢懂这方面的高手帮修改下


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


有个md5的类在检测文件时比较慢懂这方面的高手帮修改下
发表于:2007-01-10 19:57:26 楼主
如果bytebuffer值能大些比如65535这样速度就快多了。但是发现如果这样md5值就不唯一了。我也试着修改了下还是不行。只有请研究这方面高手修改下。
option   explicit
private   const   offset_4   =   4294967296#
private   const   maxint_4   =   2147483647
private   state(4)   as   long
private   bytecounter   as   long
private   bytebuffer(63)   as   byte
private   const   s11   =   7
private   const   s12   =   12
private   const   s13   =   17
private   const   s14   =   22
private   const   s21   =   5
private   const   s22   =   9
private   const   s23   =   14
private   const   s24   =   20
private   const   s31   =   4
private   const   s32   =   11
private   const   s33   =   16
private   const   s34   =   23
private   const   s41   =   6
private   const   s42   =   10
private   const   s43   =   15
private   const   s44   =   21

property   get   registera()   as   string
        registera   =   state(1)
end   property

property   get   registerb()   as   string
        registerb   =   state(2)
end   property

property   get   registerc()   as   string
        registerc   =   state(3)
end   property

property   get   registerd()   as   string
        registerd   =   state(4)
end   property

public   function   md5_string_calc(sourcestring   as   string)   as   string
        md5init
        md5update   lenb(strconv(sourcestring,   vbfromunicode)),   stringtoarray(sourcestring)
        md5final
        md5_string_calc   =   getvalues
end   function

public   function   md5_file_calc(infile   as   string)   as   string
        on   error   goto   errorhandler
        dim   fileo   as   integer
        fileo   =   freefile
        call   filelen(infile)
        open   infile   for   binary   access   read   as   #fileo
        md5init
        do   while   not   eof(fileo)
                doevents
                get   #fileo,   ,   bytebuffer
                if   loc(fileo)   <   lof(fileo)   then
                        bytecounter   =   bytecounter   +   64
                        md5transform   bytebuffer
                end   if
        loop
        bytecounter   =   bytecounter   +   (lof(fileo)   mod   64)
        close   #fileo
        md5final
        md5_file_calc   =   getvalues
        exit   function
errorhandler:
        md5_file_calc   =   " "
end   function

private   function   stringtoarray(instring   as   string)   as   byte()
        dim   i   as   integer,   bytbuffer()   as   byte
        redim   bytbuffer(lenb(strconv(instring,   vbfromunicode)))
        bytbuffer   =   strconv(instring,   vbfromunicode)
        stringtoarray   =   bytbuffer
end   function

public   function   getvalues()   as   string
        getvalues   =   longtostring(state(1))   &   longtostring(state(2))   &   longtostring(state(3))   &   longtostring(state(4))
end   function

private   function   longtostring(num   as   long)   as   string
                dim   a   as   byte,   b   as   byte,   c   as   byte,   d   as   byte
                a   =   num   and   &hff&
                if   a   <   16   then   longtostring   =   "0 "   &   hex(a)   else   longtostring   =   hex(a)
                b   =   (num   and   &hff00&)   \   256
                if   b   <   16   then   longtostring   =   longtostring   &   "0 "   &   hex(b)   else   longtostring   =   longtostring   &   hex(b)
                c   =   (num   and   &hff0000)   \   65536
                if   c   <   16   then   longtostring   =   longtostring   &   "0 "   &   hex(c)   else   longtostring   =   longtostring   &   hex(c)
                if   num   <   0   then   d   =   ((num   and   &h7f000000)   \   16777216)   or   &h80&   else   d   =   (num   and   &hff000000)   \   16777216
                if   d   <   16   then   longtostring   =   longtostring   &   "0 "   &   hex(d)   else   longtostring   =   longtostring   &   hex(d)
end   function

public   sub   md5init()
        bytecounter   =   0
        state(1)   =   unsignedtolong(1732584193#)
        state(2)   =   unsignedtolong(4023233417#)
        state(3)   =   unsignedtolong(2562383102#)
        state(4)   =   unsignedtolong(271733878#)
end   sub

public   sub   md5final()
        dim   dblbits   as   double,   padding(72)   as   byte,   lngbytesbuffered   as   long
        padding(0)   =   &h80
        dblbits   =   bytecounter   *   8
        lngbytesbuffered   =   bytecounter   mod   64
        if   lngbytesbuffered   <=   56   then   md5update   56   -   lngbytesbuffered,   padding   else   md5update   120   -   bytecounter,   padding
        padding(0)   =   unsignedtolong(dblbits)   and   &hff&
        padding(1)   =   unsignedtolong(dblbits)   \   256   and   &hff&
        padding(2)   =   unsignedtolong(dblbits)   \   65536   and   &hff&
        padding(3)   =   unsignedtolong(dblbits)   \   16777216   and   &hff&
        padding(4)   =   0
        padding(5)   =   0
        padding(6)   =   0
        padding(7)   =   0
        md5update   8,   padding
end   sub

public   sub   md5update(inputlen   as   long,   inputbuffer()   as   byte)
        dim   ii   as   integer,   i   as   integer,   j   as   integer,   k   as   integer,   lngbufferedbytes   as   long,   lngbufferremaining   as   long,   lngrem   as   long

        lngbufferedbytes   =   bytecounter   mod   64
        lngbufferremaining   =   64   -   lngbufferedbytes
        bytecounter   =   bytecounter   +   inputlen

        if   inputlen   > =   lngbufferremaining   then
                for   ii   =   0   to   lngbufferremaining   -   1
                        bytebuffer(lngbufferedbytes   +   ii)   =   inputbuffer(ii)
                next   ii
                md5transform   bytebuffer
                lngrem   =   (inputlen)   mod   64
                for   i   =   lngbufferremaining   to   inputlen   -   ii   -   lngrem   step   64
                        for   j   =   0   to   63
                                bytebuffer(j)   =   inputbuffer(i   +   j)
                        next   j
                        md5transform   bytebuffer
                next   i
                lngbufferedbytes   =   0
        else
            i   =   0
        end   if
        for   k   =   0   to   inputlen   -   i   -   1
                bytebuffer(lngbufferedbytes   +   k)   =   inputbuffer(i   +   k)
        next   k
end   sub

发表于:2007-01-10 19:57:321楼 得分:0
private   sub   md5transform(buffer()   as   byte)
        dim   x(16)   as   long,   a   as   long,   b   as   long,   c   as   long,   d   as   long
       
        a   =   state(1)
        b   =   state(2)
        c   =   state(3)
        d   =   state(4)
        decode   64,   x,   buffer
        ff   a,   b,   c,   d,   x(0),   s11,   -680876936
        ff   d,   a,   b,   c,   x(1),   s12,   -389564586
        ff   c,   d,   a,   b,   x(2),   s13,   606105819
        ff   b,   c,   d,   a,   x(3),   s14,   -1044525330
        ff   a,   b,   c,   d,   x(4),   s11,   -176418897
        ff   d,   a,   b,   c,   x(5),   s12,   1200080426
        ff   c,   d,   a,   b,   x(6),   s13,   -1473231341
        ff   b,   c,   d,   a,   x(7),   s14,   -45705983
        ff   a,   b,   c,   d,   x(8),   s11,   1770035416
        ff   d,   a,   b,   c,   x(9),   s12,   -1958414417
        ff   c,   d,   a,   b,   x(10),   s13,   -42063
        ff   b,   c,   d,   a,   x(11),   s14,   -1990404162
        ff   a,   b,   c,   d,   x(12),   s11,   1804603682
        ff   d,   a,   b,   c,   x(13),   s12,   -40341101
        ff   c,   d,   a,   b,   x(14),   s13,   -1502002290
        ff   b,   c,   d,   a,   x(15),   s14,   1236535329

        gg   a,   b,   c,   d,   x(1),   s21,   -165796510
        gg   d,   a,   b,   c,   x(6),   s22,   -1069501632
        gg   c,   d,   a,   b,   x(11),   s23,   643717713
        gg   b,   c,   d,   a,   x(0),   s24,   -373897302
        gg   a,   b,   c,   d,   x(5),   s21,   -701558691
        gg   d,   a,   b,   c,   x(10),   s22,   38016083
        gg   c,   d,   a,   b,   x(15),   s23,   -660478335
        gg   b,   c,   d,   a,   x(4),   s24,   -405537848
        gg   a,   b,   c,   d,   x(9),   s21,   568446438
        gg   d,   a,   b,   c,   x(14),   s22,   -1019803690
        gg   c,   d,   a,   b,   x(3),   s23,   -187363961
        gg   b,   c,   d,   a,   x(8),   s24,   1163531501
        gg   a,   b,   c,   d,   x(13),   s21,   -1444681467
        gg   d,   a,   b,   c,   x(2),   s22,   -51403784
        gg   c,   d,   a,   b,   x(7),   s23,   1735328473
        gg   b,   c,   d,   a,   x(12),   s24,   -1926607734

        hh   a,   b,   c,   d,   x(5),   s31,   -378558
        hh   d,   a,   b,   c,   x(8),   s32,   -2022574463
        hh   c,   d,   a,   b,   x(11),   s33,   1839030562
        hh   b,   c,   d,   a,   x(14),   s34,   -35309556
        hh   a,   b,   c,   d,   x(1),   s31,   -1530992060
        hh   d,   a,   b,   c,   x(4),   s32,   1272893353
        hh   c,   d,   a,   b,   x(7),   s33,   -155497632
        hh   b,   c,   d,   a,   x(10),   s34,   -1094730640
        hh   a,   b,   c,   d,   x(13),   s31,   681279174
        hh   d,   a,   b,   c,   x(0),   s32,   -358537222
        hh   c,   d,   a,   b,   x(3),   s33,   -722521979
        hh   b,   c,   d,   a,   x(6),   s34,   76029189
        hh   a,   b,   c,   d,   x(9),   s31,   -640364487
        hh   d,   a,   b,   c,   x(12),   s32,   -421815835
        hh   c,   d,   a,   b,   x(15),   s33,   530742520
        hh   b,   c,   d,   a,   x(2),   s34,   -995338651

        ii   a,   b,   c,   d,   x(0),   s41,   -198630844
        ii   d,   a,   b,   c,   x(7),   s42,   1126891415
        ii   c,   d,   a,   b,   x(14),   s43,   -1416354905
        ii   b,   c,   d,   a,   x(5),   s44,   -57434055
        ii   a,   b,   c,   d,   x(12),   s41,   1700485571
        ii   d,   a,   b,   c,   x(3),   s42,   -1894986606
        ii   c,   d,   a,   b,   x(10),   s43,   -1051523
        ii   b,   c,   d,   a,   x(1),   s44,   -2054922799
        ii   a,   b,   c,   d,   x(8),   s41,   1873313359
        ii   d,   a,   b,   c,   x(15),   s42,   -30611744
        ii   c,   d,   a,   b,   x(6),   s43,   -1560198380
        ii   b,   c,   d,   a,   x(13),   s44,   1309151649
        ii   a,   b,   c,   d,   x(4),   s41,   -145523070
        ii   d,   a,   b,   c,   x(11),   s42,   -1120210379
        ii   c,   d,   a,   b,   x(2),   s43,   718787259
        ii   b,   c,   d,   a,   x(9),   s44,   -343485551

        state(1)   =   longoverflowadd(state(1),   a)
        state(2)   =   longoverflowadd(state(2),   b)
        state(3)   =   longoverflowadd(state(3),   c)
        state(4)   =   longoverflowadd(state(4),   d)
end   sub

private   sub   decode(length   as   integer,   outputbuffer()   as   long,   inputbuffer()   as   byte)
        dim   intdblindex   as   integer,   intbyteindex   as   integer,   dblsum   as   double
        for   intbyteindex   =   0   to   length   -   1   step   4
                dblsum   =   inputbuffer(intbyteindex)   +   inputbuffer(intbyteindex   +   1)   *   256#   +   inputbuffer(intbyteindex   +   2)   *   65536#   +   inputbuffer(intbyteindex   +   3)   *   16777216#
                outputbuffer(intdblindex)   =   unsignedtolong(dblsum)
                intdblindex   =   intdblindex   +   1
        next   intbyteindex
end   sub

private   function   ff(a   as   long,   b   as   long,   c   as   long,   d   as   long,   x   as   long,   s   as   long,   ac   as   long)   as   long
        a   =   longoverflowadd4(a,   (b   and   c)   or   (not   (b)   and   d),   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

private   function   gg(a   as   long,   b   as   long,   c   as   long,   d   as   long,   x   as   long,   s   as   long,   ac   as   long)   as   long
        a   =   longoverflowadd4(a,   (b   and   d)   or   (c   and   not   (d)),   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

private   function   hh(a   as   long,   b   as   long,   c   as   long,   d   as   long,   x   as   long,   s   as   long,   ac   as   long)   as   long
        a   =   longoverflowadd4(a,   b   xor   c   xor   d,   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

private   function   ii(a   as   long,   b   as   long,   c   as   long,   d   as   long,   x   as   long,   s   as   long,   ac   as   long)   as   long
        a   =   longoverflowadd4(a,   c   xor   (b   or   not   (d)),   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

function   longleftrotate(value   as   long,   bits   as   long)   as   long
        dim   lngsign   as   long,   lngi   as   long
        bits   =   bits   mod   32
        if   bits   =   0   then   longleftrotate   =   value:   exit   function
        for   lngi   =   1   to   bits
                lngsign   =   value   and   &hc0000000
                value   =   (value   and   &h3fffffff)   *   2
                value   =   value   or   ((lngsign   <   0)   and   1)   or   (cbool(lngsign   and   &h40000000)   and   &h80000000)
        next
        longleftrotate   =   value
end   function

private   function   longoverflowadd(val1   as   long,   val2   as   long)   as   long
        dim   lnghighword   as   long,   lnglowword   as   long,   lngoverflow   as   long
        lnglowword   =   (val1   and   &hffff&)   +   (val2   and   &hffff&)
        lngoverflow   =   lnglowword   \   65536
        lnghighword   =   (((val1   and   &hffff0000)   \   65536)   +   ((val2   and   &hffff0000)   \   65536)   +   lngoverflow)   and   &hffff&
        longoverflowadd   =   unsignedtolong((lnghighword   *   65536#)   +   (lnglowword   and   &hffff&))
end   function

private   function   longoverflowadd4(val1   as   long,   val2   as   long,   val3   as   long,   val4   as   long)   as   long
        dim   lnghighword   as   long,   lnglowword   as   long,   lngoverflow   as   long
        lnglowword   =   (val1   and   &hffff&)   +   (val2   and   &hffff&)   +   (val3   and   &hffff&)   +   (val4   and   &hffff&)
        lngoverflow   =   lnglowword   \   65536
        lnghighword   =   (((val1   and   &hffff0000)   \   65536)   +   ((val2   and   &hffff0000)   \   65536)   +   ((val3   and   &hffff0000)   \   65536)   +   ((val4   and   &hffff0000)   \   65536)   +   lngoverflow)   and   &hffff&
        longoverflowadd4   =   unsignedtolong((lnghighword   *   65536#)   +   (lnglowword   and   &hffff&))
end   function

private   function   unsignedtolong(value   as   double)   as   long
        if   value   <   0   or   value   > =   offset_4   then   error   6
        if   value   <=   maxint_4   then   unsignedtolong   =   value   else   unsignedtolong   =   value   -   offset_4
end   function

private   function   longtounsigned(value   as   long)   as   double
        if   value   <   0   then   longtounsigned   =   value   +   offset_4   else   longtounsigned   =   value
end   function


发表于:2007-01-11 16:46:422楼 得分:0
自己定下
发表于:2007-01-12 16:47:353楼 得分:0
帮顶
发表于:2007-01-29 08:37:014楼 得分:0
自己再顶下
发表于:2007-01-30 20:07:305楼 得分:0
接着顶!


快速检索

最新资讯
热门点击