interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / md5 / md5.om

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