sha256(3tcl)
π 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-fileis provided, the commands themselves make sure of that, because they open the referenced file in binary mode. but when literal data or a-channelis provided instead, the caller is responsible for making sure it's binary. the conversion command you need isencoding 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-keyworks just like for the::sha2::sha256command.
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β adddatato the hash identified bytoken. callingSHA256Update $token "abcd"is equivalent to callingSHA256Update $token "ab"followed bySHA256Update $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::SHA256Initcommand, except it requires thekeythat 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 π
- [1] "Secure Hash Standard", National Institute of Standards and Technology, U.S. Department Of Commerce, April 1995. (http://www.itl.nist.gov/fipspubs/fip180-1.htm)
- [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 π 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 πΈ
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
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2008, Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms