Test code for both PBCC and PBWin versions.
PBCC:
Code:
'------------------------------------------------------------------------------- #register none 'turn off automatic assignment of vars to CPU registers by the 'compiler due to heavy use of registers in assember section. #dim all 'force declaration/casting of vars before use. #include "SHA1_Hash.inc" '------------------------------------------------------------------------------- 'Byte-by-byte binary to hex string function tSHA (SHA as string * 20) as string local pByte as byte pointer local Result as string local x as long local y as long pByte = varptr(SHA) for x = 1 to 5 for y = 1 to 4 Result = Result + hex$(@pByte, 2) incr pByte next Result = Result + " " next function = Result end function '------------------------------------------------------------------------------- function pbmain '{DIM to LOCAL} local SHA as string * 20 ' Control samples from official document 'FIPS 180-2, A.1 example. CalcSHA1("abc", SHA) print "Input = abc" print "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" + _ " FIPS 180-2, A.1" print tSHA(SHA) + "As calculated by CalcSHA1." print ' 'FIPS 180-2, A.2 example. CalcSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", SHA) print "Input = abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" print "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" + _ " FIPS 180-2, A.2" print tSHA(SHA) + "As calculated by CalcSHA1." print ' 'FIPS 180-2, A.3 example. CalcSHA1(string$(1000000, "a"), SHA) print "Input = 1000000 lower case " + $dq + "a" + $dq print "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" + _ " FIPS 180-2, A.3" print tSHA(SHA) + "As calculated by CalcSHA1." print ' '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' ' Test of speed ' '{On my 2.6GHz Pentium the original speed test returned "0 KB per second". ' ' Changing input string to 2 GB returns "9.99999999999999E+998 per ' ' second". Rediculous! PC too fast for meaningful change in TIMER with ' ' one iteration of CalcSHA. So, wrote new speed test.} local Tmp as string 'Input local T1, T2 as double 'Start/Stop TIMER values local x as long 'FOR/NEXT counter ' print "Speed test, please wait ..." Tmp = string$(1000000, "w") sleep 0 T1 = timer for x = 1 to 100 CalcSHA1(Tmp, SHA) next T2 = timer print format$(100000000 / 1000000 / (T2 - T1)) + " MB per second" color 2, 15 print "Press any key to exit" waitstat end function
Code:
'------------------------------------------------------------------------------- #compile exe 'as opposed to dll. #register none 'turn off automatic assignment of vars to CPU registers by the 'compiler due to heavy use of registers in assember section. #dim all 'force declaration/casting of vars before use. #include "SHA1_Hash.inc" '------------------------------------------------------------------------------- 'Byte-by-byte binary to hex string function tSHA (SHA as string * 20) as string local pByte as byte pointer local Result as string local x as long local y as long pByte = varptr(SHA) for x = 1 to 5 for y = 1 to 4 Result = Result + hex$(@pByte, 2) incr pByte next Result = Result + " " next function = Result end function '------------------------------------------------------------------------------- function pbmain '{DIM to LOCAL} local SHA as string * 20 ' Control samples from official document 'FIPS 180-2, A.1 example. CalcSHA1("abc", SHA) msgbox "Input = abc" + $crlf + _ "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" + _ " FIPS 180-2, A.1" + $crlf + _ tSHA(SHA) + "As calculated by CalcSHA1." ' 'FIPS 180-2, A.2 example. CalcSHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", SHA) msgbox "Input = abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" + _ $crlf + _ "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" + _ " FIPS 180-2, A.2" + $crlf + _ tSHA(SHA) + "As calculated by CalcSHA1." ' 'FIPS 180-2, A.3 example. CalcSHA1(string$(1000000, "a"), SHA) msgbox "Input = 1000000 lower case " + $dq + "a" + $dq + $crlf + _ "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F" + _ " FIPS 180-2, A.3" + $crlf + _ tSHA(SHA) + "As calculated by CalcSHA1." '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ' Test of speed '{On my 2.6GHz Pentium the original speed test returned "0 KB per second". ' Changing input string to 2 GB returns "9.99999999999999E+998 per ' second". Rediculous! PC too fast for meaningful change in TIMER with ' one iteration of CalcSHA. So, wrote new speed test.} local Tmp as string 'Input local T1, T2 as double 'Start/Stop TIMER values local x as long 'FOR/NEXT counter ' Tmp = string$(1000000, "w") sleep 0 T1 = timer for x = 1 to 100 CalcSHA1(Tmp, SHA) next T2 = timer msgbox format$(100000000 / 1000000 / (T2 - T1)) + " MB per second" end function
Leave a comment: