January 26, 2021, 06:37:14 AM

### Author Topic: [bmx] A medley of digests by Yan [ 1+ years ago ]  (Read 404 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bmx] A medley of digests by Yan [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : A medley of digests
Author : Yan
Posted : 1+ years ago

Description : Three functions that take a string and spit out an MD5, SHA-1 or SHA-256 hash.

Code :
Code: BlitzMax
1. Strict
2.
3. '====================================== TEST ======================================
4.
5. Print
6. Print "==== MD5 ===="
7. Print MD5("The quick brown fox jumps over the lazy dog")
8. Print "9e107d9d372bb6826bd81d3542a419d6 <- Should be this"
9. Print
10. Print "===== SHA-1 ====="
11. Print SHA1("The quick brown fox jumps over the lazy dog")
12. Print "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12 <- Should be this"
13. Print
14. Print "===== SHA-256 ====="
15. Print SHA256("The quick brown fox jumps over the lazy dog")
16. Print "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592 <- Should be this"
17.
18. End
19.
20. '====================================== TEST ======================================
21.
22.
23. Function MD5\$(in\$)
24.   Local h0 = \$67452301, h1 = \$EFCDAB89, h2 = \$98BADCFE, h3 = \$10325476
25.
26.   Local r[] = [7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,..
27.                 5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,..
28.                 4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,..
29.                 6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21]
30.
31.   Local k[] = [\$D76AA478, \$E8C7B756, \$242070DB, \$C1BDCEEE, \$F57C0FAF, \$4787C62A,..
32.                 \$A8304613, \$FD469501, \$698098D8, \$8B44F7AF, \$FFFF5BB1, \$895CD7BE,..
33.                 \$6B901122, \$FD987193, \$A679438E, \$49B40821, \$F61E2562, \$C040B340,..
34.                 \$265E5A51, \$E9B6C7AA, \$D62F105D, \$02441453, \$D8A1E681, \$E7D3FBC8,..
35.                 \$21E1CDE6, \$C33707D6, \$F4D50D87, \$455A14ED, \$A9E3E905, \$FCEFA3F8,..
36.                 \$676F02D9, \$8D2A4C8A, \$FFFA3942, \$8771F681, \$6D9D6122, \$FDE5380C,..
37.                 \$A4BEEA44, \$4BDECFA9, \$F6BB4B60, \$BEBFBC70, \$289B7EC6, \$EAA127FA,..
38.                 \$D4EF3085, \$04881D05, \$D9D4D039, \$E6DB99E5, \$1FA27CF8, \$C4AC5665,..
39.                 \$F4292244, \$432AFF97, \$AB9423A7, \$FC93A039, \$655B59C3, \$8F0CCC92,..
40.                 \$FFEFF47D, \$85845DD1, \$6FA87E4F, \$FE2CE6E0, \$A3014314, \$4E0811A1,..
41.                 \$F7537E82, \$BD3AF235, \$2AD7D2BB, \$EB86D391]
42.
43.   Local intCount = (((in\$.length + 8) Shr 6) + 1) Shl 4
44.   Local data[intCount]
45.
46.   For Local c=0 Until in\$.length
47.     data[c Shr 2] = data[c Shr 2] | ((in\$[c] & \$FF) Shl ((c & 3) Shl 3))
48.   Next
49.   data[in\$.length Shr 2] = data[in\$.length Shr 2] | (\$80 Shl ((in\$.length & 3) Shl 3))
50.   data[data.length - 2] = (Long(in\$.length) * 8) & \$FFFFFFFF
51.   data[data.length - 1] = (Long(in\$.length) * 8) Shr 32
52.
53.   For Local chunkStart=0 Until intCount Step 16
54.     Local a = h0, b = h1, c = h2, d = h3
55.
56.     For Local i=0 To 15
57.       Local f = d ~ (b & (c ~ d))
58.       Local t = d
59.
60.       d = c ; c = b
61.       b = Rol((a + f + k[i] + data[chunkStart + i]), r[i]) + b
62.       a = t
63.     Next
64.
65.     For Local i=16 To 31
66.       Local f = c ~ (d & (b ~ c))
67.       Local t = d
68.
69.       d = c ; c = b
70.       b = Rol((a + f + k[i] + data[chunkStart + (((5 * i) + 1) & 15)]), r[i]) + b
71.       a = t
72.     Next
73.
74.     For Local i=32 To 47
75.       Local f = b ~ c ~ d
76.       Local t = d
77.
78.       d = c ; c = b
79.       b = Rol((a + f + k[i] + data[chunkStart + (((3 * i) + 5) & 15)]), r[i]) + b
80.       a = t
81.     Next
82.
83.     For Local i=48 To 63
84.       Local f = c ~ (b | ~d)
85.       Local t = d
86.
87.       d = c ; c = b
88.       b = Rol((a + f + k[i] + data[chunkStart + ((7 * i) & 15)]), r[i]) + b
89.       a = t
90.     Next
91.
92.     h0 :+ a ; h1 :+ b
93.     h2 :+ c ; h3 :+ d
94.   Next
95.
96.   Return (LEHex(h0) + LEHex(h1) + LEHex(h2) + LEHex(h3)).ToLower()
97. End Function
98.
99. Function SHA1\$(in\$)
100.   Local h0 = \$67452301, h1 = \$EFCDAB89, h2 = \$98BADCFE, h3 = \$10325476, h4 = \$C3D2E1F0
101.
102.   Local intCount = (((in\$.length + 8) Shr 6) + 1) Shl 4
103.   Local data[intCount]
104.
105.   For Local c=0 Until in\$.length
106.     data[c Shr 2] = (data[c Shr 2] Shl 8) | (in\$[c] & \$FF)
107.   Next
108.   data[in\$.length Shr 2] = ((data[in\$.length Shr 2] Shl 8) | \$80) Shl ((3 - (in\$.length & 3)) Shl 3)
109.   data[data.length - 2] = (Long(in\$.length) * 8) Shr 32
110.   data[data.length - 1] = (Long(in\$.length) * 8) & \$FFFFFFFF
111.
112.   For Local chunkStart=0 Until intCount Step 16
113.     Local a = h0, b = h1, c = h2, d = h3, e = h4
114.
115.     Local w[] = data[chunkStart..chunkStart + 16]
116.     w = w[..80]
117.
118.     For Local i=16 To 79
119.       w[i] = Rol(w[i - 3] ~ w[i - 8] ~ w[i - 14] ~ w[i - 16], 1)
120.     Next
121.
122.     For Local i=0 To 19
123.       Local t = Rol(a, 5) + (d ~ (b & (c ~ d))) + e + \$5A827999 + w[i]
124.
125.       e = d ; d = c
126.       c = Rol(b, 30)
127.       b = a ; a = t
128.     Next
129.
130.     For Local i=20 To 39
131.       Local t = Rol(a, 5) + (b ~ c ~ d) + e + \$6ED9EBA1 + w[i]
132.
133.       e = d ; d = c
134.       c = Rol(b, 30)
135.       b = a ; a = t
136.     Next
137.
138.     For Local i=40 To 59
139.       Local t = Rol(a, 5) + ((b & c) | (d & (b | c))) + e + \$8F1BBCDC + w[i]
140.
141.       e = d ; d = c
142.       c = Rol(b, 30)
143.       b = a ; a = t
144.     Next
145.
146.     For Local i=60 To 79
147.       Local t = Rol(a, 5) + (b ~ c ~ d) + e + \$CA62C1D6 + w[i]
148.
149.       e = d ; d = c
150.       c = Rol(b, 30)
151.       b = a ; a = t
152.     Next
153.
154.     h0 :+ a ; h1 :+ b ; h2 :+ c
155.     h3 :+ d ; h4 :+ e
156.   Next
157.
158.   Return (Hex(h0) + Hex(h1) + Hex(h2) + Hex(h3) + Hex(h4)).ToLower()
159. End Function
160.
161. Function SHA256\$(in\$)
162.   Local h0 = \$6A09E667, h1 = \$BB67AE85, h2 = \$3C6EF372, h3 = \$A54FF53A
163.   Local h4 = \$510E527F, h5 = \$9B05688C, h6 = \$1F83D9AB, h7 = \$5BE0CD19
164.
165.   Local k[] = [\$428A2F98, \$71374491, \$B5C0FBCF, \$E9B5DBA5, \$3956C25B, \$59F111F1,..
166.                 \$923F82A4, \$AB1C5ED5, \$D807AA98, \$12835B01, \$243185BE, \$550C7DC3,..
167.                 \$72BE5D74, \$80DEB1FE, \$9BDC06A7, \$C19BF174, \$E49B69C1, \$EFBE4786,..
168.                 \$0FC19DC6, \$240CA1CC, \$2DE92C6F, \$4A7484AA, \$5CB0A9DC, \$76F988DA,..
169.                 \$983E5152, \$A831C66D, \$B00327C8, \$BF597FC7, \$C6E00BF3, \$D5A79147,..
170.                 \$06CA6351, \$14292967, \$27B70A85, \$2E1B2138, \$4D2C6DFC, \$53380D13,..
171.                 \$650A7354, \$766A0ABB, \$81C2C92E, \$92722C85, \$A2BFE8A1, \$A81A664B,..
172.                 \$C24B8B70, \$C76C51A3, \$D192E819, \$D6990624, \$F40E3585, \$106AA070,..
173.                 \$19A4C116, \$1E376C08, \$2748774C, \$34B0BCB5, \$391C0CB3, \$4ED8AA4A,..
174.                 \$5B9CCA4F, \$682E6FF3, \$748F82EE, \$78A5636F, \$84C87814, \$8CC70208,..
175.                 \$90BEFFFA, \$A4506CEB, \$BEF9A3F7, \$C67178F2]
176.
177.   Local intCount = (((in\$.length + 8) Shr 6) + 1) Shl 4
178.   Local data[intCount]
179.
180.   For Local c=0 Until in\$.length
181.     data[c Shr 2] = (data[c Shr 2] Shl 8) | (in\$[c] & \$FF)
182.   Next
183.   data[in\$.length Shr 2] = ((data[in\$.length Shr 2] Shl 8) | \$80) Shl ((3 - (in\$.length & 3)) Shl 3)
184.   data[data.length - 2] = (Long(in\$.length) * 8) Shr 32
185.   data[data.length - 1] = (Long(in\$.length) * 8) & \$FFFFFFFF
186.
187.   For Local chunkStart=0 Until intCount Step 16
188.     Local a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7
189.
190.     Local w[] = data[chunkStart..chunkStart + 16]
191.     w = w[..64]
192.
193.     For Local i=16 To 63
194.       w[i] = w[i - 16] + (Ror(w[i - 15], 7) ~ Ror(w[i - 15], 18) ~ (w[i - 15] Shr 3))..
195.             + w[i - 7] + (Ror(w[i - 2], 17) ~ Ror(w[i - 2], 19) ~ (w[i - 2] Shr 10))
196.     Next
197.
198.     For Local i=0 To 63
199.       Local t0 = (Ror(a, 2) ~ Ror(a, 13) ~ Ror(a, 22)) + ((a & b) | (b & c) | (c & a))
200.       Local t1 = h + (Ror(e, 6) ~ Ror(e, 11) ~ Ror(e, 25)) + ((e & f) | (~e & g)) + k[i] + w[i]
201.
202.       h = g ; g = f ; f = e ; e = d + t1
203.       d = c ; c = b ; b = a ;  a = t0 + t1
204.     Next
205.
206.     h0 :+ a ; h1 :+ b ; h2 :+ c ; h3 :+ d
207.     h4 :+ e ; h5 :+ f ; h6 :+ g ; h7 :+ h
208.   Next
209.
210.   Return (Hex(h0) + Hex(h1) + Hex(h2) + Hex(h3) + Hex(h4) + Hex(h5) + Hex(h6) + Hex(h7)).ToLower()
211. End Function
212.
213. Function Rol(val, shift)
214.   Return (val Shl shift) | (val Shr (32 - shift))
215. End Function
216.
217. Function Ror(val, shift)
218.   Return (val Shr shift) | (val Shl (32 - shift))
219. End Function
220.
221. Function LEHex\$(val)
222.   Local out\$ = Hex(val)
223.
224.   Return out\$[6..8] + out\$[4..6] + out\$[2..4] + out\$[0..2]
225. End Function

Filax(Posted 1+ years ago)

Many thanks

FlameDuck(Posted 1+ years ago)

A adaption of the MD5 digest that works on banks, rather than strings.
Code: [Select]
Strict

Function MD5:String(inbank:TBank)
Local h0 = \$67452301, h1 = \$EFCDAB89, h2 = \$98BADCFE, h3 = \$10325476

Local r[] = [7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,..
5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,..
4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,..
6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21]

Local k[] = [\$D76AA478, \$E8C7B756, \$242070DB, \$C1BDCEEE, \$F57C0FAF, \$4787C62A,..
\$A8304613, \$FD469501, \$698098D8, \$8B44F7AF, \$FFFF5BB1, \$895CD7BE,..
\$6B901122, \$FD987193, \$A679438E, \$49B40821, \$F61E2562, \$C040B340,..
\$265E5A51, \$E9B6C7AA, \$D62F105D, \$02441453, \$D8A1E681, \$E7D3FBC8,..
\$21E1CDE6, \$C33707D6, \$F4D50D87, \$455A14ED, \$A9E3E905, \$FCEFA3F8,..
\$676F02D9, \$8D2A4C8A, \$FFFA3942, \$8771F681, \$6D9D6122, \$FDE5380C,..
\$A4BEEA44, \$4BDECFA9, \$F6BB4B60, \$BEBFBC70, \$289B7EC6, \$EAA127FA,..
\$D4EF3085, \$04881D05, \$D9D4D039, \$E6DB99E5, \$1FA27CF8, \$C4AC5665,..
\$F4292244, \$432AFF97, \$AB9423A7, \$FC93A039, \$655B59C3, \$8F0CCC92,..
\$FFEFF47D, \$85845DD1, \$6FA87E4F, \$FE2CE6E0, \$A3014314, \$4E0811A1,..
\$F7537E82, \$BD3AF235, \$2AD7D2BB, \$EB86D391]

Local intCount = (((inbank.Size() + 8) Shr 6) + 1) Shl 4
Local data[intCount]
Local inPtr:Byte Ptr = inbank.Buf()

For Local c=0 Until inbank.Size()
data[c Shr 2] = data[c Shr 2] | ((inPtr[c] & \$FF) Shl ((c & 3) Shl 3))
Next

data[inbank.Size() Shr 2] = data[inbank.Size() Shr 2] | (\$80 Shl ((inbank.Size() & 3) Shl 3))
data[data.length - 2] = (Long(inbank.Size()) * 8) & \$FFFFFFFF
data[data.length - 1] = (Long(inbank.Size()) * 8) Shr 32

For Local chunkStart=0 Until intCount Step 16
Local a = h0, b = h1, c = h2, d = h3

For Local i=0 To 15
Local f = d ~ (b & (c ~ d))
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + data[chunkStart + i]), r[i]) + b
a = t
Next

For Local i=16 To 31
Local f = c ~ (d & (b ~ c))
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + data[chunkStart + (((5 * i) + 1) & 15)]), r[i]) + b
a = t
Next

For Local i=32 To 47
Local f = b ~ c ~ d
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + data[chunkStart + (((3 * i) + 5) & 15)]), r[i]) + b
a = t
Next

For Local i=48 To 63
Local f = c ~ (b | ~d)
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + data[chunkStart + ((7 * i) & 15)]), r[i]) + b
a = t
Next

h0 :+ a ; h1 :+ b
h2 :+ c ; h3 :+ d
Next

Return (LEHex(h0) + LEHex(h1) + LEHex(h2) + LEHex(h3)).ToLower()
End Function

Function Rol(val, shift)
Return (val Shl shift) | (val Shr (32 - shift))
End Function

Function Ror(val, shift)
Return (val Shr shift) | (val Shl (32 - shift))
End Function

Function LEHex\$(val)
Local out\$ = Hex(val)

Return out\$[6..8] + out\$[4..6] + out\$[2..4] + out\$[0..2]
End Function

Cold Harbour(Posted 1+ years ago)

The SHA-1 and SHA-256 are very welcome.Thanks!

Yan(Posted 1+ years ago)

Buffered MD5 file routine...
Code: [Select]
Strict

'============================================ TEST ============================================

Notify("MD5 : " + FileMD5(RequestFile("Calculate MD5 of which file...", "", False, CurrentDir() + "/")))

End

'============================================ TEST ============================================

Function FileMD5\$(filePath\$, bufferSize=\$400000)
Assert (bufferSize & 63) = 0 Else "bufferSize must be a multiple of 64 bytes"

Local h0 = \$67452301, h1 = \$EFCDAB89, h2 = \$98BADCFE, h3 = \$10325476

Local r[] = [7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,..
5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,..
4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,..
6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21]

Local k[] = [\$D76AA478, \$E8C7B756, \$242070DB, \$C1BDCEEE, \$F57C0FAF, \$4787C62A,..
\$A8304613, \$FD469501, \$698098D8, \$8B44F7AF, \$FFFF5BB1, \$895CD7BE,..
\$6B901122, \$FD987193, \$A679438E, \$49B40821, \$F61E2562, \$C040B340,..
\$265E5A51, \$E9B6C7AA, \$D62F105D, \$02441453, \$D8A1E681, \$E7D3FBC8,..
\$21E1CDE6, \$C33707D6, \$F4D50D87, \$455A14ED, \$A9E3E905, \$FCEFA3F8,..
\$676F02D9, \$8D2A4C8A, \$FFFA3942, \$8771F681, \$6D9D6122, \$FDE5380C,..
\$A4BEEA44, \$4BDECFA9, \$F6BB4B60, \$BEBFBC70, \$289B7EC6, \$EAA127FA,..
\$D4EF3085, \$04881D05, \$D9D4D039, \$E6DB99E5, \$1FA27CF8, \$C4AC5665,..
\$F4292244, \$432AFF97, \$AB9423A7, \$FC93A039, \$655B59C3, \$8F0CCC92,..
\$FFEFF47D, \$85845DD1, \$6FA87E4F, \$FE2CE6E0, \$A3014314, \$4E0811A1,..
\$F7537E82, \$BD3AF235, \$2AD7D2BB, \$EB86D391]

Local fileStream:TStream = OpenStream(filePath\$, True, False)
If fileStream = Null Then Return

Local buffer:Byte Ptr = MemAlloc(bufferSize)
Local bitCount:Long, dataTop = bufferSize

Repeat

If fileStream.EOF()
dataTop = (((bytesRead + 8) Shr 6) + 1) Shl 6
If dataTop > bufferSize
buffer = MemExtend(buffer, bufferSize, dataTop)
EndIf

bitCount :+ (bytesRead Shl 3)

For Local b = (bytesRead + 1) Until (dataTop - 8)
buffer[b] = 0
Next

LEPokeLong(buffer, dataTop - 8, bitCount)
Else
bitCount :+ (bufferSize Shl 3)
EndIf

For Local chunkStart=0 Until (dataTop Shr 2) Step 16
Local a = h0, b = h1, c = h2, d = h3

For Local i=0 To 15
Local f = d ~ (b & (c ~ d))
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + LEPeekInt(buffer, (chunkStart + i) Shl 2)), r[i]) + b
a = t
Next

For Local i=16 To 31
Local f = c ~ (d & (b ~ c))
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + LEPeekInt(buffer, (chunkStart + (((5 * i) + 1) & 15)) Shl 2)), r[i]) + b
a = t
Next

For Local i=32 To 47
Local f = b ~ c ~ d
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + LEPeekInt(buffer, (chunkStart + (((3 * i) + 5) & 15)) Shl 2)), r[i]) + b
a = t
Next

For Local i=48 To 63
Local f = c ~ (b | ~d)
Local t = d

d = c ; c = b
b = Rol((a + f + k[i] + LEPeekInt(buffer, (chunkStart + ((7 * i) & 15)) Shl 2)), r[i]) + b
a = t
Next

h0 :+ a ; h1 :+ b
h2 :+ c ; h3 :+ d
Next
Until fileStream.EOF()

fileStream.Close()
MemFree(buffer)

Return (LEHex(h0) + LEHex(h1) + LEHex(h2) + LEHex(h3)).ToLower()
End Function

Function Rol(val, shift)
Return (val Shl shift) | (val Shr (32 - shift))
End Function

Function LEPeekInt(buffer:Byte Ptr, offset)
Return (buffer[offset + 3] Shl 24) | (buffer[offset + 2] Shl 16) | ..
(buffer[offset + 1] Shl 8) | buffer[offset]
End Function

Function LEPokeLong(buffer:Byte Ptr, offset, value:Long)
For Local b=7 To 0 Step -1
buffer[offset + b] = (value Shr (b Shl 3)) & \$ff
Next
End Function

Function LEHex\$(val)
Local out\$ = Hex(val)

Return out\$[6..8] + out\$[4..6] + out\$[2..4] + out\$[0..2]
End Function

Filax(Posted 1+ years ago)

Any idea for 2048 byte keys ?

ShadowTurtle(Posted 1+ years ago)

Make it similary for storing two 16 bit values into one 32bit bank.this means (it is just another sample): md5(md5("this is") + md5("crypted")). [/i]

SimplePortal 2.3.6 © 2008-2014, SimplePortal