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