1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| <%
function tohex(value)
value = clng(value)
tohex = lcase(hex(value))
do while len(tohex) < 8
tohex = "0" & tohex
loop
end function
function tobin(value)
dim hexstr
hexstr = tohex(value)
tobin = ""
for i = 1 to len(hexstr)
select case mid(hexstr, i, 1)
case "f", "F"
tobin = tobin & "1111"
case "e", "E"
tobin = tobin & "1110"
case "d", "D"
tobin = tobin & "1101"
case "c", "C"
tobin = tobin & "1100"
case "b", "B"
tobin = tobin & "1011"
case "a", "A"
tobin = tobin & "1010"
case "9"
tobin = tobin & "1001"
case "8"
tobin = tobin & "1000"
case "7"
tobin = tobin & "0111"
case "6"
tobin = tobin & "0110"
case "5"
tobin = tobin & "0101"
case "4"
tobin = tobin & "0100"
case "3"
tobin = tobin & "0011"
case "2"
tobin = tobin & "0010"
case "1"
tobin = tobin & "0001"
case else
tobin = tobin & "0000"
end select
next
end function
function bin2dec(binstr)
dim flip
flip = false
bin2dec = clng(0)
if left(binstr,1) = "0" then
for i = 1 to len(binstr)
if mid(binstr,33-i,1)="1" then
bin2dec = bin2dec + (2^(i-1))
end if
next
else
for i = 1 to len(binstr)
if flip then
if mid(binstr,33-i,1)="0" then
bin2dec = bin2dec - (2^(i-1))
end if
else
if mid(binstr,33-i,1)="1" then
bin2dec = bin2dec - (2^(i-1))
flip = true
end if
end if
next
end if
end function
function rshift(value,count)
dim binstr
if count >= 32 then
binstr = ""
elseif count > 0 then
binstr = left(tobin(value), 32-count)
else
binstr = tobin(value)
end if
do while len(binstr) < 32
binstr = "0" + binstr
loop
rshift = bin2dec(binstr)
end function
function lshift(value,count)
dim binstr
if count >= 32 then
binstr = ""
elseif count > 0 then
binstr = right(tobin(value), 32-count)
else
binstr = tobin(value)
end if
do while len(binstr) < 32
binstr = binstr + "0"
loop
lshift = bin2dec(binstr)
end function
function rol(value,count)
rol = lshift(value,count) or rshift(value,32-count)
end function
function add32(a, b)
dim bina, binb, total, result, carry
bina = tobin(a)
binb = tobin(b)
result = ""
carry = "0"
for i = 1 to 32
total = 0
if mid(bina,33-i,1)="1" then total = total + 1
if mid(binb,33-i,1)="1" then total = total + 1
if carry="1" then total = total + 1
select case total
case 3
carry = "1"
result = "1" + result
case 2
carry = "1"
result = "0" + result
case 1
carry = "0"
result = "1" + result
case else
carry = "0"
result = "0" + result
end select
next
add32 = bin2dec(result)
end function
function f(b,c,d,t)
if t < 20 then
f = (b and c) or ((not b) and d)
elseif t < 40 then
f = b xor c xor d
elseif t < 60 then
f = (b and c) or (b and d) or (c and d)
else
f = b xor c xor d
end if
end function
function k(t)
if t < 20 then
k = clng(1518500249)
elseif t < 40 then
k = clng(1859775393)
elseif t < 60 then
k = clng(-1894007588)
else
k = clng(-899497514)
end if
end function
function pad(message)
dim l, n
l = len(message)
n = (((l+8) \ 64) + 1)*16
redim m(n-1)
for i = 0 to n-1
m(i) = clng(0)
next
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)
dim h0, h1, h2, h3, h4
dim a, b, c, d, e, temp
dim l, n
dim w(79)
l = len(message)
n = (((l+8) \ 64) + 1)*16
m = pad(message)
h0 = clng(1732584193)
h1 = clng(-271733879)
h2 = clng(-1732584194)
h3 = clng(271733878)
h4 = clng(-1009589776)
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 = tohex(h0)+tohex(h1)+tohex(h2)+tohex(h3)+tohex(h4)
end function
%> |
Partager