Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

SHA-1

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SHA-1

    Speed is not so nice, like Florent's RIPEMD
    (on my PC - PIII-550, Win2000) - about 8,2 Mb/sec)
    Pls, let me know, if something wrong or you have suggestions.

    Code:
       #Compile Exe
       #Register None
       #Dim All
       #Include "Win32Api.Inc"
    
       $FileNm = "C:\Win32Api\Win322.hlp"  ' <------ Change
    
       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
    [This message has been edited by Semen Matusovski (edited November 03, 2000).]

  • #2
    Thank you for your code for the sha1 encoding.
    We want to use Sha1 for password encoding as per Netscape. But
    apparently you have to be able to specific the 'salt' bytes for
    the encoding sequence. I believe that the salt bytes should be
    the first 4 characters of password.

    I was wondering if you have a version of your code where we can
    specific the salt characters for encryption.

    Thanks

    ------------------

    Comment


    • #3
      Martin --
      Pls, read SHA-1 specification and purpose in http://www.itl.nist.gov/fipspubs/fip180-1.htm
      BTW, if you want to discuss, you need to post new topic in Programming forum (something like Re SHA-1)

      ------------------
      E-MAIL: matus@perevozki.ru

      Comment

      Working...
      X