{
    "mode": "perldoc",
    "parameter": "Digest::SHA3",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Digest%3A%3ASHA3/json",
    "generated": "2026-06-11T19:17:24Z",
    "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 cryptographic hash function, as\nspecified in FIPS 202 (SHA-3 Standard: Permutation-Based Hash and Extendable-Output Functions).\n\nThe module gives Perl programmers a convenient way to calculate SHA3-224, SHA3-256, SHA3-384,\nand SHA3-512 message digests, as well as variable-length hashes using SHAKE128 and SHAKE256.\nDigest::SHA3 can handle 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 compiler, perhaps you can\nfind the module in a binary form compatible with your particular processor and operating system.\n\nThe programming interface is easy to use: it's the same one found in CPAN's Digest module. So,\nif your applications currently use Digest::SHA and you'd prefer the newer flavor of the NIST\nstandard, it's a simple matter to convert them.\n\nThe interface provides two ways to calculate digests: all-at-once, or in stages. To illustrate,\nthe following short program computes the SHA3-256 digest 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 of 8, use the\n*addbits()* method. For example, consider the 446-bit message consisting of the bit-string\n\"110\" repeated 148 times, followed by \"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 two-argument version\n*addbits($data, $nbits)*, where *$data* is in the customary packed binary format used for Perl\nstrings.\n",
            "subsections": []
        },
        "UNICODE AND SIDE EFFECTS": {
            "content": "Perl supports Unicode strings as of version 5.6. Such strings may contain wide characters:\nnamely, characters whose ordinal values are greater than 255. This can cause problems for digest\nalgorithms such as SHA-3 that are specified to operate on sequences of bytes.\n\nThe rule by which Digest::SHA3 handles a Unicode string is easy to state, but potentially\nconfusing to grasp: the string is interpreted as a sequence of byte values, where each byte\nvalue is equal to the ordinal value (viz. code point) of its corresponding Unicode character.\nThat way, the Unicode string 'abc' has exactly the same digest value as the ordinary string\n'abc'.\n\nSince a wide character does not fit into a byte, the Digest::SHA3 routines croak if they\nencounter one. Whereas if a Unicode string contains no wide characters, the module accepts it\nquite happily. The following 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 equivalent byte sequence\nin the native encoding (cf. utf8::downgrade). This side effect influences only the way Perl\nstores the data internally, 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. Problems can occur when\nfeeding such digests to other software that expects properly padded Base64 encodings.\n\nFor the time being, any necessary padding must be done by the user. Fortunately, this is a\nsimple operation: if the length of a Base64-encoded digest isn't a multiple of 4, simply append\n\"=\" characters 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 C99, or *int64* used\nby Microsoft C/C++), all of these functions will be available for use. Otherwise you won't be\nable to perform any of them.\n\nIn the interest of simplicity, maintainability, and small code size, it's unlikely that future\nversions of this module will support a 32-bit implementation. Older platforms using 32-bit-only\ncompilers should continue 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, should reasonably require\nthat one's compiler adhere to programming language standards dating from at least 1999.\n\n*Functional style*\n",
            "subsections": [
                {
                    "name": "sha3_224",
                    "content": ""
                },
                {
                    "name": "sha3_256",
                    "content": ""
                },
                {
                    "name": "sha3_384",
                    "content": ""
                },
                {
                    "name": "sha3_512",
                    "content": ""
                },
                {
                    "name": "shake128",
                    "content": ""
                },
                {
                    "name": "shake256",
                    "content": "Logically joins the arguments into a single string, and returns its SHA3-0/224/256/384/512\ndigest encoded as a binary string.\n\nThe digest size for shake128 is 1344 bits (168 bytes); for shake256, it's 1088 bits (136\nbytes). To obtain extendable-output from the SHAKE algorithms, use the object-oriented\ninterface with repeated calls to the *squeeze* method.\n"
                },
                {
                    "name": "sha3_224_hex",
                    "content": ""
                },
                {
                    "name": "sha3_256_hex",
                    "content": ""
                },
                {
                    "name": "sha3_384_hex",
                    "content": ""
                },
                {
                    "name": "sha3_512_hex",
                    "content": ""
                },
                {
                    "name": "shake128_hex",
                    "content": ""
                },
                {
                    "name": "shake256_hex",
                    "content": "Logically joins the arguments into a single string, and returns its SHA3-0/224/256/384/512\nor SHAKE128/256 digest encoded as a hexadecimal string.\n"
                },
                {
                    "name": "sha3_224_base64",
                    "content": ""
                },
                {
                    "name": "sha3_256_base64",
                    "content": ""
                },
                {
                    "name": "sha3_384_base64",
                    "content": ""
                },
                {
                    "name": "sha3_512_base64",
                    "content": ""
                },
                {
                    "name": "shake128_base64",
                    "content": ""
                },
                {
                    "name": "shake256_base64",
                    "content": "Logically joins the arguments into a single string, and returns its SHA3-0/224/256/384/512\nor SHAKE128/256 digest encoded as a Base64 string.\n\nIt's important to note that the resulting string does not contain the padding characters\ntypical of Base64 encodings. This omission is deliberate, and is done to maintain\ncompatibility with the family of CPAN Digest modules. See \"PADDING OF BASE64 DIGESTS\" for\ndetails.\n\n*OOP style*\n"
                },
                {
                    "name": "new",
                    "content": "Returns a new Digest::SHA3 object. Allowed values for *$alg* are 224, 256, 384, and 512 for\nthe SHA3 algorithms; or 128000 and 256000 for SHAKE128 and SHAKE256, respectively. If the\nargument is missing, SHA3-224 will be used by default.\n\nInvoking *new* as an instance method will not create a new object; instead, it will simply\nreset the object to the initial state associated with *$alg*. If the argument is missing,\nthe object will continue using the same algorithm that was selected at creation.\n"
                },
                {
                    "name": "reset",
                    "content": "This method has exactly the same effect as *new($alg)*. In fact, *reset* is just an alias\nfor *new*.\n\nhashsize\nReturns the number of digest bits for this object. The values are 224, 256, 384, 512, 1344,\nand 1088 for SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, and SHAKE256, respectively.\n\nalgorithm\nReturns the digest algorithm for this object. The values are 224, 256, 384, 512, 128000, and\n256000 for SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, and SHAKE256, respectively.\n\nclone\nReturns a duplicate copy of the object.\n"
                },
                {
                    "name": "add",
                    "content": "Logically joins the arguments into a single string, and uses it to update the current digest\nstate. In other words, the following statements 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"
                },
                {
                    "name": "add_bits",
                    "content": ""
                },
                {
                    "name": "add_bits",
                    "content": "Updates the current digest state by appending bits to it. The return value is the updated\nobject itself.\n\nThe first form causes the most-significant *$nbits* of *$data* to be appended to the stream.\nThe *$data* argument is in the customary binary format used for Perl strings. Setting the\noptional *$lsb* flag to a true value indicates that the final (partial) byte of *$data* is\naligned with the least-significant bit; by default it's aligned with the most-significant\nbit, as required by the parent Digest module.\n\nThe second form takes an ASCII string of \"0\" and \"1\" characters as its argument. It's\nequivalent 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 operation. 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 tests, are delivered in\nleast-significant-bit format. Using the optional *$lsb* flag in these cases allows your code\nto be simpler and more efficient. See the test directory for examples.\n\nThe fact that SHA-2 and SHA-3 employ opposite bit-ordering schemes has caused noticeable\nconfusion in the programming community. Exercise caution if your code examines individual\nbits in data streams.\n"
                },
                {
                    "name": "addfile",
                    "content": "Reads from *FILE* until EOF, and appends that data to the current state. The return value is\nthe updated object itself.\n"
                },
                {
                    "name": "addfile",
                    "content": "Reads the contents of *$filename*, and appends that data to the current state. The return\nvalue is the updated object itself.\n\nBy default, *$filename* is simply opened and read; no special modes or I/O disciplines are\nused. To change this, set the optional *$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, whereby DOS and Mac OS\nline terminators are converted internally to UNIX newlines before processing. This ensures\nconsistent digest values when working simultaneously across multiple file systems. The \"U\"\nmode influences only text files, namely those passing Perl's *-T* test; binary files are\nprocessed with no translation whatsoever.\n\nThe BITS mode (\"0\") interprets the contents of *$filename* as a logical stream of bits,\nwhere each ASCII '0' or '1' character represents a 0 or 1 bit, respectively. All other\ncharacters are ignored. This provides a convenient way to calculate the digest values of\npartial-byte data by using files, rather than having to write programs using the *addbits*\nmethod.\n\ndigest\nReturns the digest encoded as a binary string.\n\nNote that the *digest* method is a read-once operation. Once it has been performed, the\nDigest::SHA3 object is automatically reset in preparation for calculating another digest\nvalue. Call *$sha->clone->digest* if it's necessary to preserve the original digest state.\n\nhexdigest\nReturns the digest encoded as a hexadecimal string.\n\nLike *digest*, this method is a read-once operation. Call *$sha->clone->hexdigest* if it's\nnecessary to preserve the original digest state.\n\nb64digest\nReturns the digest encoded as a Base64 string.\n\nLike *digest*, this method is a read-once operation. Call *$sha->clone->b64digest* if it's\nnecessary to preserve the original digest state.\n\nIt's important to note that the resulting string does not contain the padding characters\ntypical of Base64 encodings. This omission is deliberate, and is done to maintain\ncompatibility with the family of CPAN Digest modules. See \"PADDING OF BASE64 DIGESTS\" for\ndetails.\n\nsqueeze\nReturns the next 168 (136) bytes of the SHAKE128 (SHAKE256) digest encoded as a binary\nstring. The *squeeze* method may be called repeatedly to construct digests of any desired\nlength.\n\nThis method is applicable only to SHAKE128 and SHAKE256 objects.\n"
                }
            ]
        },
        "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 the daily effort to\nbelieve things which daily become more incredible. To be done with this effort is an\nindispensible condition of secure and lasting 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 under the same terms as\nPerl itself.\n\nperlartistic\n",
            "subsections": []
        }
    },
    "summary": "Digest::SHA3 - Perl extension for SHA-3",
    "flags": [],
    "examples": [],
    "see_also": []
}