SHA-2 hash functions (SHA-224, SHA-256, SHA-384, SHA-512)

Share your advanced PureBasic knowledge/code with the community.
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

SHA-2 hash functions (SHA-224, SHA-256, SHA-384, SHA-512)

Post by javabean »

I have created a static .lib (x86) containing some SHA-2 hash functions (SHA-224, SHA-256, SHA-384 and SHA-512).
I found the source code for the lib on the net. Sources are written in 'C language'.
The .lib, sources and an example can be downloaded HERE (left mouse click to download!) as a 7z-archive.

Code: Select all

;++++++++++++++++++++++++++++++++++++++++++++++
;| Demonstration of the 'SHA-2 functions lib' |
;++++++++++++++++++++++++++++++++++++++++++++++
;
; Demo:
; PureBasic demonstration code by javabean (2010)
; PureBasic 4.40 (Windows - x86)
;---
; Library source:
; 'FIPS 180-2 SHA-224/256/384/512 implementation'
; Source code for 'sha2.lib' taken from
; (http://www.ouah.org/ogay/sha2/sha2.tar.gz)
; Copyright (C) 2005, 2007 Olivier Gay
; <olivier.gay@a3.epfl.ch>
;
; LICENSE:
; --------
; Copyright (C) 2005, 2007 Olivier Gay <olivier.gay@a3.epfl.ch>
; All rights reserved.
;
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions
; are met:
; 1. Redistributions of source code must retain the above copyright
;    notice, this list of conditions and the following disclaimer.
; 2. Redistributions in binary form must reproduce the above copyright
;    notice, this list of conditions and the following disclaimer in the
;    documentation and/or other materials provided with the distribution.
; 3. Neither the name of the project nor the names of its contributors
;    may be used to endorse or promote products derived from this software
;    without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
; OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
; LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
; OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
; SUCH DAMAGE.
;---
; Library build:
; 'sha2.lib' built with 'PellesC for Windows 4.50.113'
; Additional objectfiles (exploded from 'crt.lib' using POLIB \EXPLODE):
; _llshl.obj, _ullshr.obj
;++++++++++++++++++++++++++++++++++++++++++++++++


Import "sha2.lib"; '_stdcall'
  sha224(*message.l, len.l, *digest.l) As "_sha224@12"
  sha256(*message.l, len.l, *digest.l) As "_sha256@12"
  sha384(*message.l, len.l, *digest.l) As "_sha384@12"
  sha512(*message.l, len.l, *digest.l) As "_sha512@12"
EndImport


#SHA224_DIGEST_SIZE = 224/8
#SHA256_DIGEST_SIZE = 256/8
#SHA384_DIGEST_SIZE = 384/8
#SHA512_DIGEST_SIZE = 512/8

 
OpenConsole()
  ConsoleTitle("SHA2 Test")
  
  message$=""
  Repeat 
    Print("Please enter a message: ")
    msg$ = Input()
  Until msg$<>""
 
   OemToChar_(msg$,message$) ;translates a string from the OEM-defined character set into an ANSI character string.
  
   *digest224 = AllocateMemory(#SHA224_DIGEST_SIZE)
   sha224(@message$, Len(message$),*digest224)
   *digest256 = AllocateMemory(#SHA256_DIGEST_SIZE)
   sha256(@message$, Len(message$),*digest256)
   *digest384 = AllocateMemory(#SHA384_DIGEST_SIZE)
   sha384(@message$, Len(message$),*digest384)
    *digest512 = AllocateMemory(#SHA512_DIGEST_SIZE)
   sha512(@message$, Len(message$),*digest512)
   
   PrintN("")
   PrintN("Message : "+msg$): PrintN("")
   
   str$="": For i=0 To (#SHA224_DIGEST_SIZE -1): str$+RSet(Hex(PeekA(*digest224+i),#PB_Byte),2,"0"): Next :  PrintN("SHA224 : "+str$)
   str$="": For i=0 To (#SHA256_DIGEST_SIZE -1): str$+RSet(Hex(PeekA(*digest256+i),#PB_Byte),2,"0"): Next :  PrintN("SHA256 : "+str$)
   str$="": For i=0 To (#SHA384_DIGEST_SIZE -1): str$+RSet(Hex(PeekA(*digest384+i),#PB_Byte),2,"0"): Next :  PrintN("SHA384 : "+str$)
   str$="": For i=0 To (#SHA512_DIGEST_SIZE -1): str$+RSet(Hex(PeekA(*digest512+i),#PB_Byte),2,"0"): Next :  PrintN("SHA512 : "+str$)
  
   
  PrintN("")
  PrintN("")
  PrintN("PRESS 'RETURN' TO QUIT.")
  Input()
CloseConsole()
Edit: [21.02.2010]: Changed "ImportC" to "Import" (_stdcall)
Last edited by javabean on Tue Mar 02, 2010 8:04 pm, edited 4 times in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: SHA-2 hash functions (SHA-224, SHA-256, SHA-384, SHA-512)

Post by netmaestro »

Looks very interesting, javabean and thanks - but I currently am getting a 0-byte download :cry:
BERESHEIT
javabean
User
User
Posts: 60
Joined: Sat Nov 08, 2003 10:29 am
Location: Austria

Re: SHA-2 hash functions (SHA-224, SHA-256, SHA-384, SHA-512)

Post by javabean »

Ooops - sorry for that...
Should be fixed now!
Post Reply