Hash sha1 è una codifica di una stringa di testo, qualcosa del tipo “e40c090ab42eea0b66ffd5f179823e0f62046464”
Se lavorate in ASP questo è il codice da integrare nella pagina.
<%
Function AndW(ByRef pBytWord1Ary, ByRef pBytWord2Ary)
Dim lBytWordAry(3)
Dim lLngIndex
For lLngIndex = 0 To 3
lBytWordAry(lLngIndex) = CByte(pBytWord1Ary(lLngIndex) And pBytWord2Ary(lLngIndex))
Next
AndW = lBytWordAry
End Function
Function OrW(ByRef pBytWord1Ary, ByRef pBytWord2Ary)
Dim lBytWordAry(3)
Dim lLngIndex
For lLngIndex = 0 To 3
lBytWordAry(lLngIndex) = CByte(pBytWord1Ary(lLngIndex) Or pBytWord2Ary(lLngIndex))
Next
OrW = lBytWordAry
End Function
Function XorW(ByRef pBytWord1Ary, ByRef pBytWord2Ary)
Dim lBytWordAry(3)
Dim lLngIndex
For lLngIndex = 0 To 3
lBytWordAry(lLngIndex) = CByte(pBytWord1Ary(lLngIndex) Xor pBytWord2Ary(lLngIndex))
Next
XorW = lBytWordAry
End Function
Function NotW(ByRef pBytWordAry)
Dim lBytWordAry(3)
Dim lLngIndex
For lLngIndex = 0 To 3
lBytWordAry(lLngIndex) = Not CByte(pBytWordAry(lLngIndex))
Next
NotW = lBytWordAry
End Function
Function AddW(ByRef pBytWord1Ary, ByRef pBytWord2Ary)
Dim lLngIndex
Dim lIntTotal
Dim lBytWordAry(3)
For lLngIndex = 3 To 0 Step -1
If lLngIndex = 3 Then
lIntTotal = CInt(pBytWord1Ary(lLngIndex)) + pBytWord2Ary(lLngIndex)
lBytWordAry(lLngIndex) = lIntTotal Mod 256
Else
lIntTotal = CInt(pBytWord1Ary(lLngIndex)) + pBytWord2Ary(lLngIndex) + (lIntTotal \ 256)
lBytWordAry(lLngIndex) = lIntTotal Mod 256
End If
Next
AddW = lBytWordAry
End Function
Function CircShiftLeftW(ByRef pBytWordAry, ByRef pLngShift)
Dim lDbl1
Dim lDbl2
lDbl1 = WordToDouble(pBytWordAry)
lDbl2 = lDbl1
lDbl1 = CDbl(lDbl1 * (2 ^ pLngShift))
lDbl2 = CDbl(lDbl2 / (2 ^ (32 – pLngShift)))
CircShiftLeftW = OrW(DoubleToWord(lDbl1), DoubleToWord(lDbl2))
End Function
Function WordToHex(ByRef pBytWordAry)
Dim lLngIndex
For lLngIndex = 0 To 3
WordToHex = WordToHex & Right(“0” & Hex(pBytWordAry(lLngIndex)), 2)
Next
End Function
Function HexToWord(ByRef pStrHex)
HexToWord = DoubleToWord(CDbl(“&h” & pStrHex)) ‘ needs “#” at end for VB?
End Function
Function DoubleToWord(ByRef pDblValue)
Dim lBytWordAry(3)
lBytWordAry(0) = Int(DMod(pDblValue, 2 ^ 32) / (2 ^ 24))
lBytWordAry(1) = Int(DMod(pDblValue, 2 ^ 24) / (2 ^ 16))
lBytWordAry(2) = Int(DMod(pDblValue, 2 ^ 16) / (2 ^ 8))
lBytWordAry(3) = Int(DMod(pDblValue, 2 ^ 8))
DoubleToWord = lBytWordAry
End Function
Function WordToDouble(ByRef pBytWordAry)
WordToDouble = CDbl((pBytWordAry(0) * (2 ^ 24)) + (pBytWordAry(1) * (2 ^ 16)) + (pBytWordAry(2) * (2 ^ 8)) + pBytWordAry(3))
End Function
Function DMod(ByRef pDblValue, ByRef pDblDivisor)
Dim lDblMod
lDblMod = CDbl(CDbl(pDblValue) – (Int(CDbl(pDblValue) / CDbl(pDblDivisor)) * CDbl(pDblDivisor)))
If lDblMod < 0 Then
lDblMod = CDbl(lDblMod + pDblDivisor)
End If
DMod = lDblMod
End Function
Function F( ByRef lIntT, ByRef pBytWordBAry, ByRef pBytWordCAry, ByRef pBytWordDAry)
If lIntT <= 19 Then
F = OrW(AndW(pBytWordBAry, pBytWordCAry), AndW((NotW(pBytWordBAry)), pBytWordDAry))
ElseIf lIntT <= 39 Then
F = XorW(XorW(pBytWordBAry, pBytWordCAry), pBytWordDAry)
ElseIf lIntT <= 59 Then
F = OrW(OrW(AndW(pBytWordBAry, pBytWordCAry), AndW(pBytWordBAry, pBytWordDAry)), AndW(pBytWordCAry, pBytWordDAry))
Else
F = XorW(XorW(pBytWordBAry, pBytWordCAry), pBytWordDAry)
End If
End Function
Function sha1_Hash(pStrMessage)
Dim lLngLen, lBytLenW, lLngTempWordWAry, lLngNumBlocks, lLngBlock, lIntT, lBytTempAry
Dim lVarWordWAry(79), lVarWordKAry(3)
Dim lStrBlockText, lStrWordText, lStrPadMessage
Dim lBytWordH0Ary, lBytWordH1Ary, lBytWordH2Ary, lBytWordH3Ary, lBytWordH4Ary
Dim lBytWordAAry, lBytWordBAry, lBytWordCAry, lBytWordDAry, lBytWordEAry, lBytWordFAry
lLngLen = Len(cstr(pStrMessage))
lBytLenW = DoubleToWord(CDbl(lLngLen) * 8)
lStrPadMessage = pStrMessage & Chr(128) & String((128 – (lLngLen Mod 64) – 9) Mod 64, Chr(0)) & String(4, Chr(0)) & Chr(lBytLenW(0)) & Chr(lBytLenW(1)) & Chr(lBytLenW(2)) & Chr(lBytLenW(3))
lLngNumBlocks = Len(lStrPadMessage) / 64
lVarWordKAry(0) = HexToWord(“5A827999”)
lVarWordKAry(1) = HexToWord(“6ED9EBA1”)
lVarWordKAry(2) = HexToWord(“8F1BBCDC”)
lVarWordKAry(3) = HexToWord(“CA62C1D6”)
lBytWordH0Ary = HexToWord(“67452301”)
lBytWordH1Ary = HexToWord(“EFCDAB89”)
lBytWordH2Ary = HexToWord(“98BADCFE”)
lBytWordH3Ary = HexToWord(“10325476”)
lBytWordH4Ary = HexToWord(“C3D2E1F0”)
For lLngBlock = 0 To lLngNumBlocks – 1
lStrBlockText = Mid(lStrPadMessage, (lLngBlock * 64) + 1, 64)
For lIntT = 0 To 15
lStrWordText = Mid(lStrBlockText, (lIntT * 4) + 1, 4)
lVarWordWAry(lIntT) = Array(Asc(Mid(lStrWordText, 1, 1)), Asc(Mid(lStrWordText, 2, 1)), Asc(Mid(lStrWordText, 3, 1)), Asc(Mid(lStrWordText, 4, 1)))
Next
For lIntT = 16 To 79
lVarWordWAry(lIntT) = CircShiftLeftW(XorW(XorW(XorW(lVarWordWAry(lIntT – 3), lVarWordWAry(lIntT – 8)), lVarWordWAry(lIntT – 14)), lVarWordWAry(lIntT – 16)), 1)
Next
lBytWordAAry = lBytWordH0Ary
lBytWordBAry = lBytWordH1Ary
lBytWordCAry = lBytWordH2Ary
lBytWordDAry = lBytWordH3Ary
lBytWordEAry = lBytWordH4Ary
For lIntT = 0 To 79
lBytWordFAry = F(lIntT, lBytWordBAry, lBytWordCAry, lBytWordDAry)
lBytTempAry = AddW(AddW(AddW(AddW(CircShiftLeftW(lBytWordAAry, 5), lBytWordFAry), lBytWordEAry), lVarWordWAry(lIntT)), lVarWordKAry(lIntT \ 20))
lBytWordEAry = lBytWordDAry
lBytWordDAry = lBytWordCAry
lBytWordCAry = CircShiftLeftW(lBytWordBAry, 30)
lBytWordBAry = lBytWordAAry
lBytWordAAry = lBytTempAry
Next
lBytWordH0Ary = AddW(lBytWordH0Ary, lBytWordAAry)
lBytWordH1Ary = AddW(lBytWordH1Ary, lBytWordBAry)
lBytWordH2Ary = AddW(lBytWordH2Ary, lBytWordCAry)
lBytWordH3Ary = AddW(lBytWordH3Ary, lBytWordDAry)
lBytWordH4Ary = AddW(lBytWordH4Ary, lBytWordEAry)
Next
sha1_Hash = WordToHex(lBytWordH0Ary) & WordToHex(lBytWordH1Ary) & WordToHex(lBytWordH2Ary) & WordToHex(lBytWordH3Ary) & WordToHex(lBytWordH4Ary)
End Function
%>
Il codice sopra lo potete anche scaricare da qui sha1
Una volta inserito il codice, possiamo richiamare la funzione con
<% stringaSha1=sha1_Hash(“stringa da convertire”) %>
Attenzione, l’algoritmo fa una conversione in maiuscolo. Se l’applicazione in cui deve essere utilizzata è case sensitive utilizzate un Lcase ( <% stringaSha1=lcase(sha1_Hash(“stringa da convertire”)) %> )
Se invece vi serve una conversione online, esistono siti appositi come