Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them.
Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/md5.wiki
# omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
Sign in to star 0 stars 0 watching
About Files History Releases Proposed changes Issues For developers
branch `main` Branches Tags History
`omega-rosetta / drafts / rosettacode / md5.wiki`
169 lines ยท 4094 bytes Raw Blame History
1 =={{header|Omegese}}==
2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}}
3 Omega has no imports, so the small output helpers (emit and friends) are part of the program.
4
5 om2
6 text nl "\n"
7 text hexd "`0123456789abcdef`"
8 note K[i] = floor(abs(sin(i + 1)) * 2^32) as 64 eight-digit hex words (RFC 1321, section 3.4)
9 text khex "`d76aa478e8c7b756242070dbc1bdceeef57c0faf4787c62aa8304613fd469501698098d88b44f7afffff5bb1895cd7be6b901122fd987193a679438e49b40821f61e2562c040b340265e5a51e9b6c7aad62f105d02441453d8a1e681e7d3fbc821e1cde6c33707d6f4d50d87455a14eda9e3e905fcefa3f8676f02d98d2a4c8afffa39428771f6816d9d6122fde5380ca4beea444bdecfa9f6bb4b60bebfbc70289b7ec6eaa127fad4ef308504881d05d9d4d039e6db99e51fa27cf8c4ac5665f4292244432aff97ab9423a7fc93a039655b59c38f0ccc92ffeff47d85845dd16fa87e4ffe2ce6e0a30143144e0811a1f7537e82bd3af2352ad7d2bbeb86d391`"
10 note the rotate amounts of the four rounds, two decimal digits each
11 text rots "`07121722050914200411162306101521`"
12 text m0 "The quick brown fox jumps over the lazy dog"
13 text m1 "The quick brown fox jumped over the lazy dog's back"
14 text m2 "Rosetta Code"
15 text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16 global kt uint 64
17 global hs uint 4
18 global mw uint 16
19 global buf uchar 128
20 note emit writes every byte of a to stdout; a short write is retried, an error exits 1
21 fn emit a view uchar * to void
22 o = ulong 0
23 while lt o len a bound 4096
24 k = write 1 a o sub len a o
25 if le k 0
26 exit 1
27 end
28 o = add o ulong k
29 end
30 end
31 note emitc writes one byte
32 fn emitc c uchar to void
33 b = new uchar 1 0
34 put b 0 c
35 emit b
36 end
37 note hexv gives the value of one lower-case hex digit
38 fn hexv c uchar to uint
39 if le c 57
40 ret uint sub c 48
41 end
42 ret uint sub c 87
43 end
44 note emith writes the byte v as two lower-case hex digits
45 fn emith v uchar to void
46 emitc get hexd shr v 4
47 emitc get hexd and v 15
48 end
49 fn initk to void
50 for i ulong 64
51 v = uint 0
52 for j ulong 8
53 v = or shl v 4 hexv get khex add mul i 8 j
54 end
55 put kt i v
56 end
57 end
58 note rotamt is the rotate amount of step i: round i / 16, place i mod 4
59 fn rotamt i ulong to uint
60 x = mul add mul div i 16 4 rem i 4 2
61 h = sub get rots x 48
62 l = sub get rots add x 1 48
63 ret uint add mul h 10 l
64 end
65 note rnd is step i of 64 on the state (a, b, c, d) in hs, with the block's words in mw
66 fn rnd i ulong to void
67 a = get hs 0
68 b = get hs 1
69 c = get hs 2
70 d = get hs 3
71 f = uint 0
72 g = ulong 0
73 if lt i 16
74 f = or and b c and not b d
75 g = i
76 else
77 if lt i 32
78 f = or and d b and not d c
79 g = rem add mul i 5 1 16
80 else
81 if lt i 48
82 f = xor xor b c d
83 g = rem add mul i 3 5 16
84 else
85 f = xor c or b not d
86 g = rem mul i 7 16
87 end
88 end
89 end
90 f = add add add f a get kt i get mw g
91 s = rotamt i
92 put hs 0 d
93 put hs 3 c
94 put hs 2 b
95 put hs 1 add b rotl f s
96 end
97 note block loads the 64 bytes of buf at p as 16 little-endian words and runs the 64 steps
98 fn block p ulong to void
99 for w ulong 16
100 v = uint 0
101 for j ulong 4
102 v = or shl v 8 uint get buf add add p mul w 4 sub 3 j
103 end
104 put mw w v
105 end
106 sv = new uint 4 0
107 for k ulong 4
108 put sv k get hs k
109 end
110 for i ulong 64
111 rnd i
112 end
113 for k ulong 4
114 put hs k add get hs k get sv k
115 end
116 end
117 note md5 pads a (up to 119 bytes) with 0x80, zeros and the bit length (little-endian) to one or two
118 note 64-byte blocks, hashes them and prints the digest in hex
119 fn md5 a view uchar * to void
120 put hs 0 uint `1732584193`
121 put hs 1 uint `4023233417`
122 put hs 2 uint `2562383102`
123 put hs 3 uint `271733878`
124 n = len a
125 for i ulong 128
126 put buf i 0
127 end
128 for i ulong n
129 put buf i get a i
130 end
131 put buf n 128
132 t = ulong 64
133 if ge n 56
134 t = 128
135 end
136 bits = mul n 8
137 for j ulong 8
138 put buf add sub t 8 j uchar and shr bits mul j 8 255
139 end
140 for b ulong div t 64
141 block mul b 64
142 end
143 for k ulong 4
144 v = get hs k
145 for j ulong 4
146 emith uchar and shr v mul uint j 8 255
147 end
148 end
149 emit nl
150 end
151 fn main to int
152 initk
153 note the empty message, as a slice of length 0
154 md5 part m0 0 0
155 md5 m0
156 md5 m1
157 md5 m2
158 md5 m3
159 ret 0
160 end
161
162 {{out}}
163
164 `d41d8cd98f00b204e9800998ecf8427e`
165 `9e107d9d372bb6826bd81d3542a419d6`
166 `e38ca1d920c4b8b8d3946b2c72f01680`
167 `cca1bf66b09554e10f837838c3d3efb1`
168 `0f5c6c4e740bfcc08c3c26ccb2673d46`
169