Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SHA1 and TOTP
#19
(08-23-2024, 06:30 AM)DSMan195276 Wrote:
(08-23-2024, 12:43 AM)Ra7eN Wrote: It produced codes but not the correct one. I put my secretkey in FFXIV for example, and kept failing, went back to the python/qb64 worked perfect

I switched the following:
Code: (Select All)

key$ = "Hello!" + CHR$(&HDE) + CHR$(&HAD) + CHR$(&HBE) + CHR$(&HEF)

to
Code: (Select All)

key$ = "1234MYSECRETKEY567890" + CHR$(&HDE) + CHR$(&HAD) + CHR$(&HBE) + CHR$(&HEF)

again, codes kept popping up like normal. but they were not correct

Sorry, it's not super clear from your example, is `1234MYSECRETKEY567890` the base32 version or the binary version of your key? The C code does not do the decoding, so you have to provide it the decoded binary key.

Ex. The actual key for my test was `JBSWY3DPEHPK3PXP`, which when decoded as base32 produces the "Hello!" plus DEADBEEF string that I put in `key$`. The chances are high that your key will not be ASCII like my example one when decoded, so your key will just be a series of `CHR$()`s.
Ah good point, I did not decode the key first, I just ran the code as is. ' 1234MYSECRETKEY567890 ' this was just the represent my FFXIV key. As it did not work. However, this month long project has actually allowed me to create my own custom hash, and almost AES quality - not quite due to the lack of HMAC_SHA1 but still very happy with it. Will rewrite some of my code to accept this.
The TOTP feature, I found that I can just run a portable python and include it with my distro.; So it will run faily seamlessly. QB64 is just too challenging for me to get a TOTP. The following is almost exactly as per RFC 3174 . But for some reason, it will toss up a a code, but will not be correct.
But I will put it here for anyone else to take a stab at it. I am TOTALLY  exhausted. Its all per RFC 3174 , but yet qb will not throw the right code.

Code: (Select All)

Option _Explicit

Dim shstr As String
shstr$ = SHA1("abc")

Print shstr
'for debug copying to textfile since I can't copy from terminal window
Open "sha1.txt" For Output As #1
Print #1, shstr$
Close #1
End


Dim Shared hA As _Unsigned Long
Dim Shared hB As _Unsigned Long
Dim Shared hC As _Unsigned Long
Dim Shared hD As _Unsigned Long
Dim Shared hE As _Unsigned Long

Sub InitializeHashValues
    hA = &H67452301
    hB = &HEFCDAB89
    hC = &H98BADCFE
    hD = &H10325476
    hE = &HC3D2E1F0
End Sub


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

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

    While (Len(message) * 8) Mod 512 <> 448
        message = message + Chr$(0) ' Append '0' bits
    Wend

    Dim highPart As _Unsigned Long, lowPart As _Unsigned Long
    highPart = 0 ' For messages shorter than 2^32 bits, highPart is 0
    lowPart = messageLength And &HFFFFFFFF

    Dim lengthBytes As String

lengthBytes = CHR$((lowPart \ &H1000000) AND &HFF) + _
              CHR$((lowPart \ &H10000) AND &HFF) + _
              CHR$((lowPart \ &H100) AND &HFF) + _
              CHR$(lowPart AND &HFF)

    ' Ensure each part is correctly interpreted as 8 bits
  ' lengthBytes = Right$("00000000" + Hex$(Val(lengthBytes)), 8)
    message = message + String$(4, 0) + lengthBytes ' Append length

    PadMessage$ = message
End Function



Sub ProcessBlock (block As String)
    Dim i As _Unsigned Long
    Dim t As _Unsigned Long
    Dim TEMP As _Unsigned Long
    Dim W(79) As _Unsigned Long
    For i = 0 To 15
        W(i) = CVL(Mid$(block, i * 4 + 1, 4))
    Next

    For t = 16 To 79
        W(t) = _RoL(W(t - 3) Xor W(t - 8) Xor W(t - 14) Xor W(t - 16), 1)
    Next

    Dim aTemp As _Unsigned Long, bTemp As _Unsigned Long
    Dim cTemp As _Unsigned Long, dTemp As _Unsigned Long, eTemp As _Unsigned Long
    aTemp = hA: bTemp = hB: cTemp = hC: dTemp = hD: eTemp = hE

    For t = 0 To 79
        For t = 0 To 79
            Select Case t
                Case 0 To 19
                    TEMP = (_RoL(aTemp, 5) + ((bTemp And cTemp) Or ((Not bTemp) And dTemp)) + eTemp + W(t) + &H5A827999) And &HFFFFFFFF
                Case 20 To 39
                    TEMP = (_RoL(aTemp, 5) + (bTemp Xor cTemp Xor dTemp) + eTemp + W(t) + &H6ED9EBA1) And &HFFFFFFFF
                Case 40 To 59
                    TEMP = (_RoL(aTemp, 5) + ((bTemp And cTemp) Or (bTemp And dTemp) Or (cTemp And dTemp)) + eTemp + W(t) + &H8F1BBCDC) And &HFFFFFFFF
                Case 60 To 79
                    TEMP = (_RoL(aTemp, 5) + (bTemp Xor cTemp Xor dTemp) + eTemp + W(t) + &HCA62C1D6) And &HFFFFFFFF
            End Select
        Next
        eTemp = dTemp
        dTemp = cTemp
        cTemp = _RoL(bTemp, 30)
        bTemp = aTemp
        aTemp = TEMP
    Next

    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 SHA1$ (message As String)
    ' Initialize the hash constants
    InitializeHashValues

    ' 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)
    Next

    ' Convert each part of the hash to a hexadecimal string and trim it
    Dim hashA As String, hashB As String, hashC As String, hashD As String, hashE As String
    hashA = LCase$(_Trim$(Hex$(hA)))
    hashB = LCase$(_Trim$(Hex$(hB)))
    hashC = LCase$(_Trim$(Hex$(hC)))
    hashD = LCase$(_Trim$(Hex$(hD)))
    hashE = LCase$(_Trim$(Hex$(hE)))

    ' Concatenate the final hash string explicitly
    Dim finalHash As String
    finalHash = hashA + hashB + hashC + hashD + hashE

    ' Return the concatenated and trimmed hash
    SHA1$ = finalHash
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: 8 Guest(s)