RosettaCodeData/Task/MD5/APL/md5.apl

41 lines
1.4 KiB
APL
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
md5{
⍝ index origin zero
⎕IO0
⍝ decoding UTF-8 & padding
M(,(0512|448-512|))1,l,(82)'UTF-8'⎕UCS
⍝ add length
M,,(82)(8256)l
⍝ init registers
A166 7 4 5 2 3 0 1
B1614 15 12 13 10 11 8 9
C169 8 11 10 13 12 15 14
D161 0 3 2 5 4 7 6
⍝ T table
T(2*32)×|11+64
⍝ index table
K16|i,(1+5×i),(5+3×i),7×i16
⍝ rot table
S,1 0 24 4 47 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21
⍝ truncate ⍵ to 32 bit & rot left
rot{2(322)}
proc{
⍝ pack 512 bits into 32 bit words &
⍝ precompute X[k] + T[i]
lT+(K)25616 4264 8
fn{
⍝ a b c d to binary
a b c d(322)
⍝ a + F(b,c,d)
<16:S[]rot l[]+2a+dbcd
<32:S[]rot l[]+2a+(bd)(c~d)
<48:S[]rot l[]+2a+bcd
S[]rot l[]+2a+cb~d
}
(2*32)|+{¯1(([1]+ fn )@0)}/(64),
}
⍝ process each 512 bits
loop{: (512)(512)proc }
⍝ output registers
(⎕D,⎕A)[,(216),(4256)M loop A B C D]
}