Code:
'===================================================================== '-- SHA256c.BAS '-- Test bed for SHA256c.INC '-- Compiles with either PBWIN 8/9+ or PBCC 4/5+ '===================================================================== #COMPILE EXE #DIM ALL #REGISTER NONE '============ #INCLUDE "WIN32API.INC" #INCLUDE "SHA256c.INC" '-------------------- 'Utility macros '-------------------- #IF %def(%pb_win32) MACRO eol=$CR MACRO say(t) #IF %PB_REVISION >= &H900 MessageBox 0&, BYCOPY (t), exe.namex$, %MB_OK OR %MB_TASKMODAL #ELSE MessageBox 0&, BYCOPY (t), "", %MB_OK OR %MB_TASKMODAL #ENDIF END MACRO MACRO EnterCC END MACRO MACRO ExitCC END MACRO #IF %PB_REVISION < &H900 MACRO IsFile(fname) =(len(dir$(fname,(%READONLY OR %HIDDEN OR %SYSTEM)))<>0) #ENDIF #ELSEIF %def(%pb_cc32) MACRO eol=$CRLF MACRO say(t)=stdout t MACRO EnterCC LOCAL launched AS LONG if (cursory = 1) and (cursorx = 1) then launched = -1 END MACRO MACRO ExitCC if launched then input flush stdout "Press any key to end" waitkey$ end if END MACRO #IF %PB_REVISION < &H500 MACRO IsFile(fname) =(len(dir$(fname,(%READONLY OR %HIDDEN OR %SYSTEM)))<>0) #ENDIF #ENDIF DECLARE SUB VerifyImplementation() DECLARE FUNCTION ShowHash32(shouldBe$, Hash$) AS LONG DECLARE FUNCTION Hex2ShowDword(Buffer$) AS STRING DECLARE FUNCTION Get_FileSize(File_Name$) AS QUAD '==================== FUNCTION PBMain() AS LONG REGISTER i AS LONG EnterCC VerifyImplementation '============ ExitMain: ExitCC END FUNCTION '==================== SUB VerifyImplementation() LOCAL buffer, sha, shouldBe AS STRING gosub TestVectors1 gosub TestVectors2 gosub TestVectors3 gosub TestVectors4 gosub TestVectors5 gosub TestVectors6 gosub TestVectors7 gosub FileHash EXIT SUB '============ TestVectors1: buffer = "abc" sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldBe = "BA7816BF 8F01CFEA 414140DE 5DAE2223 B00361A3 96177A9C B410FF61 F20015AD" ShowHash32 shouldBe, sha RETURN '============ TestVectors2: buffer = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldBe = "248D6A61 D20638B8 E5C02693 0C3E6039 A33CE459 64FF2167 F6ECEDD4 19DB06C1" ShowHash32 shouldBe, sha RETURN '============ TestVectors3: buffer = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhij" buffer = buffer + "klmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldBe = "CF5B16A7 78AF8380 036CE59E 7B049237 0B249B11 E8F07A51 AFAC4503 7AFEE9D1" ShowHash32 shouldBe, sha RETURN '============ TestVectors4: buffer = string$(1000000,"a") sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldBe = "CDC76E5C 9914FB92 81A1C7E2 84D73E67 F1809A48 A497200E 046D39CC C7112CD0" ShowHash32 shouldBe, sha RETURN '============ TestVectors5: buffer = "message digest" sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldbe = "F7846F55 CF23E14E EBEAB5B4 E1550CAD 5B509E33 48FBC4EF A3A1413D 393CB650" ShowHash32 shouldBe, sha RETURN '============ TestVectors6: buffer = "abcdefghijklmnopqrstuvwxyz" sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldbe = "71C480DF 93D6AE2F 1EFAD144 7C66C952 5E316218 CF51FC8D 9ED832F2 DAF18B73" ShowHash32 shouldBe, sha RETURN '============ TestVectors7: buffer = "" sha = string$(32,0) SHA256_Buffer byval strptr(buffer), len(buffer), byval strptr(sha) shouldbe = "E3B0C442 98FC1C14 9AFBF4C8 996FB924 27AE41E4 649B934C A495991B 7852B855" ShowHash32 shouldBe, sha RETURN '============ FileHash: LOCAL t$, file_name$, ecode&, file_size AS QUAD LOCAL t1, t2, t3 AS SINGLE file_name = "" if len(file_name) = 0 then say("No file specified") return end if if isfile(file_name) = 0 then say("Cannot find file " + file_name) return end if t1 = GetTickCount ecode = SHA256_File(File_Name$, sha$) t2 = GetTickCount if ecode then say("SHA256_File error" + str$(ecode) + eol + error$(ecode)) return end if say(eol + file_name + eol + Hex2ShowDword(sha)) file_size = Get_FileSize(file_name$) t3 = (t2-t1)/1000 t = "File size: " + using$(",",file_size) + " bytes" + eol t = t + "Time elapsed: " + format$(t3,"###.###") + " seconds" + eol t = t + format$(file_size/t3,"#########,") + " BPS" say(t+eol) RETURN END SUB '==================== FUNCTION ShowHash32(shouldBe$, Hash$) AS LONG LOCAL t$ t = "Should be:" + eol + shouldBe$ + eol t = t + "Actual: " + eol + Hex2ShowDWord(Hash$) say(t) END FUNCTION '==================== FUNCTION Hex2ShowDword(Buffer$) AS STRING REGISTER i AS LONG, j AS LONG LOCAL t$, pbyte AS BYTE PTR pbyte = strptr(Buffer$) for i = 0 to (%HASHLEN\4)-1 for j = 0 to 3 t = t + hex$(@pbyte,2) incr pbyte next j t = t + " " next i function = t END FUNCTION #IF %PB_REVISION >= &H900 '==================== FUNCTION Get_FileSize(File_Name$) AS QUAD LOCAL totalbytes AS QUAD, fdata AS DIRDATA if len(dir$(File_Name$, to fdata)) then totalbytes = fdata.FileSizeLow + fdata.FileSizeHigh end if function = totalbytes END FUNCTION #ELSE '==================== FUNCTION Get_FileSize(File_Name$) AS QUAD LOCAL hFind, i AS LONG, totalBytes AS QUAD LOCAL fname AS ASCIIZ * %MAX_PATH LOCAL fdata AS WIN32_FIND_DATA fname = File_Name$ hFind = FindFirstFile(fname, fdata) if hFind then totalBytes = fdata.nFileSizeLow + fdata.nFileSizeHigh if hFind then FindClose hFind end if function = totalbytes END FUNCTION #ENDIF '-- END SHA256c.BAS ---------------------------------------------------
Leave a comment: