'SHA1: Option Public Use "CoreHashLibrary" '********************************************************* '* Copyright (c) 2008 Tom O'Neil '* See disclaimer/Copyright information in CoreHashLibrary.lss '* '* This library has two classes: SHA1(text) and HMAC_SHA1(key,text) '* depending on the hash you wish to create. '* '* Each class has toHex,toStr, and toB64 public functions for output '* '********************************************************* Class SHA1 As HashHelper Private text As String Sub New(text1 As String) text = text1 End Sub Private Function Hash (m,len1 As Long) As Variant Dim h0 As Long, h1 As Long, h2 As Long, h3 As Long, h4 As Long Dim a As Long, b As Long, c As Long, d As Long, e As Long Dim temp As Long Dim l As Integer, n As Integer,length As Integer, y As Integer Dim block As Integer, t As Long Dim w(0 To 79) As Long Dim tempblanks() ' Padding m If (Ubound(m) < 31) Then Redim x(31 - Ubound(m) - 1) m = Arrayappend(m,x) End If m(RShift(len1,5)) = m(RShift(len1,5)) Or LShift(&h80,(24 - len1 Mod 32)) m(LShift(RShift(len1 + 64,9),4) + 15) = len1 h0 = &H67452301 '** 1732584193 in decimal h1 = &HEFCDAB89 '** -271733879 in decimal h2 = &H98BADCFE '** -1732584194 in decimal h3 = &H10325476 '** 271733878 in decimal h4 = &HC3D2E1F0 '** -1009589776 in decimal length = 0 For y = 0 To Ubound(m) If m(y) > 0 Then length = y End If Next For block = 0 To length Step 16 a = h0 b = h1 c = h2 d = h3 e = h4 For t = 0 To 79 If t < 16 Then w(t) = m(block + t) Else w(t) = rol(w(t-3) Xor w(t-8) Xor w(t-14) Xor w(t-16),1) End If temp = add32(rol(a,5), add32(f(b,c,d,t), add32(e, add32(w(t),k(t))))) e = d d = c c = rol(b,30) b = a a = temp Next h0 = add32(h0, a) h1 = add32(h1, b) h2 = add32(h2, c) h3 = add32(h3, d) h4 = add32(h4, e) Next Dim returnArray(4) returnArray(0) = h0 returnArray(1) = h1 returnArray(2) = h2 returnArray(3) = h3 returnArray(4) = h4 Hash = returnArray End Function Function toB64() As String toB64 = binb2b64(Hash(pad(text),Len(text)*chrsz)) End Function Function toHEX() As String toHEX = binb2hex(Hash(pad(text),Len(text)*chrsz)) End Function Function toSTR() As String toSTR = binb2str(Hash(pad(text),Len(text)*chrsz)) End Function End Class Class HMAC_SHA1 As SHA1 Private key As String Sub new(key1 As String,text1 As String) , SHA1(text1) key = key1 End Sub Private Function core_HMAC() As Variant Dim x As Integer Dim y As Integer Dim returnval As Variant Dim firstHash As Variant Dim bkey As Variant bkey = pad(key) If (Ubound(bkey) > 15) Then bkey = Hash(bkey,Len(key) * chrsz) End If Dim ipad(15) Dim opad(15) For y=0 To 15 ipad(y) = &h36363636 opad(y) = &h5C5C5C5C Next For x = 0 To 15 ipad(x) = ipad(x) Xor bkey(x) opad(x) = opad(x) Xor bkey(x) Next firstHash = Hash(Arrayappend(ipad,pad(text)),512 + Len(text) * chrsz) returnval = Hash(Arrayappend(opad,firstHash), 512 + 160) core_HMAC = returnval End Function Function toB64() As String toB64 = binb2b64(core_HMAC()) End Function Function toHEX() As String toHEX = binb2hex(core_HMAC()) End Function Function toSTR() As String toSTR = binb2str(core_HMAC()) End Function End Class