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