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