Shop Learn P1 Docs P2 Docs Events
Snippet: MD5 — Parallax Forums

Snippet: MD5

{{
  MD5 Hash in Spin
  Written by Robert Quattlebaum <darco@deepdarc.com>
  Adapted from pseudo code from http://en.wikipedia.org/wiki/MD5.

  Converted to Spin2 by Terry E. Trapp, KE4PJW
  2023-01-15
}}
CON { Public Constants }
  HASH_LENGTH = 16 ' An MD5 hash is 16 bytes long
  BLOCK_LENGTH = 64 ' Block length is 64 bytes
DAT { Tables }

k       long  $D76AA478, $E8C7B756, $242070DB, $C1BDCEEE, $F57C0FAF, $4787C62A, $A8304613, $FD469501
        long  $698098D8, $8B44F7AF, $FFFF5BB1, $895CD7BE, $6B901122, $FD987193, $A679438E, $49B40821
        long  $F61E2562, $C040B340, $265E5A51, $E9B6C7AA, $D62F105D, $02441453, $D8A1E681, $E7D3FBC8
        long  $21E1CDE6, $C33707D6, $F4D50D87, $455A14ED, $A9E3E905, $FCEFA3F8, $676F02D9, $8D2A4C8A
        long  $FFFA3942, $8771F681, $6D9D6122, $FDE5380C, $A4BEEA44, $4BDECFA9, $F6BB4B60, $BEBFBC70
        long  $289B7EC6, $EAA127FA, $D4EF3085, $04881D05, $D9D4D039, $E6DB99E5, $1FA27CF8, $C4AC5665
        long  $F4292244, $432AFF97, $AB9423A7, $FC93A039, $655B59C3, $8F0CCC92, $FFEFF47D, $85845DD1
        long  $6FA87E4F, $FE2CE6E0, $A3014314, $4E0811A1, $F7537E82, $BD3AF235, $2AD7D2BB, $EB86D391

r       byte  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22
        byte  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20,  5,  9, 14, 20
        byte  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23,  4, 11, 16, 23
        byte  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21,  6, 10, 15, 21

initial_hash long $67452301, $EFCDAB89, $98BADCFE, $10325476


PUB hash(dataptr,datalen,h)
  hashstart(h)
  hashfinish(dataptr,datalen,datalen,h)

PUB hashStart(h)
  bytemove(h,@initial_hash,HASH_LENGTH)

PUB hashBlock(dataptr,h)|i,a,b,c,d,f,g,tmp
  bytemove(@a,h,HASH_LENGTH)

  repeat i from 0 to 63
    case i
      0 .. 15:
        f := (b & c) | ((! b) & d)
        g := i
      16 .. 31:
        f := (d & b) | ((! d) & c)
        g := (5*i + 1) & 15
      32 .. 47:
        f := b ^ c ^ d
        g := (3*i + 5) & 15
      48 .. 63:
        f := c ^ (b | (! d))
        g := (7*i) & 15
    f := f + a+ k[i] + LONG[dataptr][g]
    tmp := d
    d := c
    c := b
    b += f ROL= r[i]
    a := tmp

  LONG[h][0]+=a
  LONG[h][1]+=b
  LONG[h][2]+=c
  LONG[h][3]+=d

PUB hashFinish(dataptr,datalen,totallen,h)|a[BLOCK_LENGTH/4]
  repeat while datalen >= BLOCK_LENGTH
    hashBlock(dataptr,h)
    datalen-=BLOCK_LENGTH
    dataptr+=BLOCK_LENGTH
  bytefill(@a,0,BLOCK_LENGTH)
  bytemove(@a,dataptr,datalen)
  BYTE[@a][datalen]:=$80
  if datalen>BLOCK_LENGTH-9
    hashBlock(@a,h)
    bytefill(@a,0,BLOCK_LENGTH)
  LONG[@a][14]:=totallen*8
  hashBlock(@a,h)
  bytefill(@a,0,BLOCK_LENGTH)
CON
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                   TERMS OF USE: MIT License                                                  │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation    │
│files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,    │
│modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software│
│is furnished to do so, subject to the following conditions:                                                                   │
│                                                                                                                              │
│The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.│
│                                                                                                                              │
│THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE          │
│WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR         │
│COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,   │
│ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                         │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
 }}

Comments

  • ElectrodudeElectrodude Posts: 1,549
    edited 2023-01-17 01:00

    Why do you need MD5? It's been considered suspect since 1996 and "unsuitable for further use" since 2008. MD5 collisions can be quickly and easily generated by any modern computer.

    I might be able to help you find a more secure algorithm that's fast enough if you tell me what you need it for (input sizes, speed requirements, required security guarantees).

    Assuming you just want fast and secure, you could look at BLAKE2 or BLAKE3. The BLAKE2s variant might be good - it's optimized for 32-bit processors, while the others are mostly optimized for 64-bit processors with vector instructions. BLAKE3 is also 32-bit and can be parallelized across multiple cogs, too, but it's a lot newer. They're faster than MD5 on modern PCs, and they actually afford a meaningful level of security. If you only need a message authentication code (just collision resistance, i.e. for authenticatimg firmware upgrades) and not the other properties of typical cryptographic hash functions, you might be able to use SipHash, which should be faster but requires more careful use to be secure.

  • I needed it to validate files the prop is downloading from Azure Blob Storage. Blob Storage provides a Base64 encoded MD5 in the http headers. It is used just ensures that what was downloaded is most likely not corrupt.

    MD5 collisions have been easy to fabricate for quite some time and can't be trusted for anything security related.

Sign In or Register to comment.