分享:用四十种语言分别写一个MD5算法 之17 Liberty BASIC语言MD5算法

分享:用四十种语言分别写一个MD5算法 之17 Liberty BASIC语言MD5算法

    text$="hello csdner"
    checkSum$="354658789799080trgfhgbbcf"
 
    print text$
    print checkSum$
 
    test$=md5$(text$)
 
    if test$=checkSum$ then
        print "passed"
        print test$
    else
        print "failed"
    end if
    end
 
    function md5$(text$)
 
        dim r(64)
        dim k(64)
        dim w(16)
        global two32
        two32=2^32
 
        'prepare the MD5 checksum table
        restore [perRoundShiftAmounts]
        for i=0 to 63
            read x
            r(i)=x
        next
 
        'prepare constants
        for i=0 to 63
            k(i) =  int(abs(sin(i+1)) * two32)
        next
 
        'initialise variables
        h0 = HEXDEC("67452301")
        h1 = HEXDEC("EFCDAB89")
        h2 = HEXDEC("98BADCFE")
        h3 = HEXDEC("10325476")
 
        'find num bits in message
        numbits=len(text$)*8
 
        'add bits "10000000"
        text$=text$+chr$(128)
 
        'add bits "00000000"
        while len(text$) mod 64 <> 56
            text$=text$+chr$(0)
        wend
 
        'add original length in bits
        text$=text$+dec2asc$(numbits)
 
        'MD5 rounds
        'process in 64 byte chunks 512bits
        for chunk = 1 to len(text$) step 64
            chunk$ = mid$(text$, chunk, 64)
            for word = 0 TO 15
                'invert byte order
                b0 = asc(mid$(chunk$, word*4+1, 1))
                b1 = asc(mid$(chunk$, word*4+2, 1))
                b2 = asc(mid$(chunk$, word*4+3, 1))
                b3 = asc(mid$(chunk$, word*4+4, 1))
                w(word) = ((b3*256+b2)*256+b1)*256+b0
            next word
            a = h0
            b = h1
            c = h2
            d = h3
            for i = 0 to 63
                select case
                    case 0 <= i and i <= 15
                        f = (b and c) or (bitNot(b) and d)
                        g = i
                    case 16 <= i and i <= 31
                        f = (d and b) or (bitNot(d) and c)
                        g = (5 * i + 1) mod 16
                    case 32 <= i and i <= 47
                        f = b xor c xor d
                        g = (3 * i + 5) mod 16
                    case 48 <= i and i <= 63
                        f = c xor (b or bitNot(d))
                        g = (7 * i) mod 16
                end select
                temp = d
                d = c
                c = b
                b=b+leftrotate(a + f + k(i) + w(g),r(i))
                b = b mod two32
                a = temp
            next i
            h0 = (h0 + a) mod two32
            h1 = (h1 + b) mod two32
            h2 = (h2 + c) mod two32
            h3 = (h3 + d) mod two32
        next chunk
 
        md5$ =  revOrd$(DECHEX$(h0))+_
                revOrd$(DECHEX$(h1))+_
                revOrd$(DECHEX$(h2))+_
                revOrd$(DECHEX$(h3))
 
        [perRoundShiftAmounts]
        DATA 7,12,17,22, 7,12,17,22, 7,12,17,22, 7,12,17,22
        DATA 5, 9,14,20, 5, 9,14,20, 5, 9,14,20, 5, 9,14,20
        DATA 4,11,16,23, 4,11,16,23, 4,11,16,23, 4,11,16,23
        DATA 6,10,15,21, 6,10,15,21, 6,10,15,21, 6,10,15,21
 
    end function
 
 
 
    ' dec2asc: dec to 8 byte asc
    function dec2asc$(n)
        h$ = ""
        for i = 1 to 8
            h$ = h$ + chr$(n mod 256)
            n = int(n/256)
        next
        dec2asc$= h$
    end function
 
    ' bitNot
    function bitNot(num)
        bitNot = two32 -1 -num
    end function
 
    ' leftrotate: spins bits left n times
    function leftrotate(num,times)
        num=num mod two32
        r = (num*2^times) mod two32
        l = int(num/(2^(32-times)))
        leftrotate = r+l
    end function
 
    ' reverse the HEXDEC$ order
    function revOrd$(a$)
        a$=left$("00000000", 8-len(a$))+a$
        revOrd$ = lower$(mid$(a$,7,2)+mid$(a$,5,2)+mid$(a$,3,2)+mid$(a$,1,2))
    end function

版权所有: 百万商业圈
未经许可不得转载,有任何疑问请与我本人联系 QQ 99923309 Mail:[email protected] 更多详情>>

开源:完全自主研发搜索引擎1.0源代码及说明,单机400万网页,任意50词以内的检索不超过 20毫秒

开源:基于百万商业圈.NET开发框架开发的并行带分词的采集器

百万商业圈 .NET 开发框架2.0及开发框架API说明书(BWFW)(含并行计算及中英文分词功能)

分享一点代码(小型C web开发框架),用C语言实现的一个WEB 文件上传(含全部源代码)一

天心天字辈ERP全部PDK源代码到了我手上的后果 - 超越天心之WEB天云

大家看看我的BS的甘特图排程做的如何? 无刷新Ajax甘特图 展示生产排程结果 演示

软件工程概述 - 企业架构 - IT企业做大做强之根本 - 之我见

实践检验得出的真理:asp.net 项目在 linux mono上编译需要修改的只有 3个地方

给大家漏一手本人亲自精心撰写的通用ajax框架,完全兼容 IE FireFox各个版本!(附完整源码及完整范例)

开发了一个中文分词服务器(C语言开发+词库+源代码),最大特色可以让javascript来调用!

用纯C语言写了一个HtmlParse(网页分析器)外带采集功能,大小只有200K(免费+开源+操作示意图)

分享:用九种语言分别写一个web server 之 C语言web server

分享:用九种语言分别写一个web server 之 Delphi语言web server

分享:用九种语言分别写一个web server 之 Fantom语言web server

分享:用九种语言分别写一个web server 之 Perl语言web server

分享:用九种语言分别写一个web server 之 Go语言web server

分享:用九种语言分别写一个web server 之 PicoLisp语言web server

分享:用九种语言分别写一个web server 之 PureBasic语言web server

分享:用九种语言分别写一个web server 之 Python语言web server

分享:用九种语言分别写一个web server 之 Tcl语言web server

你可能感兴趣的:(算法,Web,server,function,basic,语言)