How to resolve the algorithm MD5 step by step in the APL programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm MD5 step by step in the APL programming language
Table of Contents
Problem Statement
Encode a string using an MD5 algorithm. The algorithm can be found on Wikipedia.
Optionally, validate your implementation by running all of the test values in IETF RFC (1321) for MD5. Additionally, RFC 1321 provides more precise information on the algorithm than the Wikipedia article. If the solution on this page is a library solution, see MD5/Implementation for an implementation from scratch.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm MD5 step by step in the APL programming language
Source code in the apl programming language
md5←{
⍝ index origin zero
⎕IO←0
⍝ decoding UTF-8 & padding
M←(⊢,(0⍴⍨512|448-512|≢))1,⍨l←,⍉(8⍴2)⊤'UTF-8'⎕UCS ⍵
⍝ add length
M,←,⍉(8⍴2)⊤⌽(8⍴256)⊤≢l
⍝ init registers
A←16⊥6 7 4 5 2 3 0 1
B←16⊥14 15 12 13 10 11 8 9
C←16⊥9 8 11 10 13 12 15 14
D←16⊥1 0 3 2 5 4 7 6
⍝ T table
T←⌊(2*32)×|1○1+⍳64
⍝ index table
K←16|i,(1+5×i),(5+3×i),7×i←⍳16
⍝ rot table
S←,1 0 2⍉4 4 4⍴7 12 17 22 5 9 14 20 4 11 16 23 6 10 15 21
⍝ truncate ⍵ to 32 bit & rot left ⍺
rot←{2⊥⍺⌽(32⍴2)⊤⍵}
proc←{
⍝ pack 512 bits into 32 bit words &
⍝ precompute X[k] + T[i]
l←T+(⊂K)⌷256⊥⍉⌽16 4⍴2⊥⍉64 8⍴⍺
fn←{
⍝ a b c d to binary
a b c d←↓⍉(32⍴2)⊤⍵
⍝ a + F(b,c,d)
⍺<16:S[⍺]rot l[⍺]+2⊥a+d≠b∧c≠d
⍺<32:S[⍺]rot l[⍺]+2⊥a+(b∧d)∨(c∧~d)
⍺<48:S[⍺]rot l[⍺]+2⊥a+b≠c≠d
S[⍺]rot l[⍺]+2⊥a+c≠b∨~d
}
(2*32)|⍵+⊃{¯1⌽((⍵[1]+⍺ fn ⍵)@0)⍵}/(⌽⍳64),⊂⍵
}
⍝ process each 512 bits
loop←{⍬≡⍺:⍵ ⋄ (512↓⍺)∇(512↑⍺)proc ⍵}
⍝ output registers
(⎕D,⎕A)[,⍉(2⍴16)⊤,⍉⊖(4⍴256)⊤M loop A B C D]
}
You may also check:How to resolve the algorithm Unicode variable names step by step in the Lily programming language
You may also check:How to resolve the algorithm Memory layout of a data structure step by step in the 68000 Assembly programming language
You may also check:How to resolve the algorithm Draw a pixel step by step in the Racket programming language
You may also check:How to resolve the algorithm Execute HQ9+ step by step in the Ursala programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Objeck programming language