SHA-1
Algorithm creator(s)
NIST and NSA
PB author(s)
Semen Matusovski
Description
Creates a 160-bit hash (message digest) from messages up to 2^64 bits
Note
Produces a message digest of 160 bits, providing no more than 80 bits of security against collision attacks.
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23996-sha-1?t=23371
See also
n/a
Source Code
Download source code file sha-1.bas (Right-click -> "Save as ...")
#Compile Exe
#Register None
#Dim All
#Include "Win32Api.Inc"
$FileNm = "Win322.hlp"
Type SHA
H0 As Long
H1 As Long
H2 As Long
H3 As Long
H4 As Long
End Type
Function CalcSHA (Str As String, SHA As SHA) As Long
#Register None
Dim lStr As Long, nq As Long, n As Long, adrW As Long, adrWW As Long
Dim H0 As Long, H1 As Long, H2 As Long, H3 As Long, H4 As Long, W(0 : 79) As Long
Dim A As Long, B As Long, C As Long, D As Long, E As Long, TEMP As Long
lStr = Len(Str)
nq = Fix((lStr + 8) / 64) + 1
n = 16 * nq
ReDim WW(0 To n - 1) As Long
WW(n - 1) = lStr * 8
adrW = VarPtr(W(0))
adrWW = VarPtr(WW(0))
A = StrPtr(Str)
! PUSH EDI
! PUSH ESI
! MOV EDI, adrWW
! MOV ESI, A
! MOV ECX, lStr
! REP MOVSB
! MOV CL, &H80
! MOV [EDI], CL
! MOV EDI, adrWW
! MOV ECX, 2
CalcSHA_Lbl1:
! MOV AX, [EDI]
! MOV DX, [EDI + 2]
! MOV [EDI], DH
! MOV [EDI + 1], DL
! MOV [EDI + 2], AH
! MOV [EDI + 3], AL
! ADD EDI, 4
! INC ECX
! CMP ECX, n
! JNE CalcSHA_Lbl1
! MOV H0, &H67452301&
! MOV H1, &HEFCDAB89&
! MOV H2, &H98BADCFE&
! MOV H3, &H10325476&
! MOV H4, &HC3D2E1F0&
CalcSHA_Lbl2:
! MOV EDI, adrW
! MOV ESI, adrWW
! MOV ECX, 64
! REP MOVSB
! MOV adrWW, ESI
! MOV ECX, 0
CalcSHA_Lbl3:
! MOV ESI, ECX
! ADD ESI, ESI
! ADD ESI, ESI
! ADD ESI, adrW
! MOV EAX, [ESI + 52]
! XOR EAX, [ESI + 32]
! XOR EAX, [ESI + 8]
! XOR EAX, [ESI]
! MOV EDX, EAX
! SHL EAX, 1
! SHR EDX, 31
! OR EAX, EDX
! MOV [ESI + 64], EAX
! INC ECX
! CMP ECX, 64
! JNE CalcSHA_Lbl3
! MOV EAX, H0
! MOV A, EAX
! MOV EAX, H1
! MOV B, EAX
! MOV EAX, H2
! MOV C, EAX
! MOV EAX, H3
! MOV D, EAX
! MOV EAX, H4
! MOV E, EAX
! MOV EDI, 0
CalcSHA_Lbl4:
! CMP EDI, 19
! JA CalcSHA_Lbl5
! MOV ECX, B
! AND ECX, C
! MOV EAX, B
! NOT EAX
! AND EAX, D
! OR ECX, EAX
! ADD ECX, &H5A827999&
! JMP CalcSHA_Lbl8
CalcSHA_Lbl5:
! CMP EDI, 39
! JA CalcSHA_Lbl6
! MOV ECX, B
! XOR ECX, C
! XOR ECX, D
! ADD ECX, &H6ED9EBA1&
! JMP CalcSHA_Lbl8
CalcSHA_Lbl6:
! CMP EDI, 59
! JA CalcSHA_Lbl7
! MOV EAX, B
! AND EAX, C
! MOV ECX, B
! AND ECX, D
! MOV EDX, C
! AND EDX, D
! OR ECX, EAX
! OR ECX, EDX
! ADD ECX, &H8F1BBCDC&
! JMP CalcSHA_Lbl8
CalcSHA_Lbl7:
! MOV ECX, B
! XOR ECX, C
! XOR ECX, D
! ADD ECX, &HCA62C1D6&
CalcSHA_Lbl8:
! MOV EAX, A
! MOV EDX, EAX
! SHL EAX, 5
! SHR EDX, 27
! OR EAX, EDX
! ADD EAX, E
! ADD ECX, EAX
! MOV ESI, EDI
! ADD ESI, ESI
! ADD ESI, ESI
! ADD ESI, adrW
! MOV ESI, [ESI]
! MOV TEMP, ESI
! ADD Temp, ECX
! MOV EAX, D
! MOV E, EAX
! MOV EAX, C
! MOV D, EAX
! MOV EAX, B
! MOV EDX, EAX
! SHL EAX, 30
! SHR EDX, 2
! OR EAX, EDX
! MOV C, EAX
! MOV EAX, A
! MOV B, EAX
! MOV EAX, TEMP
! MOV A, EAX
! INC EDI
! CMP EDI, 80
! JNE CalcSHA_Lbl4
! MOV EAX, A
! ADD H0, EAX
! MOV EAX, B
! ADD H1, EAX
! MOV EAX, C
! ADD H2, EAX
! MOV EAX, D
! ADD H3, EAX
! MOV EAX, E
! ADD H4, EAX
! SUB nq, 1
! JNE CalcSHA_Lbl2
! POP ESI
! POP EDI
SHA.H0 = H0: SHA.H1 = H1: SHA.H2 = H2: SHA.H3 = H3: SHA.H4 = H4
End Function
Function tSHA (SHA As SHA) As String
Function = Hex$(SHA.H0, 8) + " " + Hex$(SHA.H1, 8) + " " + Hex$(SHA.H2, 8) + " " + _
Hex$(SHA.H3, 8) + " " + Hex$(SHA.H4, 8)
End Function
Function PbMain
Dim SHA As SHA
' Control samples from official document
CalcSHA "abc", SHA
If tSHA(SHA) <> "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D" Then MsgBox "Error-1"
CalcSHA "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", SHA
If tSHA(SHA) <> "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1" Then MsgBox "Error-2"
' Test of speed
Dim R As String, Tmp As String, t1 As Single, t2 As Single
Open $FileNm For Binary As #1
Tmp = Space$(Lof(1)): Get$ #1, Lof(1), Tmp: Close #1
t1 = Timer: CalcSHA Tmp, SHA: t2 = Timer
MsgBox Format$(Len(Tmp) / 1024 / (t2 - t1), "# Kb per second")
End Function