md5(3tcl)

πŸ“¦
version 2.0 Β· 3 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ md5 β€” the MD5 Message-Digest Algorithm ✨

okay bestie, gather round, because this is the md5(3tcl) manual page and it's genuinely iconic. it's the Tcl girlie's guide to making message digests and she is serving. let's go πŸ’…


🌸 SYNOPSIS 🌸

package require Tcl 8.5 9

package require md5 ?2.0.9?

::md5::md5 ?-hex? [ -channel channel | -file filename | string ]

::md5::hmac ?-hex? -key key [ -channel channel | -file filename | string
]

::md5::MD5Init

::md5::MD5Update token data

::md5::MD5Final token

::md5::HMACInit key

::md5::HMACUpdate token data

::md5::HMACFinal token

πŸ¦‹ DESCRIPTION πŸ¦‹

So this package is an implementation in Tcl of the MD5 message-digest algorithm, exactly as described in RFC 1321 (1). what does that even mean? it means this algorithm takes an arbitrary quantity of data and generates a 128-bit message digest from the input. no cap, any amount, in goes the data, out comes the digest. ✨

MD5 is related to the MD4 algorithm (2) but it's been strengthened against certain types of potential attack. so like, MD5 got the glow-up. for new applications you should use MD5 in preference to MD4. periodt.

Now, the tea on performance β˜• β€” if you have critcl and you've built the tcllibc package, then the implementation of the hashing function will be performed by compiled code. alternatively, if you have either cryptkit or Trf, then either of these is used to accelerate the digest computation. and if no suitable compiled package is available? then the pure-Tcl implementation is used. the programming interface remains the same in all cases, so honestly you don't even have to care. slay.

heads up bestie, there was a glow-down-then-glow-up moment here. the previous version of this package always returned a hex encoded string. this has been changed to simplify the programming interface and to make this version more compatible with other implementations. to obtain the previous usage, either explicitly specify package version 1, or use the -hex option to the md5 command. iykyk. πŸŽ€
BEWARE 🚨 the commands in this package expect binary data as their input. when a -file is provided, then this is ensured by the commands themselves, as they open the referenced file in binary mode. but when literal data, or a -channel are provided instead, then the command's caller is responsible for ensuring this fact. the necessary conversion command is encoding convertto utf-8 $string. do not skip this step i am begging you 😭
ATTENTION, there is a Tcl 9 COMPATIBILITY ISSUE here. Tcl 8.x silently mishandles non-binary input by cutting it internally to size. i.e. by only using the low byte of a higher unicode point. no error is thrown, the result is simply wrong. the audacity?? πŸ’€ Tcl 9 throws an error instead, i.e. expected byte sequence but character <location> was ...

πŸ’œ COMMANDS πŸ’œ

  • ::md5::md5 ?-hex? [ -channel channel | -file filename | string ] β€” calculate the MD5 digest of the binary data given in string. this is returned as a binary string by default. giving the -hex option will return a hexadecimal encoded version of the digest. the data to be hashed can be specified either as a string argument to the md5 command, or as a filename, or a pre-opened channel. if the -filename argument is given then the file is opened, the data read and hashed, and the file is closed. if the -channel argument is given then data is read from the channel until the end of file. the channel is not closed. only one of -file, -channel or string should be given. pick one bestie, you can't have all three, that's not the vibe.
  • ::md5::hmac ?-hex? -key key [ -channel channel | -file filename | string ] β€” calculate a Hashed Message Authentication digest (HMAC) using the MD5 digest algorithm. HMACs are described in RFC 2104 (3) and provide an MD5 digest that includes a key. all options other than -key are as for the ::md5::md5 command. πŸ”‘

πŸ‘‘ PROGRAMMING INTERFACE πŸ‘‘

for the programmer, the MD5 hash can be viewed as a bucket into which one pours data. when you have finished, you extract a value that is derived from the data that was poured into the bucket. πŸͺ£ this is genuinely the cutest mental model in the whole manpage, not me being obsessed.

the programming interface to the MD5 hash operates on a token (equivalent to the bucket). you call MD5Init to obtain a token, and then call MD5Update as many times as required to add data to the hash. to release any resources and obtain the hash value, you then call MD5Final. and an equivalent set of functions gives you a keyed digest (HMAC). πŸ’«

  • ::md5::MD5Init β€” begins a new MD5 hash. returns a token ID that must be used for the remaining functions.
  • ::md5::MD5Update token data β€” add data to the hash identified by token. calling MD5Update $token "abcd" is equivalent to calling MD5Update $token "ab" followed by MD5Update $token "cb". see EXAMPLES. (yes it says "cb", that's not a typo, that's just how the chunking works, trust 🫢)
  • ::md5::MD5Final token β€” returns the hash value and releases any resources held by this token. once this command completes the token will be invalid. the result is a binary string of 16 bytes representing the 128 bit MD5 digest value. it's giving final form. ✨
  • ::md5::HMACInit key β€” this is equivalent to the ::md5::MD5Init command except that it requires the key that will be included in the HMAC.
  • ::md5::HMACUpdate token data β€” nothing extra to say here, keep scrolling πŸ‘‡
  • ::md5::HMACFinal token β€” these commands are identical to the MD5 equivalent commands.

🌈 EXAMPLES 🌈

% md5::md5 -hex "Tcl does MD5"
8AAC1EE01E20BB347104FABB90310433

% md5::hmac -hex -key Sekret "Tcl does MD5"
35BBA244FD56D3EDF5F3C47474DACB5D

% set tok [md5::MD5Init]
::md5::1
% md5::MD5Update $tok "Tcl "
% md5::MD5Update $tok "does "
% md5::MD5Update $tok "MD5"
% md5::Hex [md5::MD5Final $tok]
8AAC1EE01E20BB347104FABB90310433

see how the chunked-updates one lands on the exact same digest as the one-shot one? that's the bucket working exactly as advertised. chef's kiss. 😘


πŸ“š REFERENCES πŸ“š

  1. Rivest, R., "The MD5 Message-Digest Algorithm", RFC 1321, MIT and RSA Data Security, Inc, April 1992. (http://www.rfc-editor.org/rfc/rfc1321.txt)
  2. Rivest, R., "The MD4 Message Digest Algorithm", RFC 1320, MIT, April 1992. (http://www.rfc-editor.org/rfc/rfc1320.txt)
  3. Krawczyk, H., Bellare, M. and Canetti, R. "HMAC: Keyed-Hashing for Message Authentication", RFC 2104, February 1997. (http://www.rfc-editor.org/rfc/rfc2104.txt)

πŸ› BUGS, IDEAS, FEEDBACK πŸ›

this document, and the package it describes, will undoubtedly contain bugs and other problems. bestie, it said it, not me. 😭

please report such in the category md5 of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. please also report any ideas for enhancements you may have for either package and/or documentation. we love feedback fr fr.

when proposing code changes, please provide unified diffs, i.e the output of diff -u. this is non-negotiable, do not send me vibes, send me diffs.

note further that attachments are strongly preferred over inlined patches. attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. (the left-most one. yes that one. πŸ’…)


πŸ”— SEE ALSO πŸ”—

md4, sha1


🏷️ KEYWORDS 🏷️

hashing, md5, message-digest, rfc 1320, rfc 1321, rfc 2104, security


πŸ—‚οΈ CATEGORY πŸ—‚οΈ

Hashes, checksums, and encryption


Copyright (c) 2003, Pat Thoyts <patthoyts@users.sourceforge.net>


okay that's the whole page bestie, you're now officially fluent in md5. this was brought to you by tcllib, version 2.0.9, and it's giving certified banger energy. go forth, hash responsibly, and remember to encoding convertto utf-8 $string before you come for me. mwah πŸ«ΆπŸ’œ

πŸ“¦ where this page comes from