Option Public
Option Explicit
Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim plaintext As String, hashtext As String
plaintext = "текст для шифрования"
hashtext = sha1(plaintext)
End Sub
Function tohex (value As Long) As String
tohex = Right(String(8, "0") & Hex(value), 8)
End Function
Function tobin (value As Long) As String
tobin = Right(String(32, "0") & Bin(value), 32)
End Function
Function bin2dec (binstr As String) As Long
bin2dec = Clng(Val("&B" & binstr & "&"))
End Function
Function rshift (value As Long, count As Integer) As Long
'** bit shift right
Dim binstr As String
binstr = Left(String(count, "0") & tobin(value), 32)
rshift = bin2dec(binstr)
End Function
Function lshift (value As Long, count As Integer) As Long
'** bit shift left
Dim binstr As String
binstr = Right(tobin(value) & String(count, "0"), 32)
lshift = bin2dec(binstr)
End Function
Function rol (value As Long, count As Integer) As Long
'** circular left-shift
Dim binstr As String
binstr = tobin(value)
rol = bin2dec(Right(binstr, 32-count) & Left(binstr, count))
End Function
Function add32 (a As Long, b As Long) As Long
'** 2's complement addition, returning only the first 32-bits of the sum
'** (this version is from Damien Katz's BitOperations library)
If ((a Eqv b) And &h80000000&) Then
add32 = ((&h80000000& Xor a) + b) Xor &h80000000&
Else
add32 = a + b
End If
End Function
Function f (b As Long, c As Long, d As Long, t As Long) As Long
Select Case t
Case Is < 20 :
f = (b And c) Or ((Not b) And d)
Case Is < 40 :
f = b Xor c Xor d
Case Is < 60 :
f = (b And c) Or (b And d) Or (c And d)
Case Else :
f = b Xor c Xor d
End Select
End Function
Function k (t As Long) As Long
Select Case t
Case Is < 20 :
k = &H5A827999 '** 1518500249 in decimal
Case Is < 40 :
k = &H6ED9EBA1 '** 1859775393 in decimal
Case Is < 60 :
k = &H8F1BBCDC '** -1894007588 in decimal
Case Else :
k = &HCA62C1D6 '** -899497514 in decimal
End Select
End Function
Function pad (message As String) As Variant
Dim l As Integer, n As Integer, i As Integer
l = Len(message)
n = (((l+8) \ 64) + 1)*16
Redim m(0 To n-1) As Long
For i = 0 To l-1
m(i\4) = m(i\4) Or lshift(Asc(Mid(message, i+1, 1)), (24 - (i Mod 4) * 8))
Next
m(l\4) = m(l\4) Or lshift(Clng(128), (24 - (l Mod 4) * 8))
m(n-1) = l * 8
pad = m
End Function
Function sha1 (message As String) As String
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
Dim m As Variant
Dim block As Integer, t As Long
Dim w(0 To 79) As Long
l = Len(message)
n = (((l+8) \ 64) + 1)*16
m = pad(message)
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
For block = 0 To n-1 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
sha1 = Lcase(tohex(h0) & tohex(h1) & tohex(h2) & tohex(h3) & tohex(h4))
End Function