{
    "mode": "perldoc",
    "parameter": "Digest::SHA3",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Digest%3A%3ASHA3/json",
    "generated": "2026-05-30T07:10:20Z",
    "synopsis": "In programs:\n# Functional interface\nuse Digest::SHA3 qw(sha3224 sha3256hex sha3512base64 ...);\n$digest = sha3224($data);\n$digest = sha3256hex($data);\n$digest = sha3384base64($data);\n$digest = sha3512($data);\n# Object-oriented\nuse Digest::SHA3;\n$sha3 = Digest::SHA3->new($alg);\n$sha3->add($data);              # feed data into stream\n$sha3->addfile(*F);\n$sha3->addfile($filename);\n$sha3->addbits($bits);\n$sha3->addbits($data, $nbits);\n$digest = $sha3->digest;        # compute digest\n$digest = $sha3->hexdigest;\n$digest = $sha3->b64digest;\n# Compute extendable-length digest\n$sha3 = Digest::SHA3->new(128000)->add($data);  # SHAKE128\n$digest  = $sha3->squeeze;\n$digest .= $sha3->squeeze;\n...\n$sha3 = Digest::SHA3->new(256000)->add($data);  # SHAKE256\n$digest  = $sha3->squeeze;\n$digest .= $sha3->squeeze;\n...",
    "sections": {
        "NAME": {
            "content": "Digest::SHA3 - Perl extension for SHA-3\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "In programs:\n\n# Functional interface\n\nuse Digest::SHA3 qw(sha3224 sha3256hex sha3512base64 ...);\n\n$digest = sha3224($data);\n$digest = sha3256hex($data);\n$digest = sha3384base64($data);\n$digest = sha3512($data);\n\n# Object-oriented\n\nuse Digest::SHA3;\n\n$sha3 = Digest::SHA3->new($alg);\n\n$sha3->add($data);              # feed data into stream\n\n$sha3->addfile(*F);\n$sha3->addfile($filename);\n\n$sha3->addbits($bits);\n$sha3->addbits($data, $nbits);\n\n$digest = $sha3->digest;        # compute digest\n$digest = $sha3->hexdigest;\n$digest = $sha3->b64digest;\n\n# Compute extendable-length digest\n\n$sha3 = Digest::SHA3->new(128000)->add($data);  # SHAKE128\n$digest  = $sha3->squeeze;\n$digest .= $sha3->squeeze;\n...\n\n$sha3 = Digest::SHA3->new(256000)->add($data);  # SHAKE256\n$digest  = $sha3->squeeze;\n$digest .= $sha3->squeeze;\n...\n",
            "subsections": []
        },
        "ABSTRACT": {
            "content": "Digest::SHA3 is a complete implementation of the NIST SHA-3\ncryptographic hash function, as specified in FIPS 202 (SHA-3 Standard:\nPermutation-Based Hash and Extendable-Output Functions).\n\nThe module gives Perl programmers a convenient way to calculate\nSHA3-224, SHA3-256, SHA3-384, and SHA3-512 message digests, as well as\nvariable-length hashes using SHAKE128 and SHAKE256. Digest::SHA3 can\nhandle all types of input, including partial-byte data.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Digest::SHA3 is written in C for speed. If your platform lacks a C\ncompiler, perhaps you can find the module in a binary form compatible\nwith your particular processor and operating system.\n\nThe programming interface is easy to use: it's the same one found in\nCPAN's Digest module. So, if your applications currently use Digest::SHA\nand you'd prefer the newer flavor of the NIST standard, it's a simple\nmatter to convert them.\n\nThe interface provides two ways to calculate digests: all-at-once, or in\nstages. To illustrate, the following short program computes the SHA3-256\ndigest of \"hello world\" using each approach:\n\nuse Digest::SHA3 qw(sha3256hex);\n\n$data = \"hello world\";\n@frags = split(//, $data);\n\n# all-at-once (Functional style)\n$digest1 = sha3256hex($data);\n\n# in-stages (OOP style)\n$state = Digest::SHA3->new(256);\nfor (@frags) { $state->add($) }\n$digest2 = $state->hexdigest;\n\nprint $digest1 eq $digest2 ?\n\"that's the ticket!\\n\" : \"oops!\\n\";\n\nTo calculate the digest of an n-bit message where *n* is not a multiple\nof 8, use the *addbits()* method. For example, consider the 446-bit\nmessage consisting of the bit-string \"110\" repeated 148 times, followed\nby \"11\". Here's how to display its SHA3-512 digest:\n\nuse Digest::SHA3;\n$bits = \"110\" x 148 . \"11\";\n$sha3 = Digest::SHA3->new(512)->addbits($bits);\nprint $sha3->hexdigest, \"\\n\";\n\nNote that for larger bit-strings, it's more efficient to use the\ntwo-argument version *addbits($data, $nbits)*, where *$data* is in the\ncustomary packed binary format used for Perl strings.\n",
            "subsections": []
        },
        "UNICODE AND SIDE EFFECTS": {
            "content": "Perl supports Unicode strings as of version 5.6. Such strings may\ncontain wide characters: namely, characters whose ordinal values are\ngreater than 255. This can cause problems for digest algorithms such as\nSHA-3 that are specified to operate on sequences of bytes.\n\nThe rule by which Digest::SHA3 handles a Unicode string is easy to\nstate, but potentially confusing to grasp: the string is interpreted as\na sequence of byte values, where each byte value is equal to the ordinal\nvalue (viz. code point) of its corresponding Unicode character. That\nway, the Unicode string 'abc' has exactly the same digest value as the\nordinary string 'abc'.\n\nSince a wide character does not fit into a byte, the Digest::SHA3\nroutines croak if they encounter one. Whereas if a Unicode string\ncontains no wide characters, the module accepts it quite happily. The\nfollowing code illustrates the two cases:\n\n$str1 = pack('U*', (0..255));\nprint sha3224hex($str1);              # ok\n\n$str2 = pack('U*', (0..256));\nprint sha3224hex($str2);              # croaks\n\nBe aware that the digest routines silently convert UTF-8 input into its\nequivalent byte sequence in the native encoding (cf. utf8::downgrade).\nThis side effect influences only the way Perl stores the data\ninternally, but otherwise leaves the actual value of the data intact.\n",
            "subsections": []
        },
        "PADDING OF BASE64 DIGESTS": {
            "content": "By convention, CPAN Digest modules do not pad their Base64 output.\nProblems can occur when feeding such digests to other software that\nexpects properly padded Base64 encodings.\n\nFor the time being, any necessary padding must be done by the user.\nFortunately, this is a simple operation: if the length of a\nBase64-encoded digest isn't a multiple of 4, simply append \"=\"\ncharacters to the end of the digest until it is:\n\nwhile (length($b64digest) % 4) {\n$b64digest .= '=';\n}\n\nTo illustrate, *sha3256base64(\"abc\")* is computed to be\n\nOphdp0/iJbIEXBcta9OQvYVfCG4+nVJbRr/iRRFDFTI\n\nwhich has a length of 43. So, the properly padded version is\n\nOphdp0/iJbIEXBcta9OQvYVfCG4+nVJbRr/iRRFDFTI=\n",
            "subsections": []
        },
        "EXPORT": {
            "content": "None by default.\n",
            "subsections": []
        },
        "EXPORTABLE FUNCTIONS": {
            "content": "Provided your C compiler supports a 64-bit type (e.g. the *long long* of\nC99, or *int64* used by Microsoft C/C++), all of these functions will\nbe available for use. Otherwise you won't be able to perform any of\nthem.\n\nIn the interest of simplicity, maintainability, and small code size,\nit's unlikely that future versions of this module will support a 32-bit\nimplementation. Older platforms using 32-bit-only compilers should\ncontinue to favor 32-bit hash implementations such as SHA-1, SHA-224, or\nSHA-256. The desire to use the SHA-3 hash standard, dating from 2015,\nshould reasonably require that one's compiler adhere to programming\nlanguage standards dating from at least 1999.\n\n*Functional style*\n\nsha3224($data, ...)\nsha3256($data, ...)\nsha3384($data, ...)\nsha3512($data, ...)\nshake128($data, ...)\nshake256($data, ...)\nLogically joins the arguments into a single string, and returns its\nSHA3-0/224/256/384/512 digest encoded as a binary string.\n\nThe digest size for shake128 is 1344 bits (168 bytes); for shake256,\nit's 1088 bits (136 bytes). To obtain extendable-output from the\nSHAKE algorithms, use the object-oriented interface with repeated\ncalls to the *squeeze* method.\n\nsha3224hex($data, ...)\nsha3256hex($data, ...)\nsha3384hex($data, ...)\nsha3512hex($data, ...)\nshake128hex($data, ...)\nshake256hex($data, ...)\nLogically joins the arguments into a single string, and returns its\nSHA3-0/224/256/384/512 or SHAKE128/256 digest encoded as a\nhexadecimal string.\n\nsha3224base64($data, ...)\nsha3256base64($data, ...)\nsha3384base64($data, ...)\nsha3512base64($data, ...)\nshake128base64($data, ...)\nshake256base64($data, ...)\nLogically joins the arguments into a single string, and returns its\nSHA3-0/224/256/384/512 or SHAKE128/256 digest encoded as a Base64\nstring.\n\nIt's important to note that the resulting string does not contain\nthe padding characters typical of Base64 encodings. This omission is\ndeliberate, and is done to maintain compatibility with the family of\nCPAN Digest modules. See \"PADDING OF BASE64 DIGESTS\" for details.\n\n*OOP style*\n\nnew($alg)\nReturns a new Digest::SHA3 object. Allowed values for *$alg* are\n224, 256, 384, and 512 for the SHA3 algorithms; or 128000 and 256000\nfor SHAKE128 and SHAKE256, respectively. If the argument is missing,\nSHA3-224 will be used by default.\n\nInvoking *new* as an instance method will not create a new object;\ninstead, it will simply reset the object to the initial state\nassociated with *$alg*. If the argument is missing, the object will\ncontinue using the same algorithm that was selected at creation.\n\nreset($alg)\nThis method has exactly the same effect as *new($alg)*. In fact,\n*reset* is just an alias for *new*.\n\nhashsize\nReturns the number of digest bits for this object. The values are\n224, 256, 384, 512, 1344, and 1088 for SHA3-224, SHA3-256, SHA3-384,\nSHA3-512, SHAKE128, and SHAKE256, respectively.\n\nalgorithm\nReturns the digest algorithm for this object. The values are 224,\n256, 384, 512, 128000, and 256000 for SHA3-224, SHA3-256, SHA3-384,\nSHA3-512, SHAKE128, and SHAKE256, respectively.\n\nclone\nReturns a duplicate copy of the object.\n\nadd($data, ...)\nLogically joins the arguments into a single string, and uses it to\nupdate the current digest state. In other words, the following\nstatements have the same effect:\n\n$sha3->add(\"a\"); $sha3->add(\"b\"); $sha3->add(\"c\");\n$sha3->add(\"a\")->add(\"b\")->add(\"c\");\n$sha3->add(\"a\", \"b\", \"c\");\n$sha3->add(\"abc\");\n\nThe return value is the updated object itself.\n\naddbits($data, $nbits [, $lsb])\naddbits($bits)\nUpdates the current digest state by appending bits to it. The return\nvalue is the updated object itself.\n\nThe first form causes the most-significant *$nbits* of *$data* to be\nappended to the stream. The *$data* argument is in the customary\nbinary format used for Perl strings. Setting the optional *$lsb*\nflag to a true value indicates that the final (partial) byte of\n*$data* is aligned with the least-significant bit; by default it's\naligned with the most-significant bit, as required by the parent\nDigest module.\n\nThe second form takes an ASCII string of \"0\" and \"1\" characters as\nits argument. It's equivalent to\n\n$sha3->addbits(pack(\"B*\", $bits), length($bits));\n\nSo, the following three statements do the same thing:\n\n$sha3->addbits(\"111100001010\");\n$sha3->addbits(\"\\xF0\\xA0\", 12);\n$sha3->addbits(\"\\xF0\\x0A\", 12, 1);\n\nSHA-3 uses least-significant-bit ordering for its internal\noperation. This means that\n\n$sha3->addbits(\"110\");\n\nis equivalent to\n\n$sha3->addbits(\"0\")->addbits(\"1\")->addbits(\"1\");\n\nMany public test vectors for SHA-3, such as the Keccak known-answer\ntests, are delivered in least-significant-bit format. Using the\noptional *$lsb* flag in these cases allows your code to be simpler\nand more efficient. See the test directory for examples.\n\nThe fact that SHA-2 and SHA-3 employ opposite bit-ordering schemes\nhas caused noticeable confusion in the programming community.\nExercise caution if your code examines individual bits in data\nstreams.\n\naddfile(*FILE)\nReads from *FILE* until EOF, and appends that data to the current\nstate. The return value is the updated object itself.\n\naddfile($filename [, $mode])\nReads the contents of *$filename*, and appends that data to the\ncurrent state. The return value is the updated object itself.\n\nBy default, *$filename* is simply opened and read; no special modes\nor I/O disciplines are used. To change this, set the optional\n*$mode* argument to one of the following values:\n\n\"b\"     read file in binary mode\n\n\"U\"     use universal newlines\n\n\"0\"     use BITS mode\n\nThe \"U\" mode is modeled on Python's \"Universal Newlines\" concept,\nwhereby DOS and Mac OS line terminators are converted internally to\nUNIX newlines before processing. This ensures consistent digest\nvalues when working simultaneously across multiple file systems. The\n\"U\" mode influences only text files, namely those passing Perl's\n*-T* test; binary files are processed with no translation\nwhatsoever.\n\nThe BITS mode (\"0\") interprets the contents of *$filename* as a\nlogical stream of bits, where each ASCII '0' or '1' character\nrepresents a 0 or 1 bit, respectively. All other characters are\nignored. This provides a convenient way to calculate the digest\nvalues of partial-byte data by using files, rather than having to\nwrite programs using the *addbits* method.\n\ndigest\nReturns the digest encoded as a binary string.\n\nNote that the *digest* method is a read-once operation. Once it has\nbeen performed, the Digest::SHA3 object is automatically reset in\npreparation for calculating another digest value. Call\n*$sha->clone->digest* if it's necessary to preserve the original\ndigest state.\n\nhexdigest\nReturns the digest encoded as a hexadecimal string.\n\nLike *digest*, this method is a read-once operation. Call\n*$sha->clone->hexdigest* if it's necessary to preserve the original\ndigest state.\n\nb64digest\nReturns the digest encoded as a Base64 string.\n\nLike *digest*, this method is a read-once operation. Call\n*$sha->clone->b64digest* if it's necessary to preserve the original\ndigest state.\n\nIt's important to note that the resulting string does not contain\nthe padding characters typical of Base64 encodings. This omission is\ndeliberate, and is done to maintain compatibility with the family of\nCPAN Digest modules. See \"PADDING OF BASE64 DIGESTS\" for details.\n\nsqueeze\nReturns the next 168 (136) bytes of the SHAKE128 (SHAKE256) digest\nencoded as a binary string. The *squeeze* method may be called\nrepeatedly to construct digests of any desired length.\n\nThis method is applicable only to SHAKE128 and SHAKE256 objects.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Digest, Digest::SHA, Digest::Keccak\n\nThe FIPS 202 SHA-3 Standard can be found at:\n\n<http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf>\n\nThe Keccak/SHA-3 specifications can be found at:\n\n<http://keccak.noekeon.org/Keccak-reference-3.0.pdf>\n<http://keccak.noekeon.org/Keccak-submission-3.pdf>\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Mark Shelor     <mshelor@cpan.org>\n",
            "subsections": []
        },
        "ACKNOWLEDGMENTS": {
            "content": "The author is particularly grateful to\n\nGuido Bertoni\nJoan Daemen\nMichael Peeters\nChris Skiscim\nGilles Van Assche\n\n\"Nothing is more fatiguing nor, in the long run, more exasperating than\nthe daily effort to believe things which daily become more incredible.\nTo be done with this effort is an indispensible condition of secure and\nlasting happiness.\" - Bertrand Russell\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "Copyright (C) 2012-2018 Mark Shelor\n\nThis library is free software; you can redistribute it and/or modify it\nunder the same terms as Perl itself.\n\nperlartistic\n",
            "subsections": []
        }
    },
    "summary": "Digest::SHA3 - Perl extension for SHA-3",
    "flags": [],
    "examples": [],
    "see_also": []
}