sha1(3tcl)
π sha1 β SHA1 Message-Digest Algorithm π
okay bestie, gather round, we're talking about sha1, a Tcl package from the tcllib world (it lives in the sha1 manual page, section 3tcl), and honestly? it's giving cryptographic icon energy. π
β¨ NAME β¨
sha1 β the SHA1 Message-Digest Algorithm. no cap, that's literally the whole name.
πΈ SYNOPSIS πΈ
package require Tcl 8.5 9
package require sha1 ?2.0.5?
::sha1::sha1 ?-hex|-bin? [ -channel channel | -file filename | ?--?
string ]
::sha1::hmac key string
::sha1::hmac ?-hex|-bin? -key key [ -channel channel | -file filename |
?--? string ]
::sha1::SHA1Init
::sha1::SHA1Update token data
::sha1::SHA1Final token
::sha1::HMACInit key
::sha1::HMACUpdate token data
::sha1::HMACFinal token
π¦ DESCRIPTION π¦
so this package gives you a Tcl implementation of the SHA1 message-digest algorithm, exactly as specified by FIPS PUB 180-1 (1). it takes a message and generates a 160-bit digest from the input. SHA1 is related to the MD4 algorithm (2), except it's been strengthend against certain types of cryptographic attack. lowkey important: SHA1 should be used in preference to MD4 or MD5 in new applications. periodt. π
this package also includes support for creating keyed message-digests using the HMAC algorithm from RFC 2104 (3), with SHA1 as the message-digest. obsessed. β¨
β οΈ BEWARE β οΈ the commands in this package expect binary data as their input. when a-fileis provided, that's handled by the commands themselves β they open the referenced file in binary mode. but when literal data, or a-channel, is provided instead, the command's caller is responsible for ensuring this fact. the necessary conversion command isencoding convertto utf-8 $string.
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 π
::sha1::sha1 ?-hex|-bin? [ -channel channel | -file filename | ?--? string ]β this one takes a message and hands you back the SHA1 digest of that message as a hexadecimal string. you can ask for the result as binary data instead by passing-bin.
the data to be hashed can be specified either as a string argument to the sha1 command, or as a filename, or as a pre-opened channel. if the -filename argument is given, then the file is opened, the data is 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, and the channel is not closed. NOTE: using 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 instead (see below, bestie, it's right there π«Ά).
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.
::sha1::hmac key string::sha1::hmac ?-hex|-bin? -key key [ -channel channel | -file filename | ?--? string ]β calculate a Hashed Message Authentication digest (HMAC) using the SHA1 digest algorithm. HMACs are described in RFC 2104 (3) and provide an SHA1 digest that includes a key. all options other than-keyare as for the::sha1::sha1command.
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 π§
the bucket metaphor, because it eats every time πͺ£β¨
for the programmer, the SHA1 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 SHA1 hash operates on a token (equivalent to the bucket). you call SHA1Init to obtain a token, and then call SHA1Update as many times as required to add data to the hash. to release any resources and obtain the hash value, you then call SHA1Final. an equivalent set of functions gives you a keyed digest (HMAC). π
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, if you have the Trf package then this can be used; otherwise there is a pure-tcl equivalent. the programming interface remains the same in all cases. iconic, honestly. π
the actual commands
::sha1::SHA1Initβ begins a new SHA1 hash. returns a token ID that must be used for the remaining functions.::sha1::SHA1Update token dataβ add data to the hash identified by token. callingSHA1Update $token "abcd"is equivalent to callingSHA1Update $token "ab"followed bySHA1Update $token "cb". see EXAMPLES. (yes bestie, it's chunking. she's flexible like that. π )::sha1::SHA1Final 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 20 bytes representing the 160 bit SHA1 digest value.::sha1::HMACInit keyβ this is equivalent to the::sha1::SHA1Initcommand except that it requires the key that will be included in the HMAC.::sha1::HMACUpdate token data::sha1::HMACFinal tokenβ these commands are identical to the SHA1 equivalent commands.
π EXAMPLES π
% sha1::sha1 "Tcl does SHA1"
285a6a91c45a9066bf39fcf24425796ef0b2a8bf
% sha1::hmac Sekret "Tcl does SHA1"
ae6251fa51b95b18cba2be95eb031d07475ff03c
% set tok [sha1::SHA1Init]
::sha1::1
% sha1::SHA1Update $tok "Tcl "
% sha1::SHA1Update $tok "does "
% sha1::SHA1Update $tok "SHA1"
% sha1::Hex [sha1::SHA1Final $tok]
285a6a91c45a9066bf39fcf24425796ef0b2a8bf
look at that output matching up, we love consistency in this house π
π REFERENCES π
- "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) - Rivest, R., "The MD4 Message Digest Algorithm", RFC 1320, MIT, April 1992. (
http://www.rfc-editor.org/rfc/rfc1320.txt) - 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 π
ngl, 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 want the glow-up 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. (iykyk π«Ά)
π SEE ALSO π
md4, md5, ripemd128, ripemd160
π₯ KEYWORDS π₯
FIPS 180-1, hashing, message-digest, rfc 2104, security, sha1
π CATEGORY π
Hashes, checksums, and encryption
π COPYRIGHT π
Copyright (c) 2005, Pat Thoyts <patthoyts@users.sourceforge.net>
and that's the whole page bestie!! this one's from tcllib, version 2.0.5, dated sha1(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) 2005, Pat Thoyts <patthoyts@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms