// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. // Use of this source code is governed by an MIT license // that can be found in the LICENSE file. // Package md5 implements the MD5 hash algorithm as defined in RFC 1321. // MD5 is cryptographically broken and should not be used for secure // applications. // Adapted from: https://github.com/golang/go/blob/master/src/crypto/md5 module md5 import math import encoding.binary const ( // The size of an MD5 checksum in bytes. Size = 16 // The blocksize of MD5 in bytes. BlockSize = 64 ) const ( Init0 = 0x67452301 Init1 = 0xEFCDAB89 Init2 = 0x98BADCFE Init3 = 0x10325476 ) // Digest represents the partial evaluation of a checksum. struct Digest { mut: s []u32 x []byte nx int len u64 } fn (d mut Digest) reset() { d.s = [u32(0); 4] d.x = [byte(0); BlockSize] d.s[0] = u32(Init0) d.s[1] = u32(Init1) d.s[2] = u32(Init2) d.s[3] = u32(Init3) d.nx = 0 d.len = u64(0) } // New returns a new hash.Hash computing the MD5 checksum. pub fn new() *Digest { mut d := &Digest{} d.reset() return d } pub fn (d mut Digest) write(p []byte) ?int { nn := p.len d.len += u64(nn) if d.nx > 0 { n := int(math.min(f64(d.x.len), f64(p.len))) for i:=0; i= p.len { p = []byte } else { p = p.right(n) } } if p.len >= BlockSize { n := p.len &~ (BlockSize - 1) block_generic(d, p.left(n)) if n >= p.len { p = []byte } else { p = p.right(n) } } if p.len > 0 { d.nx = int(math.min(f64(d.x.len), f64(p.len))) for i:=0; i