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

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
javabean
Beiträge: 29
Registriert: 16.12.2004 18:47

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

Beitrag von javabean »

Hi,
ich hab' mir für ein Projekt eine statische .lib mit SHA-2 Hashing-Funktionen (SHA-224, SHA-256, SHA-384 und SHA-512) zusammengebastelt. Vielleicht kann's ja wer brauchen...
Den Original-Source-Code (in C) dafür hab' ich im Internet gefunden.
Die Library sowie alle Source Codes und ein PB-Beispiel kann HIER (Links-Click!) als 7z-Archiv (~100 kB) runtergeladen werden.

Code: Alles auswählen

;++++++++++++++++++++++++++++++++++++++++++++++
;| 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]: "ImportC" ausgebessert zu "Import" (_stdcall)
Benutzeravatar
purebas
Beiträge: 127
Registriert: 11.03.2008 23:59
Wohnort: München

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

Beitrag von purebas »

Kompatible ist diese Ausführung leider nicht.
Meine Referenz: http://www.hashgenerator.de/

Kann ich da was machen, damit es funktioniert?
Antworten