MD5
Author: VesQ
Added: 11. huhtikuuta 2011 kello 3.42
Edited: 15. huhtikuuta 2011 kello 22.19
Category: Algoritmi
Description
Bagardin aikoinaan tekemä MD5-funktio.
Luo merkkijonosta MD5-tarkistussumman (hashin). Hashattua
merkkijonoa ei voida palauttaa alkuperäiseksi. Funktio on
hyödyllinen esim. salasanojen tallentamisessa.
Pohjana käytetty Craig Kiesaun Blitzille tehtyä MD5-funktiota:
http://www.blitzbasic.com/codearcs/codearcs.php?code=278
Code
Select all1 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | //==================================================================
// MD5-funktio [ Bagard ]
// * Pohjana käytetty Craig Kiesaun Blitzille tehtyä MD5-funktiota:
// http://www.blitzbasic.com/codearcs/codearcs.php?code=278
// -----------------------------
// Luo merkkijonosta MD5-tarkistussumman (hashin). Hashattua
// merkkijonoa ei voida palauttaa alkuperäiseksi. Funktio on
// hyödyllinen esim. salasanojen tallentamisessa.
// * Funktion ottaa parametrikseen hashattavan merkkijonon
// * Funktio palauttaa valmiin MD5-hashin.
// * Apufunktiot funktion omaan käyttöön:
// - MD5_F
// - MD5_G
// - MD5_H
// - MD5_I
// - MD5_FF
// - MD5_GG
// - MD5_HH
// - MD5_II
// - RotateLeft
// - WordToHex
// - BinAnd
// - BinNot
// - BinXor
// - BinOr
// - Bin2Dec
//==================================================================
// -----------------------------
// ESIMERKKI
// -----------------------------
SCREEN 550, 150
Locate 0, 15
Repeat
Text 0, 0, "Kirjoita hashattava merkkijono ja paina rivinvaihtoa..."
merkkijono$ = Input(">> ")
DrawScreen
Until KeyHit( cbKeyReturn )
CloseInput
Text 0, 0, "Merkkijono: " + merkkijono$
Text 0, 15, "MD5-hash: " + MD5( merkkijono$ )
DrawScreen
WaitKey
// -----------------------------
// FUNKTIO
// -----------------------------
Function MD5(jono$)
nblk = ((Len(jono$) + 8) Shr 6) + 1
Dim MD5_x(nblk * 16 - 1)
For i = 0 To nblk * 16 - 1
MD5_x(i) = 0
Next i
For i = 0 To (Len(jono$) - 1)
MD5_x(i Shr 2) = BinOr(MD5_x(i Shr 2), (Asc(Mid(jono$, (i + 1), 1)) Shl ((i Mod 4) * 8)))
Next i
MD5_x(i Shr 2) = BinOr(MD5_x(i Shr 2), (128 Shl (((i) Mod 4) * 8)))
MD5_x(nblk * 16 - 2) = Len(jono$) * 8
MD5_a = 1732584193 //&H67452301
MD5_b = -271733879 //&HEFCDAB89
MD5_c = -1732584194 //&H98BADCFE
MD5_d = 271733878 //&H10325476
// Käydään sanat läpi
For k = 0 To (nblk * 16 - 1) Step 16
MD5_AA = MD5_a
MD5_BB = MD5_b
MD5_CC = MD5_c
MD5_DD = MD5_d
// Kierros 1
MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 0), 7, -680876936) //&HD76AA478
MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 1), 12, -389564586) //&HE8C7B756
MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 2), 17, 606105819 )//&H242070DB
MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 3), 22, -1044525330) //&HC1BDCEEE
MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 4), 7, -176418897) //&HF57C0FAF
MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 5), 12, 1200080426 )//&H4787C62A
MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 6), 17, -1473231341) //&HA8304613
MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 7), 22, -45705983) //&HFD469501
MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 8), 7, 1770035416) //&H698098D8
MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 9), 12, -1958414417 )//&H8B44F7AF
MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 10), 17, -42063 )//&HFFFF5BB1
MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 11), 22, -1990404162) //&H895CD7BE
MD5_a = MD5_FF(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 12), 7, 1804603682) //&H6B901122
MD5_d = MD5_FF(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 13), 12, -40341101) //&HFD987193
MD5_c = MD5_FF(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 14), 17, -1502002290) //&HA679438E
MD5_b = MD5_FF(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 15), 22, 1236535329) //&H49B40821
// Kierros 2
MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 1), 5, -165796510) //&HF61E2562
MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 6), 9, -1069501632) //&HC040B340
MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 11), 14, 643717713) //&H265E5A51
MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 0), 20, -373897302) //&HE9B6C7AA
MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 5), 5, -701558691) //&HD62F105D
MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 10), 9, 38016083) //&H2441453
MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 15), 14, -660478335) //&HD8A1E681
MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 4), 20, -405537848) //&HE7D3FBC8
MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 9), 5, 568446438) //&H21E1CDE6
MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 14), 9, -1019803690) //&HC33707D6
MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 3), 14, -187363961) //&HF4D50D87
MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 8), 20, 1163531501) //&H455A14ED
MD5_a = MD5_GG(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 13), 5, -1444681467) //&HA9E3E905
MD5_d = MD5_GG(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 2), 9, -51403784) //&HFCEFA3F8
MD5_c = MD5_GG(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 7), 14, 1735328473) //&H676F02D9
MD5_b = MD5_GG(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 12), 20, -1926607734) //&H8D2A4C8A
// Kierros 3
MD5_a = MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 5), 4, -378558) //&HFFFA3942
MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 8), 11, -2022574463) //&H8771F681
MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 11), 16, 1839030562) //&H6D9D6122
MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 14), 23, -35309556) //&HFDE5380C
MD5_a = MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 1), 4, -1530992060) //&HA4BEEA44
MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 4), 11, 1272893353) //&H4BDECFA9
MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 7), 16, -155497632) //&HF6BB4B60
MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 10), 23, -1094730640) //&HBEBFBC70
MD5_a = MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 13), 4, 681279174) //&H289B7EC6
MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 0), 11, -358537222) //&HEAA127FA
MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 3), 16, -722521979) //&HD4EF3085
MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 6), 23, 76029189) //&H4881D05
MD5_a = MD5_HH(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 9), 4, -640364487) //&HD9D4D039
MD5_d = MD5_HH(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 12), 11, -421815835) //&HE6DB99E5
MD5_c = MD5_HH(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 15), 16, 530742520) //&H1FA27CF8
MD5_b = MD5_HH(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 2), 23, -995338651) //&HC4AC5665
// Kierros 4
MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 0), 6, -198630844) //&HF4292244
MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 7), 10, 1126891415) //&H432AFF97
MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 14), 15, -1416354905) //&HAB9423A7
MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 5), 21, -57434055) //&HFC93A039
MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 12), 6, 1700485571) //&H655B59C3
MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 3), 10, -1894986606) //&H8F0CCC92
MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 10), 15, -1051523) //&HFFEFF47D
MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 1), 21, -2054922799) //&H85845DD1
MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 8), 6, 1873313359) //&H6FA87E4F
MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 15), 10, -30611744) //&HFE2CE6E0
MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 6), 15, -1560198380 )//&HA3014314
MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 13), 21, 1309151649) //&H4E0811A1
MD5_a = MD5_II(MD5_a, MD5_b, MD5_c, MD5_d, MD5_x(k + 4), 6, -145523070) //&HF7537E82
MD5_d = MD5_II(MD5_d, MD5_a, MD5_b, MD5_c, MD5_x(k + 11), 10, -1120210379) //&HBD3AF235
MD5_c = MD5_II(MD5_c, MD5_d, MD5_a, MD5_b, MD5_x(k + 2), 15, 718787259) //&H2AD7D2BB
MD5_b = MD5_II(MD5_b, MD5_c, MD5_d, MD5_a, MD5_x(k + 9), 21, -343485551) //&HEB86D391
MD5_a = MD5_a + MD5_AA
MD5_b = MD5_b + MD5_BB
MD5_c = MD5_c + MD5_CC
MD5_d = MD5_d + MD5_DD
Next k
Return Lower(Str(WordToHex(MD5_a)) + Str(WordToHex(MD5_b)) + Str(WordToHex(MD5_c)) + Str(WordToHex(MD5_d)))
End Function
Function MD5_F(x, y, z)
Return BinOr(BinAnd(x, y), BinAnd(BinNot(x), z))
End Function
Function MD5_G(x, y, z)
Return BinOr(BinAnd(x, z), BinAnd(y, BinNot(z)))
End Function
Function MD5_H(x, y, z)
Return BinXor(BinXor(x, y), z)
End Function
Function MD5_I(x, y, z)
Return BinXor(y, BinOr(x, BinNot(z)))
End Function
Function MD5_FF(a, b, c, d, x, s, ac)
a = (a + ((MD5_F(b, c, d)+ x)+ ac))
a = RotateLeft(a, s)
Return a + b
End Function
Function MD5_GG(a, b, c, d, x, s, ac)
a = (a + ((MD5_G(b, c, d) + x) + ac))
a = RotateLeft(a, s)
Return a + b
End Function
Function MD5_HH(a, b, c, d, x, s, ac)
a = (a + ((MD5_H(b, c, d) + x) + ac))
a = RotateLeft(a, s)
Return a + b
End Function
Function MD5_II(a, b, c, d, x, s, ac)
a = (a + ((MD5_I(b, c, d) + x) + ac))
a = RotateLeft(a, s)
Return a + b
End Function
Function RotateLeft(lValue, iShiftBits)
Return BinOr(lValue Shl iShiftBits, lValue Shr (32 - iShiftBits))
End Function
Function WordToHex(lValue)
For lCount = 0 To 3
lByte = BinAnd(lValue Shr lCount * 8, 255)
ToHex$ = ToHex$ + Right("0" + Hex(lByte), 2)
Next lCount
Return ToHex$
End Function
Function BinAnd(luku1, luku2)
For i = 0 To 31
luku3 = luku3 + (((luku1 Shr i) Mod 2) And ((luku2 Shr i) Mod 2)) Shl i
Next i
Return luku3
End Function
Function BinNot(luku1)
For i = 0 To 31
luku3 = luku3 + (Not ((luku1 Shr i) Mod 2)) Shl i
Next i
Return luku3
End Function
Function BinXor(luku1, luku2)
For i = 0 To 31
luku3 = luku3 + (((luku1 Shr i) Mod 2) Xor ((luku2 Shr i) Mod 2)) Shl i
Next i
Return luku3
End Function
Function BinOr(luku1, luku2)
For i = 0 To 31
luku3 = luku3 + (((luku1 Shr i) Mod 2) Or ((luku2 Shr i) Mod 2)) Shl i
Next i
Return luku3
End Function
Function Bin2Dec(jono$)
For i = Len(jono$) To 1 Step -1
arvo = Int(Mid(jono$, i, 1))
If arvo = 1 Then
luku = luku + 2 ^ (Len(jono$) - i)
EndIf
Next i
Return luku
End Function
|
Comments
No comments. You can be first!
Leave a comment
You must be logged in to comment.