VBS implementation of 256 bit SHA2 cryptographic hash. Drag and drop a file onto the script to calculate the hash.
Option Explicit Dim i, temp(8), fraccubeprimes, hashValues
Dim done512, index512, words(64), index32, mask(4)
Dim s0, s1, t1, t2, maj, ch, sTemp
Dim oFS, sFile, oFile, sMessage mask(0) = 4294967296
mask(1) = 16777216
mask(2) = 65536
mask(3) = 256 hashValues = Array( _
1779033703, 3144134277, 1013904242, 2773480762, _
1359893119, 2600822924, 528734635, 1541459225) fraccubeprimes = Array( _
&h428a2f98, &h71374491, &hb5c0fbcf, &he9b5dba5, &h3956c25b, &h59f111f1, &h923f82a4, &hab1c5ed5, _
&hd807aa98, &h12835b01, &h243185be, &h550c7dc3, &h72be5d74, &h80deb1fe, &h9bdc06a7, &hc19bf174, _
&he49b69c1, &hefbe4786, &h0fc19dc6, &h240ca1cc, &h2de92c6f, &h4a7484aa, &h5cb0a9dc, &h76f988da, _
&h983e5152, &ha831c66d, &hb00327c8, &hbf597fc7, &hc6e00bf3, &hd5a79147, &h06ca6351, &h14292967, _
&h27b70a85, &h2e1b2138, &h4d2c6dfc, &h53380d13, &h650a7354, &h766a0abb, &h81c2c92e, &h92722c85, _
&ha2bfe8a1, &ha81a664b, &hc24b8b70, &hc76c51a3, &hd192e819, &hd6990624, &hf40e3585, &h106aa070, _
&h19a4c116, &h1e376c08, &h2748774c, &h34b0bcb5, &h391c0cb3, &h4ed8aa4a, &h5b9cca4f, &h682e6ff3, _
&h748f82ee, &h78a5636f, &h84c87814, &h8cc70208, &h90befffa, &ha4506ceb, &hbef9a3f7, &hc67178f2) sFile = WScript.Arguments(0)
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFile = oFS.OpenTextFile(sFile, 1)
Set oFS = Nothing Do Until oFile.AtEndOfStream
sMessage = oFile.Read(511) & Chr(128)
done512 = False
index512 = 0 If (Len(sMessage) Mod 64) <> 0 Then
For i = (Len(sMessage) Mod 64) To 63
sMessage = sMessage & Chr(0)
Next
End If Do Until done512
For i = 0 To 15
words(i) = Hex( "&h" & _
Right("0" & Asc(Mid(sMessage, i*4 + 1, 1)), 2) & _
Right("0" & Asc(Mid(sMessage, i*4 + 2, 1)), 2) & _
Right("0" & Asc(Mid(sMessage, i*4 + 3, 1)), 2) & _
Right("0" & Asc(Mid(sMessage, i*4 + 4, 1)), 2))
Next
For i = 16 To 63
s0 = largeXor(largeXor(rightRotate(words(i-15), 7, 32), rightRotate(words(i-15), 18, 32), 32), Int(words(i-15) / 8), 32)
s1 = largeXor(largeXor(rightRotate(words(i-2), 17, 32), rightRotate(words(i-2), 19, 32), 32), Int(words(i-2) / 1024), 32)
words(i) = words(i-16) + s0 + words(i-7) + s1
Next For i = 0 To 7
temp(i) = hashValues(i)
Next For i = 0 To 63
s0 = largeXor(largeXor(rightRotate(temp(0), 2, 32), rightRotate(temp(0), 13, 32), 32), rightRotate(temp(0), 22, 32), 32)
maj = largeXor(largeXor(largeAnd(temp(0), temp(1), 32), largeAnd(temp(0), temp(2), 32), 32), largeAnd(temp(1), temp(2), 32), 32)
t2 = s0 + maj
s1 = largeXor(largeXor(rightRotate(temp(4), 6, 32), rightRotate(temp(4), 11, 32), 32), rightRotate(temp(4), 25, 32), 32)
ch = largeXor(largeAnd(temp(4), temp(5), 32), largeAnd(largeNot(temp(4), 32), temp(6), 32), 32)
t1 = temp(7) + s1 + ch + fraccubeprimes(i) + words(i)
temp(7) = temp(6)
temp(6) = temp(5)
temp(5) = temp(4)
temp(4) = temp(3) + t1
temp(3) = temp(2)
temp(2) = temp(1)
temp(1) = temp(0)
temp(0) = t1 + t2
Next
For i = 0 To 7
hashValues(i) = largeMod(hashValues(i) + temp(i), 4294967295)
Next
If (index512 + 1) * 64 >= Len(sMessage) Then done512 = True
index512 = index512 + 1
Loop
Loop For i = 0 To 31
sTemp = sTemp & Right("0" & Hex(Int((hashValues(i\4) / mask(i Mod 4) - Int(hashValues(i\4) / mask(i Mod 4))) * 256)), 2)
Next oFile.Close
Set oFile = Nothing
MsgBox sTemp, 0, "SHA256" Function largeMod(value, modValue)
largeMod = Int((Value / modValue - int(value / modValue)) * modValue)
End Function Function rightRotate(value, amount, totalBits)
'To leftRotate, make amount = totalBits - amount
Dim i
rightRotate = 0
For i = 0 To (totalBits - 1)
If i >= amount Then
rightRotate = rightRotate + (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)) * 2 ^ (i - amount)
Else
rightRotate = rightRotate + (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)) * 2 ^ (totalBits - amount + i)
End If
Next
End Function Function largeXor(value, xorValue, totalBits)
Dim i, a, b
largeXor = 0
For i = 0 To (totalBits - 1)
a = (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2))
b = (int((xorValue / (2 ^ (i + 1)) - int(xorValue / (2 ^ (i + 1)))) * 2))
If a <> b Then
largeXor = largeXor + 2 ^ i
End If
Next
End Function Function largeOr(value, orValue, totalBits)
Dim i, a, b
largeOr = 0
For i = 0 To (totalBits - 1)
a = (int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2))
b = (int((orValue / (2 ^ (i + 1)) - int(orValue / (2 ^ (i + 1)))) * 2))
If a Or b Then
largeOr = largeOr + 2 ^ i
End If
Next
End Function Function largeNot(value, totalBits)
Dim i, a
largeNot = 0
For i = 0 To (totalBits - 1)
a = int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)
If a = 0 Then
largeNot = largeNot + 2 ^ i
End If
Next
End Function Function largeAnd(value, andValue, totalBits)
Dim i, a, b
largeAnd = 0
For i = 0 To (totalBits - 1)
a = int((value / (2 ^ (i + 1)) - int(value / (2 ^ (i + 1)))) * 2)
b = (int((andValue / (2 ^ (i + 1)) - int(andValue / (2 ^ (i + 1)))) * 2))
If a = 1 And b = 1 Then
largeAnd = largeAnd + 2 ^ i
End If
Next
End Function Function leftShift(value, amount)
leftShift = value * 2 ^ amount
End Function Function rightShift(value, amount)
rightShift = Int(value / (2 ^ amount))
End Function