sha256(3tcl)

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

πŸ’– NAME πŸ’–

okay bestie, gather round β€” sha256 is the SHA256 Message-Digest Algorithm, and we're gonna be so normal about it πŸ’…


✨ SYNOPSIS ✨

package require Tcl 8.5 9

package require sha256 ?1.0.6?

::sha2::sha256  ?-hex|-bin?  [  -channel channel | -file filename | ?--?
string ]

::sha2::sha224 ?-hex|-bin? [ -channel channel | -file  filename  |  ?--?
string ]

::sha2::hmac key string

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

::sha2::SHA256Init

::sha2::SHA224Init

::sha2::SHA256Update token data

::sha2::SHA256Final token

::sha2::SHA224Final token

::sha2::HMACInit key

::sha2::HMACUpdate token data

::sha2::HMACFinal token

πŸ¦‹ DESCRIPTION πŸ¦‹

so this package is a whole Tcl implementation of the SHA256 and SHA224 message-digest algorithms, exactly as specified by FIPS PUB 180-1 (1). you feed it a message, it hands you back a 256-bit (or 224-bit) digest of that input. and like… the SHA2 girlies are related to the SHA1 algorithm, family reunion vibes 🌸

it ALSO includes support for making keyed message-digests using the HMAC algorithm from RFC 2104 (3), with SHA256 as the message-digest. iconic behaviour honestly πŸ‘‘

BEWARE 🚨 the commands in this package expect binary data as their input. when a -file is provided, the commands themselves make sure of that, because they open the referenced file in binary mode. but when literal data or a -channel is provided instead, the caller is responsible for making sure it's binary. the conversion command you need is encoding convertto utf-8 $string. no cap, do not skip this.
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 just simply wrong πŸ’€ Tcl 9 throws an error instead, i.e. expected byte sequence but character <location> was ...

πŸŽ€ COMMANDS πŸŽ€

  • ::sha2::sha256 ?-hex|-bin? [ -channel channel | -file filename | ?--? string ] β€” takes a message and returns the SHA256 digest of it as a hexadecimal string. you can ask for the result as binary data by giving -bin. ✨

the data to be hashed can be given either as a string argument to the sha256 command, or as a filename or a pre-opened channel. if the -filename argument is given, the file gets opened, the data read and hashed, and the file is closed. if the -channel argument is given, data is read from the channel until the end of file, and the channel is not closed. NOTE: using the channel or filename options results in the internal use of vwait β€” so to avoid nested event loops in Tk or tclhttpd applications you should use the incremental programming API (see below) 🫢

only one of -file, -channel or string should be given.

if the string to hash could get mistaken for an option (leading dash "-"), use the option -- before it to terminate option processing and force interpretation as a string. πŸ’…

  • ::sha2::sha224 ?-hex|-bin? [ -channel channel | -file filename | ?--? string ] β€” same energy as ::sha2::sha256, except it returns the SHA224 digest instead. πŸ¦‹
  • ::sha2::hmac key string
  • ::sha2::hmac ?-hex|-bin? -key key [ -channel channel | -file filename | ?--? string ] β€” calculates a Hashed Message Authentication digest (HMAC) using the SHA256 digest algorithm. HMACs are described in RFC 2104 (3) and give you an SHA256 digest that includes a key πŸ” every option other than -key works just like for the ::sha2::sha256 command.

if the string to hash could get mistaken for an option (leading dash "-"), use the option -- before it to terminate option processing and force interpretation as a string. πŸ’…


🌈 PROGRAMMING INTERFACE 🌈

for the programmer girlies: think of the SHA256 hash as a bucket you pour data into. when you're done pouring, you extract a value derived from everything that went in. the programming interface operates on a token (which is basically the bucket, bestie πŸͺ£). you call SHA256Init to get a token, then call SHA256Update as many times as you need to add data to the hash. to release any resources and get the hash value, you call SHA256Final. and there's an equivalent set of functions that gives you a keyed digest (HMAC). periodt. ✨

if you have critcl and have built the tcllibc package, then the implementation of the hashing function will be performed by compiled code. failing that, there's a pure-tcl equivalent. the programming interface stays the same in all cases β€” she's consistent like that πŸ’β€β™€οΈ

  • ::sha2::SHA256Init
  • ::sha2::SHA224Init β€” begins a new SHA256/SHA224 hash. returns a token ID that must be used for the remaining functions. πŸŽ€
  • ::sha2::SHA256Update token data β€” add data to the hash identified by token. calling SHA256Update $token "abcd" is equivalent to calling SHA256Update $token "ab" followed by SHA256Update $token "cb". see EXAMPLES. note that this command is used for both SHA256 and SHA224 β€” only the initialization and finalization commands of the two hashes differ. πŸ‘―β€β™€οΈ
  • ::sha2::SHA256Final token
  • ::sha2::SHA224Final 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 32/28 bytes representing the 256/224 bit SHA256 / SHA224 digest value.
  • ::sha2::HMACInit key β€” this is equivalent to the ::sha2::SHA256Init command, except it requires the key that will be included in the HMAC. πŸ”‘
  • ::sha2::HMACUpdate token data
  • ::sha2::HMACFinal token β€” these commands are identical to the SHA256 equivalent commands. πŸ’–

πŸ“ EXAMPLES πŸ“

% sha2::sha256 "Tcl does SHA256"
0b91043ee484abd83c3e4b08d6034d71b937026379f0f59bda6e625e6e214789

% sha2::hmac Sekret "Tcl does SHA256"
4f9352c64d655e8a36abe73e6163a9d7a54039877c1c92ec90b07d48d4e854e0

% set tok [sha2::SHA256Init]
::sha2::1
% sha2::SHA256Update $tok "Tcl "
% sha2::SHA256Update $tok "does "
% sha2::SHA256Update $tok "SHA256"
% sha2::Hex [sha2::SHA256Final $tok]
0b91043ee484abd83c3e4b08d6034d71b937026379f0f59bda6e625e6e214789

look at that same digest showing up twice, we love a consistent queen πŸ‘‘


πŸ“š REFERENCES πŸ“š


πŸ™ˆ BUGS, IDEAS, FEEDBACK πŸ™ˆ

this document, and the package it describes, will undoubtedly contain bugs and other problems 😭 please report such in the category sha1 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 wanna hear it, fr fr 🫢

when proposing code changes, please provide unified diffs, i.e the output of diff -u.

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. πŸ’…


πŸ‘€ SEE ALSO πŸ‘€

md4, md5, ripemd128, ripemd160, sha1


⭐ KEYWORDS ⭐

FIPS 180-1, hashing, message-digest, rfc 2104, security, sha256


πŸ’œ CATEGORY πŸ’œ

Hashes, checksums, and encryption


Copyright (c) 2008, Andreas Kupries <andreas_kupries@users.sourceforge.net>


okay and that's the whole thing bestie, tysm for reading πŸ₯Ή this was tcllib version 1.0.6, dated the same, living under sha256(3tcl) β€” go forth and hash responsibly, mwah πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from