
---
I spent a bit of time on cleaning up the code, recompiling and repackaging it. You can download it from http://www.host4scripts.de/pub/SHA1.zip.
It contains 2 Libraries that contain the same function set, so make sure to just use one. The SHA1.SSE2 lib is compiled so it includes both the command set of the SHA1 lib as an SSE2 enabled version, which can result in a speed gain.
Make sure that you choose the PB compiler options appropriatly, when using the SSE2 version. PureBasic can produce executables that only use the SSE2 part, or both SSE2 as non-SSE2, or only non-SSE2.
An sample how to use is included:
Code: Select all
; SHA1 Lib by Max., August 16, 2004
; - http://www.host4scripts.de/pub/SHA1.zip -
;
; Two Libs are provided in this package.
; Both are containing the same functions. Put _just one_ of them into the
; PureLibraries\UserLibraries folder!
;
; 1. SHA1 (4 KB)
; The standard lib
; 2. SHA1.SSE2 (21 KB)
; This library contains the above functions plus the same compiled
; for SSE2 enabled PCs.
; Depending on your compiler settings, PureBasic automatically
; links the appropriate functions to your executable or even any,
; deciding which function to use for best performance during run-
; time.
;
; While the speed improvement is not too bad with ~10% performance
; gain on SSE2 machines, the reason for this was because I wanted
; to test it. :-)
; For sure better performance gains could result by optimizing the
; code itself. That is the next task.
;
; The functions:
;
; SHA1(@Hash,@Input,InputLen)
;
; The result is given back in @Hash. You also can use pointers to a memory
; buffer, depending on what you want to hash.
;
; Hash.s=SHA1Fingerprint(Input.s)
;
; Easy-to-use version. The hash from an input string is returned as
; hex string.
;
; In the sample, I am using a known test vector to verify that it works.
; You should see no output except the milliseconds elapsed for running
; the program except there is an error (result is wrong).
;
; BTW, the digest of SHA1 is always 160bit long, that means you need
; 20 bytes to store @Hash when using the SHA1 function or 40 characters
; when using SHA1Fingerprint.
;expected hash of test vector "abc": a9993e364706816aba3e25717850c26c9cd0d89d
Start = GetTickCount_()
For bench=1 To 5000000
Result.s = ""
;SHA1 generic
Hash.s=Space(255)
Input.s = "abc"
SHA1(@Hash,@Input,Len(Input))
For i=1 To 20 ;SHA1 always returns a 20 byte digest (160bit)
Result.s=Result+RSet(Hex(PeekB(@Hash+i-1)& $ff),2,"0")
Next i
If Result<>UCase("a9993e364706816aba3e25717850c26c9cd0d89d")
Debug Result.s
Debug Len(Result)
Debug "Error"
End
EndIf
;easy mode
Result.s = SHA1Fingerprint("abc")
If Result<>UCase("a9993e364706816aba3e25717850c26c9cd0d89d")
Debug Result.s
Debug Len(Result)
Debug "Error"
End
EndIf
Next bench
Debug GetTickCount_()-Start