interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

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