Page 1 of 1

SHA1 Hash Library

Posted: Mon Aug 16, 2004 10:27 pm
by Max.²
Edit: Updated to fix a memory leak. Should have noticed that and fixed it, instead of looking for furtile optimizations. :lol: Speed is much higher now.

---
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

Posted: Thu Aug 19, 2004 12:24 am
by newbie
my homepage is updated ;)

Thanks for you work Max.², it's greatly appreciated ! :D

Posted: Thu Aug 19, 2004 12:58 am
by Max.²
newbie wrote:my homepage is updated ;)

Thanks for you work Max.², it's greatly appreciated ! :D
Darn, I got too many ideas at the moment.

I want to finish the other SHA hashes, then I'd like to start on TEA. And I got some more ideas on helper functions, like customizable padding. And so on and so on. :?

Learning a lot out of it though. Tonight I finally understood pointers in C entirely. I hope. :wink:

Posted: Thu Aug 19, 2004 7:58 am
by Rings
just a small tip for creating Purebasic-Libs.
If you create OBJ-Files with your compiler or Assembler, you can merge them into a LIB file, simply with:

lcclib.exe /out:MyLIB.LIB MyFunction.OBJ MyFunction2.OBJ MyFunction3.OBJ

in the DESC-File you can put all Declarations and mark the library as a LIB one . Hope you (every LibCoder) understand this.

lcclib is available in the LCC package, or in older Purebasic-versions.

Posted: Fri Aug 20, 2004 12:53 am
by Max.²
Rings wrote:in the DESC-File you can put all Declarations and mark the library as a LIB one . Hope you (every LibCoder) understand this.
Done that already. Basically everything is in a seperate object file. Beside the smaller size I like the easier handling. Not need to recompile everything just because I screwed up something elsewhere. :lol:

Posted: Thu May 07, 2009 8:38 pm
by safra
Hi, I have searched my butt-tocks off looking for a SHA1 algorithm - seems every link in this forum is dead. Can anyone help please?

Posted: Thu May 07, 2009 8:48 pm
by Fluid Byte
What about the in-built functions?

PS: What is "butt-tocks"? Ass?

Posted: Thu May 07, 2009 8:53 pm
by safra
Fluid Byte wrote:What about the in-built functions?

PS: What is "butt-tocks"? Ass?
Ah, my bad - I should have said I was looking for SHA1 code, not an already-done-for-you function. I'd like to see how it works in Purebasic.

And yeah, ass - Forrest Gump style!

Posted: Thu May 07, 2009 9:41 pm
by freak
PB uses this implementation:
http://sea-to-sky.net/~sreid/sha1.c

However if you want to know how the algorithm actually works, such an implementation is the wrong place to start looking.

Here is some pseudocode which is probably easier to understand:
http://en.wikipedia.org/wiki/SHA_hash_f ... pseudocode

Posted: Fri May 08, 2009 6:25 am
by safra
Oh, that is brilliant, thank you! :D