SHA256

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.
Christian+
Beiträge: 213
Registriert: 13.07.2008 10:05
Computerausstattung: Windows 8.1 Pro
AMD Phenom II X4 955 @ 3.2 GHz
4GB RAM
NVIDIA GeForce GTX 660

SHA256

Beitrag von Christian+ »

Da es bisher noch keinen SHA256 Algorithmus gibt der rein in Pure Basic umgesetzt ist, sondern nur welche die auf eine Lib zugreifen oder in ASM umgesetzt sind, habe ich kurz mal Pseudocode nach PureBasic übersetzt. Diese Variante ist denke ich nicht so schnell wie eine gute ASM Umsetzung aber für die bei denen es nicht so sehr auf den Speed ankommt ist diese Variante eventuell ganz brauchbar. Vorteil davon ist, dass man dann nicht so ein ASM Monster einbinden muss wo man kein Plan hat wie es Funktioniert und auch keine externe Lib braucht die es vielleicht 2 Monate später nicht mehr gibt.

Update 1:
Der Code ist nun zu einem Modul für PB5.20 umgebaut, das einiges schneller ist und mehr Funktionen bietet.

Code: Alles auswählen

; ==================================================================================================
; --- Program         : Module SHA256
; --- Author          : Christian+
; --- Source          : http://purebasic.fr/german/viewtopic.php?p=309915#p309915
; --- Date            : August 09, 2013
; --- Compiler        : PureBasic 5.20
; --- Target OS       : All
; ==================================================================================================
;
; Komplett in Pure Basic umgesetztes Modul für SHA 256.
;
; ==================================================================================================

DeclareModule SHA256

Structure SHA256DATA
  Size.q
  ChunkSize.i
  *Hash.LongArray
  *Chunk.LongArray
EndStructure

Declare.i ExamineSHA256()
Declare   NextSHA256(*Id.SHA256DATA, *Memory, Size.q)
Declare.i FinishSHA256(*Id.SHA256DATA)

Declare.i SHA256(*Memory, Size.q = #PB_Any)
Declare.i SHA256String(String.s, Format.i = #PB_Ascii)
Declare.i SHA256File(FileName.s)

Declare.s ConvertToHexString(*Hash)

Declare.s FinishSHA256Fingerprint(*Id.SHA256DATA)

Declare.s SHA256Fingerprint(*Memory, Size.q = #PB_Any)
Declare.s SHA256StringFingerprint(String.s, Format.i = #PB_Ascii)
Declare.s SHA256FileFingerprint(FileName.s)

EndDeclareModule

; ==================================================================================================

Module SHA256

EnableExplicit

DisableDebugger

Structure LongArray
  l.l[0]
EndStructure

Macro RightShift(Number, Count)
  ((Number >> Count) & (~($FFFFFFFF << (32 - Count))))
EndMacro

Macro RightRotate(Number, Count)
  (RightShift(Number, Count) | (Number << (32 - Count)))
EndMacro

Procedure.l EndianSwap32(x.l)
  ProcedureReturn x<<24 | ((x<<8) & $00FF0000) | ((x>>8) & $0000FF00) | ((x>>24) & $000000FF)
EndProcedure

Procedure.q EndianSwap64(x.q)
  Protected z.q = x << 56
  z = z | ((x << 40) & $00FF000000000000)
  z = z | ((x << 24) & $0000FF0000000000)
  z = z | ((x <<  8) & $000000FF00000000)
  z = z | ((x >>  8) & $00000000FF000000)
  z = z | ((x >> 24) & $0000000000FF0000)
  z = z | ((x >> 40) & $000000000000FF00)
  z = z | ((x >> 56) & $00000000000000FF)
  ProcedureReturn z
EndProcedure

DataSection
  k:
  Data.l $428a2f98, $71374491, $b5c0fbcf, $e9b5dba5, $3956c25b, $59f111f1, $923f82a4, $ab1c5ed5
  Data.l $d807aa98, $12835b01, $243185be, $550c7dc3, $72be5d74, $80deb1fe, $9bdc06a7, $c19bf174
  Data.l $e49b69c1, $efbe4786, $0fc19dc6, $240ca1cc, $2de92c6f, $4a7484aa, $5cb0a9dc, $76f988da
  Data.l $983e5152, $a831c66d, $b00327c8, $bf597fc7, $c6e00bf3, $d5a79147, $06ca6351, $14292967
  Data.l $27b70a85, $2e1b2138, $4d2c6dfc, $53380d13, $650a7354, $766a0abb, $81c2c92e, $92722c85
  Data.l $a2bfe8a1, $a81a664b, $c24b8b70, $c76c51a3, $d192e819, $d6990624, $f40e3585, $106aa070
  Data.l $19a4c116, $1e376c08, $2748774c, $34b0bcb5, $391c0cb3, $4ed8aa4a, $5b9cca4f, $682e6ff3
  Data.l $748f82ee, $78a5636f, $84c87814, $8cc70208, $90befffa, $a4506ceb, $bef9a3f7, $c67178f2
EndDataSection

Procedure.i ExamineSHA256()
  Protected *Id.SHA256DATA = AllocateMemory(SizeOf(SHA256DATA))
  *Id\Hash = AllocateMemory(32, #PB_Memory_NoClear)
  *Id\Chunk = AllocateMemory(256, #PB_Memory_NoClear)
  *Id\Hash\l[0] = $6A09E667 : *Id\Hash\l[1] = $BB67AE85 : *Id\Hash\l[2] = $3C6EF372 : *Id\Hash\l[3] = $A54FF53A 
  *Id\Hash\l[4] = $510E527F : *Id\Hash\l[5] = $9B05688C : *Id\Hash\l[6] = $1F83D9AB : *Id\Hash\l[7] = $5BE0CD19
  ProcedureReturn *Id
EndProcedure

Procedure ProcessChunk(*Id.SHA256DATA)
  Protected *k.Long = ?k
  Protected *Chunk.Long = *Id\Chunk
  Protected.i a, b, c, d, e, f, g, h, t1, t2, s0, s1, maj, ch, c15, c2, i
  For i = 0 To 15 : *Id\Chunk\l[i] = EndianSwap32(*Id\Chunk\l[i]) : Next
  For i = 16 To 63
    c15 = *Id\Chunk\l[i-15]
    c2 = *Id\Chunk\l[i-2]
    s0 = RightRotate(c15, 7) ! RightRotate(c15, 18) ! RightShift(c15, 3)
    s1 = RightRotate(c2, 17) ! RightRotate(c2, 19) ! RightShift(c2, 10)
    *Id\Chunk\l[i] = *Id\Chunk\l[i-16] + s0 + *Id\Chunk\l[i-7] + s1
  Next
  a = *Id\Hash\l[0] : b = *Id\Hash\l[1] : c = *Id\Hash\l[2] : d = *Id\Hash\l[3]
  e = *Id\Hash\l[4] : f = *Id\Hash\l[5] : g = *Id\Hash\l[6] : h = *Id\Hash\l[7]  
  For i = 0 To 63
    s0 = RightRotate(a, 2) ! RightRotate(a, 13) ! RightRotate(a, 22)
    maj = (a & b) ! (a & c) ! (b & c)
    t2 = S0 + maj
    s1 = RightRotate(e, 6) ! RightRotate(e, 11) ! RightRotate(e, 25)
    ch = (e & f) ! ((~ e) & g)
    t1 = h + S1 + ch + *k\l + *Chunk\l 
    h = g : g = f : f = e : e = d + t1
    d = c : c = b : b = a : a = t1 + t2
    *k+4
    *Chunk+4
  Next
  *Id\Hash\l[0] + a : *Id\Hash\l[1] + b : *Id\Hash\l[2] + c : *Id\Hash\l[3] + d
  *Id\Hash\l[4] + e : *Id\Hash\l[5] + f : *Id\Hash\l[6] + g : *Id\Hash\l[7] + h
EndProcedure

Procedure NextSHA256(*Id.SHA256DATA, *Memory, Size.q)
  Protected.i d, NumberOfChunks, i
  *Id\Size + Size
  If *Id\ChunkSize > 0
    d = (64 - *Id\ChunkSize)
    If d > Size : d = Size : EndIf
    CopyMemory(*Memory, *Id\Chunk + *Id\ChunkSize, d)
    *Memory + d : Size - d : *Id\ChunkSize + d
    If *Id\ChunkSize = 64
      ProcessChunk(*Id.SHA256DATA)
      *Id\ChunkSize = 0
    EndIf
  EndIf
  NumberOfChunks = Size / 64
  For i = 0 To NumberOfChunks - 1
    CopyMemory(*Memory+i*64, *Id\Chunk, 64)
    ProcessChunk(*Id)
  Next
  If Size % 64 <> 0
    CopyMemory(*Memory + NumberOfChunks * 64, *Id\Chunk, Size % 64)
    *Id\ChunkSize = Size % 64
  EndIf
EndProcedure

Procedure.i FinishSHA256(*Id.SHA256DATA)
  Protected i.i
  Protected *Hash = *Id\Hash
  Protected BitLength.q = *Id\Size * 8 + 1 + 64
  If BitLength % 512 <> 0
    BitLength = BitLength + (512 - (BitLength % 512))
  EndIf
  FillMemory(*Id\Chunk+*Id\ChunkSize, 64-*Id\ChunkSize, 0)
  PokeA(*Id\Chunk+*Id\ChunkSize, $80)
  If (BitLength/8 - *Id\Size) > 64
    ProcessChunk(*Id)
    FillMemory(*Id\Chunk, 64, 0)
  EndIf
  PokeQ(*Id\Chunk + 64 - 8, EndianSwap64(*Id\Size*8))
  ProcessChunk(*Id)
  For i = 0 To 7 : *Id\Hash\l[i] = EndianSwap32(*Id\Hash\l[i]) : Next
  FreeMemory(*Id\Chunk)
  FreeMemory(*Id)
  ProcedureReturn *Id\Hash
EndProcedure

Procedure.i SHA256(*Memory, Size.q = #PB_Any)
  Protected *Id = ExamineSHA256()
  If Size = #PB_Any : Size = MemorySize(*Memory) : EndIf
  NextSHA256(*Id, *Memory, Size)
  ProcedureReturn FinishSHA256(*Id)
EndProcedure

Procedure.i SHA256String(String.s, Format.i = #PB_Ascii)
  Protected Size.i, *Memory, *Hash
  Size = StringByteLength(String, Format)
  *Memory = AllocateMemory(Size + 1)
  PokeS(*Memory, String, -1, Format)
  *Hash = SHA256(*Memory, Size)
  FreeMemory(*Memory)
  ProcedureReturn *Hash
EndProcedure

Procedure.i SHA256File(FileName.s)
  Protected Size.i, File.i = ReadFile(#PB_Any, FileName)
  If Not File : ProcedureReturn 0 : EndIf
  Protected *Id = ExamineSHA256()
  Protected *Memory = AllocateMemory(640000, #PB_Memory_NoClear)
  While Not Eof(File)
    Size = ReadData(File, *Memory, 640000)
    NextSHA256(*Id, *Memory, Size)
  Wend
  FreeMemory(*Memory)
  CloseFile(File)
  ProcedureReturn FinishSHA256(*Id)
EndProcedure

Macro HexByte(Hash)
  RSet(Hex(PeekA(Hash), #PB_Ascii), 2, "0")
EndMacro

Macro HexLong(Hash)
  HexByte(Hash) + HexByte(Hash+1) + HexByte(Hash+2) + HexByte(Hash+3)
EndMacro

Macro HexSHA256(Hash)
  HexLong(Hash) + HexLong(Hash+4) + HexLong(Hash+8) + HexLong(Hash+12) + HexLong(Hash+16) + HexLong(Hash+20) + HexLong(Hash+24) + HexLong(Hash+28)
EndMacro

Procedure.s ConvertToHexString(*Hash)
  ProcedureReturn HexSHA256(*Hash)
EndProcedure

Procedure.s FinishSHA256Fingerprint(*Id.SHA256DATA)
  Protected *Hash, Hash.s
  *Hash = FinishSHA256(*Id.SHA256DATA)
  Hash = HexSHA256(*Hash)
  FreeMemory(*Hash)
  ProcedureReturn Hash
EndProcedure

Procedure.s SHA256Fingerprint(*Memory, Size.q = #PB_Any)
  Protected *Hash, Hash.s
  If Size = #PB_Any : Size = MemorySize(*Memory) : EndIf
  *Hash = SHA256(*Memory, Size)
  Hash = HexSHA256(*Hash)
  FreeMemory(*Hash)
  ProcedureReturn Hash
EndProcedure

Procedure.s SHA256StringFingerprint(String.s, Format.i = #PB_Ascii)
  Protected *Hash, Hash.s
  *Hash = SHA256String(String, Format)
  Hash = HexSHA256(*Hash)
  FreeMemory(*Hash)
  ProcedureReturn Hash
EndProcedure

Procedure.s SHA256FileFingerprint(FileName.s)
  Protected *Hash, Hash.s
  *Hash = SHA256File(FileName)
  Hash = HexSHA256(*Hash)
  FreeMemory(*Hash)
  ProcedureReturn Hash
EndProcedure

EndModule

;- Beispiele
CompilerIf #PB_Compiler_IsMainFile
  
  EnableExplicit
  
  Define String.s, *Hash
  
  String = ""
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  
  Debug "---------------------------------------------------------------------"
  
  String = "The quick brown fox jumps over the lazy dog"
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592")
  
  Debug "---------------------------------------------------------------------"
  
  String = "The quick brown fox jumps over the lazy dog."
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")
  
  Debug "---------------------------------------------------------------------"
  
  String = "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern"
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8")
  
  Debug "---------------------------------------------------------------------"
  
  String = "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern"
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("78206a866dbb2bf017d8e34274aed01a8ce405b69d45db30bafa00f5eeed7d5e")
  
CompilerEndIf

; ==================================================================================================
Update 2:
Es gibt nun außerdem eine neue experimentelle auf Speed optimierte Version die unter x64 Assembler nutzt: SHA256
Zuletzt geändert von Christian+ am 09.08.2013 17:45, insgesamt 8-mal geändert.
Windows 8.1 Pro 64Bit | AMD Phenom II X4 955 @ 3.2 GHz | 4GB RAM | NVIDIA GeForce GTX 660
Christian+
Beiträge: 213
Registriert: 13.07.2008 10:05
Computerausstattung: Windows 8.1 Pro
AMD Phenom II X4 955 @ 3.2 GHz
4GB RAM
NVIDIA GeForce GTX 660

Re: SHA256

Beitrag von Christian+ »

Code ist nun vollständig überarbeitet und ca. 20-mal schneller als die ganz einfache alte Variante die nur eine möglichst simple Umsetzung des Wikipedia Pseudocode war.
Windows 8.1 Pro 64Bit | AMD Phenom II X4 955 @ 3.2 GHz | 4GB RAM | NVIDIA GeForce GTX 660
Benutzeravatar
ts-soft
Beiträge: 22292
Registriert: 08.09.2004 00:57
Computerausstattung: Mainboard: MSI 970A-G43
CPU: AMD FX-6300 Six-Core Processor
GraKa: GeForce GTX 750 Ti, 2 GB
Memory: 16 GB DDR3-1600 - Dual Channel
Wohnort: Berlin

Re: SHA256

Beitrag von ts-soft »

:allright: danke fürs teilen!

Ich hab mir da gleich ein Modul draus gebastelt, lediglich die 4 Declares in DeclareModule und den Rest in Module.
Speedtest hab ich keinen durchgeführt, aber ich denke für meine Aufgaben sollte es genügen :wink:

Gruß
Thomas
PureBasic 5.73 LTS | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Nutella hat nur sehr wenig Vitamine. Deswegen muss man davon relativ viel essen.
Bild
Benutzeravatar
Bisonte
Beiträge: 2465
Registriert: 01.04.2007 20:18

Re: SHA256

Beitrag von Bisonte »

Ich hoffe so ist das nach Vorschrift ;)

Aber bei dem beispielhaften Code war das ja einfach :allright:

Code: Alles auswählen

; ==================================================================================================
; --- Program         : Module SHA256
; --- File            : module_sha256.pbi
; --- Author          : Christian+ (fit to module by Bisonte)
; --- Source          : http://purebasic.fr/german/viewtopic.php?p=309915#p309915
; --- Date            : August 03, 2013
; --- Compiler        : PureBasic 5.20 beta 8
; --- Target OS       : All
; ==================================================================================================
;
; Da es bisher noch keinen SHA256 Algorithmus gibt der rein in Pure Basic umgesetzt ist, 
; sondern nur welche die auf eine Lib zugreifen oder in ASM umgesetzt sind, habe ich kurz mal 
; Pseudocode nach PureBasic übersetzt. Diese Variante ist denke ich nicht so schnell wie eine gute
; ASM Umsetzung aber für die bei denen es nicht so sehr auf den Speed ankommt ist diese Variante
; eventuell ganz brauchbar. Vorteil davon ist, dass man dann nicht so ein ASM Monster einbinden
; muss wo man kein Plan hat wie es Funktioniert und auch keine externe Lib braucht die es vielleicht
; 2 Monate später nicht mehr gibt.
;
; Code ist nun vollständig überarbeitet und ca. 20-mal schneller als die ganz einfache alte Variante
; die nur eine möglichst simple Umsetzung des Wikipedia Pseudocode war.
;
; ==================================================================================================
DeclareModule SHA256
  
  Declare.i SHA256(*Memory, Size.q)
  Declare.i SHA256String(String.s, Format.i = #PB_Ascii)
  
  Declare.s SHA256Fingerprint(*Memory, Size.q)
  Declare.s SHA256StringFingerprint(String.s, Format.i = #PB_Ascii)  
  
EndDeclareModule
Module        SHA256
  
  EnableExplicit
  
  Structure LongArray
    l.l[0]
  EndStructure
  
  Macro RightShift(Number, Count)
    ((Number >> Count) & (~($FFFFFFFF << (32 - Count))))
  EndMacro
  
  Macro RightRotate(Number, Count)
    (RightShift(Number, Count) | (Number << (32 - Count)))
  EndMacro
  
  Procedure.l EndianSwap32(x.l)
    ProcedureReturn x<<24 | ((x<<8) & $00FF0000) | ((x>>8) & $0000FF00) | ((x>>24) & $000000FF)
  EndProcedure
  
  Procedure.q EndianSwap64(x.q)
    Protected z.q = x << 56
    z = z | ((x << 40) & $00FF000000000000)
    z = z | ((x << 24) & $0000FF0000000000)
    z = z | ((x <<  8) & $000000FF00000000)
    z = z | ((x >>  8) & $00000000FF000000)
    z = z | ((x >> 24) & $0000000000FF0000)
    z = z | ((x >> 40) & $000000000000FF00)
    z = z | ((x >> 56) & $00000000000000FF)
    ProcedureReturn z
  EndProcedure
  
  DataSection
    k:
    Data.l $428a2f98, $71374491, $b5c0fbcf, $e9b5dba5, $3956c25b, $59f111f1, $923f82a4, $ab1c5ed5
    Data.l $d807aa98, $12835b01, $243185be, $550c7dc3, $72be5d74, $80deb1fe, $9bdc06a7, $c19bf174
    Data.l $e49b69c1, $efbe4786, $0fc19dc6, $240ca1cc, $2de92c6f, $4a7484aa, $5cb0a9dc, $76f988da
    Data.l $983e5152, $a831c66d, $b00327c8, $bf597fc7, $c6e00bf3, $d5a79147, $06ca6351, $14292967
    Data.l $27b70a85, $2e1b2138, $4d2c6dfc, $53380d13, $650a7354, $766a0abb, $81c2c92e, $92722c85
    Data.l $a2bfe8a1, $a81a664b, $c24b8b70, $c76c51a3, $d192e819, $d6990624, $f40e3585, $106aa070
    Data.l $19a4c116, $1e376c08, $2748774c, $34b0bcb5, $391c0cb3, $4ed8aa4a, $5b9cca4f, $682e6ff3
    Data.l $748f82ee, $78a5636f, $84c87814, $8cc70208, $90befffa, $a4506ceb, $bef9a3f7, $c67178f2
  EndDataSection
  
  Procedure.i SHA256(*Memory, Size.q)
    Protected.l a, b, c, d, e, f, g, h, t1, t2, s0, s1, maj, ch
    Protected.i ii, i
    Protected *k.LongArray = ?k
    Protected *Hash.LongArray = AllocateMemory(256/8)
    Protected *Chunk.LongArray = AllocateMemory(65*4)
    Protected Bytes.q = Size
    Protected BitLength.q = Size * 8 + 1 + 64
    If BitLength % 512 <> 0
      BitLength = BitLength + (512 - (BitLength % 512))
    EndIf
    *Hash\l[0] = $6A09E667 : *Hash\l[1] = $BB67AE85 : *Hash\l[2] = $3C6EF372 : *Hash\l[3] = $A54FF53A
    *Hash\l[4] = $510E527F : *Hash\l[5] = $9B05688C : *Hash\l[6] = $1F83D9AB : *Hash\l[7] = $5BE0CD19
    For ii = 0 To BitLength/512-1
      If Bytes >= 64
        CopyMemory(*Memory+ii*64, *Chunk, 64)
        Bytes - 64
      Else
        FillMemory(*Chunk, 64, 0)
        If Bytes > 0 : CopyMemory(*Memory+ii*64, *Chunk, Bytes) : EndIf
        If Bytes <> -128 : PokeA(*Chunk + Bytes, $80) : Bytes = -128 : EndIf
        If ii = BitLength/512-1
          PokeQ(*Chunk + 64 - 8, EndianSwap64(Size*8))
        EndIf
      EndIf
      For i = 0 To 16 : *Chunk\l[i] = EndianSwap32(*Chunk\l[i]) : Next
      For i = 16 To 63
        s0 = RightRotate(*Chunk\l[i-15], 7) ! RightRotate(*Chunk\l[i-15], 18) ! RightShift(*Chunk\l[i-15], 3)
        s1 = RightRotate(*Chunk\l[i-2], 17) ! RightRotate(*Chunk\l[i-2], 19) ! RightShift(*Chunk\l[i-2], 10)
        *Chunk\l[i] = *Chunk\l[i-16] + s0 + *Chunk\l[i-7] + s1
      Next
      a = *Hash\l[0] : b = *Hash\l[1] : c = *Hash\l[2] : d = *Hash\l[3]
      e = *Hash\l[4] : f = *Hash\l[5] : g = *Hash\l[6] : h = *Hash\l[7]
      For i = 0 To 63
        s0 = RightRotate(a, 2) ! RightRotate(a, 13) ! RightRotate(a, 22)
        maj = (a & b) ! (a & c) ! (b & c)
        t2 = S0 + maj
        s1 = RightRotate(e, 6) ! RightRotate(e, 11) ! RightRotate(e, 25)
        ch = (e & f) ! ((~ e) & g)
        t1 = h + S1 + ch + *k\l[i] + *Chunk\l[i]
        h = g : g = f : f = e : e = d + t1
        d = c : c = b : b = a : a = t1 + t2
      Next
      *Hash\l[0] + a : *Hash\l[1] + b : *Hash\l[2] + c : *Hash\l[3] + d
      *Hash\l[4] + e : *Hash\l[5] + f : *Hash\l[6] + g : *Hash\l[7] + h
    Next
    FreeMemory(*Chunk)
    For i = 0 To 7 : *Hash\l[i] = EndianSwap32(*Hash\l[i]) : Next
    ProcedureReturn *Hash
  EndProcedure
  
  Procedure.i SHA256String(String.s, Format.i = #PB_Ascii)
    Protected Size.i, *Memory, *Hash
    Size = StringByteLength(String, Format)
    *Memory = AllocateMemory(Size + 1)
    PokeS(*Memory, String, -1, Format)
    *Hash = SHA256(*Memory, Size)
    FreeMemory(*Memory)
    ProcedureReturn *Hash
  EndProcedure
  
  Macro HexByte(Hash)
    RSet(Hex(PeekA(Hash), #PB_Ascii), 2, "0")
  EndMacro
  
  Macro HexLong(Hash)
    HexByte(Hash) + HexByte(Hash+1) + HexByte(Hash+2) + HexByte(Hash+3)
  EndMacro
  
  Macro HexSHA256(Hash)
    HexLong(Hash) + HexLong(Hash+4) + HexLong(Hash+8) + HexLong(Hash+12) + HexLong(Hash+16) + HexLong(Hash+20) + HexLong(Hash+24) + HexLong(Hash+28)
  EndMacro
  
  Procedure.s SHA256Fingerprint(*Memory, Size.q)
    Protected *Hash, Hash.s
    *Hash = SHA256(*Memory, Size)
    Hash = HexSHA256(*Hash)
    FreeMemory(*Hash)
    ProcedureReturn Hash
  EndProcedure
  
  Procedure.s SHA256StringFingerprint(String.s, Format.i = #PB_Ascii)
    Protected *Hash, Hash.s
    *Hash = SHA256String(String, Format)
    Hash = HexSHA256(*Hash)
    FreeMemory(*Hash)
    ProcedureReturn Hash
  EndProcedure
  
EndModule
CompilerIf #PB_Compiler_IsMainFile
  
  EnableExplicit
  
  Define String.s, *Hash
  
  String = ""
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
  
  Debug "---------------------------------------------------------------------"
  
  String = "The quick brown fox jumps over the lazy dog"
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592")
  
  Debug "---------------------------------------------------------------------"
  
  String = "The quick brown fox jumps over the lazy dog."
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("ef537f25c895bfa782526529a9b63d97aa631564d5d789c2b765448c8635fb6c")
  
  Debug "---------------------------------------------------------------------"
  
  String = "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern"
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8")
  
  Debug "---------------------------------------------------------------------"
  
  String = "Frank jagt im komplett verwahrlosten Taxi quer durch Bayern"
  Debug SHA256::SHA256StringFingerprint(String, #PB_Ascii)
  Debug UCase("78206a866dbb2bf017d8e34274aed01a8ce405b69d45db30bafa00f5eeed7d5e")
  
CompilerEndIf
; ==================================================================================================
PureBasic 6.21 (Windows x86/x64) | Windows11 Pro x64 | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | GeForce RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
Christian+
Beiträge: 213
Registriert: 13.07.2008 10:05
Computerausstattung: Windows 8.1 Pro
AMD Phenom II X4 955 @ 3.2 GHz
4GB RAM
NVIDIA GeForce GTX 660

Re: SHA256

Beitrag von Christian+ »

Schön dass es auf meine neue Version Feedback gibt scheint als interessiert ein SHA256 doch einige.
Wenn ich heute noch dazu komme pass ich das mal noch so an dass es auch möglich ist Speicherbereiche Stück für Stück und komplette Files zu verarbeiten. Dann gibt es ein schönes Modul zu SHA256 das die Gleichen Funktionen wie bei den von Pure Basic nativ unterstützten Hash Funktionen hat.

@ts-soft Danke für den Hinweis auf die Module das so zu verpacken hatte ich auch schon überlegt wusste aber nicht wie viele die Beta nutzen.

@Bisonte Danke für die Anpassung und den schönen Kommentar am Anfang, so was sollte ich mir auch angewöhnen immer gleich einzufügen.
Windows 8.1 Pro 64Bit | AMD Phenom II X4 955 @ 3.2 GHz | 4GB RAM | NVIDIA GeForce GTX 660
Christian+
Beiträge: 213
Registriert: 13.07.2008 10:05
Computerausstattung: Windows 8.1 Pro
AMD Phenom II X4 955 @ 3.2 GHz
4GB RAM
NVIDIA GeForce GTX 660

Re: SHA256

Beitrag von Christian+ »

So jetzt habe ich die fehlenden Funktionen eingebaut sollte jetzt alles abdecken können wozu man einen SHA256 brauchen kann.
Zuletzt geändert von Christian+ am 09.08.2013 17:32, insgesamt 1-mal geändert.
Windows 8.1 Pro 64Bit | AMD Phenom II X4 955 @ 3.2 GHz | 4GB RAM | NVIDIA GeForce GTX 660
Benutzeravatar
Bisonte
Beiträge: 2465
Registriert: 01.04.2007 20:18

Re: SHA256

Beitrag von Bisonte »

Den "Header" lass ich mir automatisch erstellen mit meinem IDE Tool : http://purebasic.fr/german/viewtopic.ph ... 50#p304750
PureBasic 6.21 (Windows x86/x64) | Windows11 Pro x64 | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | GeForce RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
walbus
Beiträge: 137
Registriert: 03.03.2013 20:50

Re: SHA256

Beitrag von walbus »

:)
Zuletzt geändert von walbus am 08.02.2015 23:33, insgesamt 1-mal geändert.
Benutzeravatar
kernadec
Beiträge: 25
Registriert: 05.07.2009 17:51

Re: SHA256

Beitrag von kernadec »

Hallo,
Christian+ , walbus; Vielen Dank für Ihre Programme :allright:
@walbus , Haben Sie sich Gedanken über die Möglichkeit einer externen Datei für Übersetzung
Schriften von Gadgets und Dialoge in anderen Sprachen
danke
Christian+
Beiträge: 213
Registriert: 13.07.2008 10:05
Computerausstattung: Windows 8.1 Pro
AMD Phenom II X4 955 @ 3.2 GHz
4GB RAM
NVIDIA GeForce GTX 660

Re: SHA256

Beitrag von Christian+ »

Ich werde mal schauen ob ich das Speed technisch noch etwas optimieren kann. Im Bereich der Geschwindigkeit sind Assembler Lösungen natürlich im Vorteil gegenüber meiner.

@walbus
Schön, dass du meine Funktion gut gebrauchen kannst dafür veröffentliche ich es ja hier.
Über eine Erwähnung wenn jemand Code von mir nutzt den ich hier im Forum veröffentliche freue ich mich natürlich auch immer, von daher darfst du mich gerne in der Hilfe erwähnen.
Windows 8.1 Pro 64Bit | AMD Phenom II X4 955 @ 3.2 GHz | 4GB RAM | NVIDIA GeForce GTX 660
Antworten