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 / sha-1.wiki

153 lines · 3146 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"
8text m0 "abc"
9text m1 "Rosetta Code"
10text m2 "The quick brown fox jumps over the lazy dog"
11text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
12global hs uint 5
13global ww uint 80
14global buf uchar 128
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitc writes one byte
27fn emitc c uchar to void
28b = new uchar 1 0
29put b 0 c
30emit b
31end
32note emith writes the byte v as two lower-case hex digits
33fn emith v uchar to void
34emitc get hexd shr v 4
35emitc get hexd and v 15
36end
37note rnd is step i of 80 on the state (a, b, c, d, e) in hs, with the expanded schedule in ww
38fn rnd i ulong to void
39a = get hs 0
40b = get hs 1
41c = get hs 2
42d = get hs 3
43f = uint 0
44k = uint 0
45if lt i 20
46f = or and b c and not b d
47k = 1518500249
48else
49if lt i 40
50f = xor xor b c d
51k = 1859775393
52else
53if lt i 60
54f = or or and b c and b d and c d
55k = 2400959708
56else
57f = xor xor b c d
58k = 3395469782
59end
60end
61end
62t = add add add add rotl a uint 5 f get hs 4 k get ww i
63put hs 4 d
64put hs 3 c
65put hs 2 rotl b uint 30
66put hs 1 a
67put hs 0 t
68end
69note block loads the 64 bytes of buf at p as 16 big-endian words, extends them to 80 and runs the steps
70fn block p ulong to void
71for w ulong 16
72v = uint 0
73for j ulong 4
74v = or shl v 8 uint get buf add add p mul w 4 j
75end
76put ww w v
77end
78for w ulong 64
79x = add w 16
80v = xor get ww sub x 3 get ww sub x 8
81v = xor v get ww sub x 14
82v = xor v get ww w
83put ww x rotl v uint 1
84end
85sv = new uint 5 0
86for k ulong 5
87put sv k get hs k
88end
89for i ulong 80
90rnd i
91end
92for k ulong 5
93put hs k add get hs k get sv k
94end
95end
96note digest prints the five state words big-endian in hex
97fn digest to void
98for k ulong 5
99v = get hs k
100for j ulong 4
101s = mul uint sub 3 j 8
102emith uchar and shr v s 255
103end
104end
105emit nl
106end
107note sha1 pads a (up to 119 bytes) with 0x80, zeros and the bit length (big-endian) to one or two
108note 64-byte blocks, hashes them and prints the digest in hex
109fn sha1 a view uchar * to void
110put hs 0 uint 1732584193
111put hs 1 uint 4023233417
112put hs 2 uint 2562383102
113put hs 3 uint 271733878
114put hs 4 uint 3285377520
115n = len a
116for i ulong 128
117put buf i 0
118end
119for i ulong n
120put buf i get a i
121end
122put buf n 128
123t = ulong 64
124if ge n 56
125t = 128
126end
127bits = mul n 8
128for j ulong 8
129put buf sub sub t 1 j uchar and shr bits mul j 8 255
130end
131for b ulong div t 64
132block mul b 64
133end
134digest
135end
136fn main to int
137note the empty message, as a slice of length 0
138sha1 part m0 0 0
139sha1 m0
140sha1 m1
141sha1 m2
142sha1 m3
143ret 0
144end
145</syntaxhighlight>
146{{out}}
147<pre>
148da39a3ee5e6b4b0d3255bfef95601890afd80709
149a9993e364706816aba3e25717850c26c9cd0d89d
15048c98f7e5a6e736d790ab740dfc3f51a61abe2b5
1512fd4e1c67a2d28fced849ee1bb76e7391b93eb12
152ed6c69d9e8b4373af86303dfaa3528dfbc129902
153</pre>