sha256(n)
π sha256 β SHA256 Message-Digest Algorithm π
okay bestie, sit down, hydrate, because we're about to talk about sha256(n) β the Tcl package that does the SHA256 + SHA224 message-digest thing. it's giving cryptography, it's giving FIPS, it's giving "please read the BEWARE section, i'm begging you" π«Ά
β¨ 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 Tcl implementation of the SHA256 and SHA224 message-digest algorithms, exactly as specified by FIPS PUB 180-1 (1). they take a message and generate a 256-bit (or 224-bit) digest from the input. the SHA2 girlies are related to the SHA1 algorithm, if you were wondering. π
it also includes support for creating keyed message-digests using the HMAC algorithm from RFC 2104 (3), with SHA256 as the message-digest. iconic honestly.
β οΈ BEWARE β the commands in this package expect binary data as their input. when a-fileis provided, the commands themselves ensure this, because they open the referenced file in binary mode. but when literal data or a-channelis provided instead, then the command's caller is responsible for ensuring this fact. the necessary conversion command isencoding convertto utf-8 $string. no cap, don't skip this step.
π¨ 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. π 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 that message as a hexadecimal string. you may request the result as binary data by giving-bin. the data to be hashed can be specified either as a string argument to thesha256command, or as a filename, or as a pre-opened channel. if the-filenameargument is given then the file is opened, the data read and hashed, and the file is closed. if the-channelargument is given then data is read from the channel until the end of file. the channel is not closed.
β οΈ NOTE: use of the channel or filename options results in the internal use of vwait. 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 can be 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 ]β like::sha2::sha256, except that the SHA224 digest is returned.::sha2::hmac key string::sha2::hmac ?-hex|-bin? -key key [ -channel channel | -file filename | ?--? string ]β calculate a Hashed Message Authentication digest (HMAC) using the SHA256 digest algorithm. HMACs are described in RFC 2104 (3) and provide an SHA256 digest that includes a key. all options other than-keyare as for the::sha2::sha256command. if the string to hash can be 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, the SHA256 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. the programming interface to the SHA256 hash operates on a token (equivalent to the bucket). you call SHA256Init to obtain a token and then call SHA256Update as many times as required to add data to the hash. to release any resources and obtain the hash value, you then call SHA256Final. an equivalent set of functions gives you a keyed digest (HMAC). main character energy, honestly. π
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 is a pure-tcl equivalent. the programming interface remains the same in all cases. so you literally cannot tell the difference from the outside. slay.
::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. 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 both 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 that 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
π 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 love feedback bestie, 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 that's the whole page, we ate and left no crumbs π this cutie is brought to you by tcllib, version 1.0.6 β go forth and hash responsibly, bestie π¦β¨
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- 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