Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SHA1 and TOTP
#9
OK here is another attempt at sha1. it prints a code but comparing against other sha1 encoders I get the following:

"The quick brown fox jumps over the lazy dog"
official SHA1 encoder result:
2fd4e1c67a2d28fced849ee1bb76e7391b93eb12

Mine
324CE301DEF8AB8998BADCFE10325476C3D2E1F0

I am still looking to see if A..E are the right numbers to use
or it could just be some hacks I used to make qb64 work LOL!!!

Jump in, sing along!! This is the easy one, after this gonna have to figure out how to get HMAC_SHA1 required by TOTP.
why QB64 - BECAUSE!


Code: (Select All)

'SHA-1 uses five initial constants:
' not sure if these are the corectvalues
Dim A As _Unsigned Long
Dim B As _Unsigned Long
Dim C As _Unsigned Long
Dim D As _Unsigned Long
Dim E As _Unsigned Long
A = &H67452301
B = &HEFCDAB89
C = &H98BADCFE
D = &H10325476
E = &HC3D2E1F0


Print SHA1("The quick brown fox jumps over the lazy dog")
'for debug copying
Open "sha1.txt" For Output As #1
Print #1, SHA1("The quick brown fox jumps over the lazy dog")
Close
End


Function PadMessage$ (message As String)
    'SHA-1 requires that the input message be padded to a length that
    'is a multiple of 512 bits (64 bytes). The padding is done in two steps:
    'Append a single '1' bit followed by '0' bits until the message is 64 bits shy of being a multiple of 512 bits.
    'Append the original length of the message as a 64-bit integer.

    Dim messageLength As _Unsigned Long
    messageLength = Len(message) * 8 ' Length in bits

    ' Append a '1' bit (0x80 in hex, or 128 in decimal)
    message = message + Chr$(128)

    ' Append '0' bits until the length is 64 bits shy of a multiple of 512
    While (Len(message) * 8) Mod 512 <> 448
        message = message + Chr$(0)
    Wend

    ' Append the original length of the message as a 64-bit big-endian integer
    Dim lengthBytes As String
    For i = 7 To 0 Step -1
        lengthBytes = lengthBytes + Chr$(ShiftRight(messageLength, i * 8) And 255)
    Next
    message = message + lengthBytes
    PadMessage$ = message
End Function

'QB64 does'nt support a shift right function SHR so here is a simple function
Function ShiftRight (value As _Unsigned Long, positions As Integer)
    ShiftRight = Int(value / (2 ^ positions))
End Function

'QB64 NO rotate left. so here is a function that shold do it.
Function RotateLeft (value As _Unsigned Long, shift As Integer)
    RotateLeft = ((value * 2 ^ shift) Or (value \ 2 ^ (32 - shift))) And &HFFFFFFFF
End Function


Sub ProcessBlock (block As String, hA As _Unsigned Long, hB As _Unsigned Long, hC As _Unsigned Long, hD As _Unsigned Long, hE As _Unsigned Long)
    Dim W(79) As _Unsigned Long
    Dim tempValue As _Unsigned Long

    ' Break the block into 16 words (32 bits each)
    For i = 0 To 15
        W(i) = CVL(Mid$(block, i * 4 + 1, 4))
    Next

    ' Expand the 16 words into 80 words
    For i = 16 To 79
        tempValue = W(i - 3) Xor W(i - 8) Xor W(i - 14) Xor W(i - 16)
        W(i) = RotateLeft(tempValue, 1)
    Next

    ' Initialize the hash value for this block
    Dim aTemp As _Unsigned Long, bTemp As _Unsigned Long, cTemp As _Unsigned Long, dTemp As _Unsigned Long, eTemp As _Unsigned Long
    aTemp = hA: bTemp = hB: cTemp = hC: dTemp = hD: eTemp = hE

    ' Main loop
    For i = 0 To 79
        Select Case i
            Case 0 To 19
                tempValue = (RotateLeft(aTemp, 5) + ((bTemp And cTemp) Or ((Not bTemp) And dTemp)) + eTemp + W(i) + &H5A827999) And &HFFFFFFFF
            Case 20 To 39
                tempValue = (RotateLeft(aTemp, 5) + (bTemp Xor cTemp Xor dTemp) + eTemp + W(i) + &H6ED9EBA1) And &HFFFFFFFF
            Case 40 To 59
                tempValue = (RotateLeft(aTemp, 5) + ((bTemp And cTemp) Or (bTemp And dTemp) Or (cTemp And dTemp)) + eTemp + W(i) + &H8F1BBCDC) And &HFFFFFFFF
            Case 60 To 79
                tempValue = (RotateLeft(aTemp, 5) + (bTemp Xor cTemp Xor dTemp) + eTemp + W(i) + &HCA62C1D6) And &HFFFFFFFF
        End Select

        eTemp = dTemp
        dTemp = cTemp
        cTemp = RotateLeft(bTemp, 30)
        bTemp = aTemp
        aTemp = tempValue
    Next

    ' Add this block's hash to the result so far:
    hA = (hA + aTemp) And &HFFFFFFFF
    hB = (hB + bTemp) And &HFFFFFFFF
    hC = (hC + cTemp) And &HFFFFFFFF
    hD = (hD + dTemp) And &HFFFFFFFF
    hE = (hE + eTemp) And &HFFFFFFFF
End Sub

Function ToHexString$ (value As _Unsigned Long)
    Dim hexStr As String
    hexStr = Hex$(value)

    ' Pad with leading zeros if necessary to ensure 8 characters
    If Len(hexStr) < 8 Then
        hexStr = String$(8 - Len(hexStr), "0") + hexStr
    End If

    ToHexString$ = hexStr
End Function


Function SHA1$ (message As String)
    ' Initialize the hash constants
    Dim hA As _Unsigned Long
    Dim hB As _Unsigned Long
    Dim hC As _Unsigned Long
    Dim hD As _Unsigned Long
    Dim hE As _Unsigned Long
    hA = &H67452301
    hB = &HEFCDAB89
    hC = &H98BADCFE
    hD = &H10325476
    hE = &HC3D2E1F0

    ' Preprocess the message (pad it)
    message = PadMessage(message)

    ' Process each 512-bit block
    Dim i As Long
    For i = 1 To Len(message) Step 64
        ProcessBlock Mid$(message, i, 64), hA, hB, hC, hD, hE
    Next

    ' Produce the final hash value (concatenate A, B, C, D, E)
    SHA1$ = Hex$(hA) + Hex$(hB) + Hex$(hC) + Hex$(hD) + Hex$(hE)
End Function
3 out of 2 people have trouble with fractions

Reply


Messages In This Thread
SHA1 and TOTP - by Ra7eN - 08-21-2024, 01:35 AM
RE: SHA1 and TOTP - by TerryRitchie - 08-21-2024, 01:48 AM
RE: SHA1 and TOTP - by Ra7eN - 08-21-2024, 02:13 AM
RE: SHA1 and TOTP - by SMcNeill - 08-21-2024, 03:47 AM
RE: SHA1 and TOTP - by RhoSigma - 08-21-2024, 10:44 AM
RE: SHA1 and TOTP - by DSMan195276 - 08-21-2024, 04:19 AM
RE: SHA1 and TOTP - by Ra7eN - 08-21-2024, 05:14 PM
RE: SHA1 and TOTP - by Ra7eN - 08-21-2024, 09:44 PM
RE: SHA1 and TOTP - by Ra7eN - 08-22-2024, 12:01 AM
RE: SHA1 and TOTP - by Ra7eN - 08-22-2024, 12:09 AM
RE: SHA1 and TOTP - by Ra7eN - 08-22-2024, 12:25 AM
RE: SHA1 and TOTP - by SpriggsySpriggs - 08-22-2024, 11:29 AM
RE: SHA1 and TOTP - by Ra7eN - 08-22-2024, 11:37 AM
RE: SHA1 and TOTP - by DSMan195276 - 08-22-2024, 12:13 PM
RE: SHA1 and TOTP - by Ra7eN - 08-22-2024, 11:41 PM
RE: SHA1 and TOTP - by DSMan195276 - 08-23-2024, 12:32 AM
RE: SHA1 and TOTP - by Ra7eN - 08-23-2024, 12:43 AM
RE: SHA1 and TOTP - by DSMan195276 - 08-23-2024, 06:30 AM
RE: SHA1 and TOTP - by Ra7eN - 08-24-2024, 12:38 AM
RE: SHA1 and TOTP - by Kernelpanic - 08-24-2024, 08:37 PM



Users browsing this thread: 2 Guest(s)