{
    "mode": "perldoc",
    "parameter": "Digest",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Digest/json",
    "generated": "2026-06-09T22:54:24Z",
    "synopsis": "$md5  = Digest->new(\"MD5\");\n$sha1 = Digest->new(\"SHA-1\");\n$sha256 = Digest->new(\"SHA-256\");\n$sha384 = Digest->new(\"SHA-384\");\n$sha512 = Digest->new(\"SHA-512\");\n$hmac = Digest->HMACMD5($key);",
    "sections": {
        "NAME": {
            "content": "Digest - Modules that calculate message digests\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "$md5  = Digest->new(\"MD5\");\n$sha1 = Digest->new(\"SHA-1\");\n$sha256 = Digest->new(\"SHA-256\");\n$sha384 = Digest->new(\"SHA-384\");\n$sha512 = Digest->new(\"SHA-512\");\n\n$hmac = Digest->HMACMD5($key);\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"Digest::\" modules calculate digests, also called \"fingerprints\" or \"hashes\", of some data,\ncalled a message. The digest is (usually) some small/fixed size string. The actual size of the\ndigest depend of the algorithm used. The message is simply a sequence of arbitrary bytes or\nbits.\n\nAn important property of the digest algorithms is that the digest is *likely* to change if the\nmessage change in some way. Another property is that digest functions are one-way functions,\nthat is it should be *hard* to find a message that correspond to some given digest. Algorithms\ndiffer in how \"likely\" and how \"hard\", as well as how efficient they are to compute.\n\nNote that the properties of the algorithms change over time, as the algorithms are analyzed and\nmachines grow faster. If your application for instance depends on it being \"impossible\" to\ngenerate the same digest for a different message it is wise to make it easy to plug in stronger\nalgorithms as the one used grow weaker. Using the interface documented here should make it easy\nto change algorithms later.\n\nAll \"Digest::\" modules provide the same programming interface. A functional interface for simple\nuse, as well as an object oriented interface that can handle messages of arbitrary length and\nwhich can read files directly.\n\nThe digest can be delivered in three formats:\n\n*binary*\nThis is the most compact form, but it is not well suited for printing or embedding in\nplaces that can't handle arbitrary data.\n\n*hex*   A twice as long string of lowercase hexadecimal digits.\n\n*base64*\nA string of portable printable characters. This is the base64 encoded representation of\nthe digest with any trailing padding removed. The string will be about 30% longer than\nthe binary version. MIME::Base64 tells you more about this encoding.\n\nThe functional interface is simply importable functions with the same name as the algorithm. The\nfunctions take the message as argument and return the digest. Example:\n\nuse Digest::MD5 qw(md5);\n$digest = md5($message);\n\nThere are also versions of the functions with \"hex\" or \"base64\" appended to the name, which\nreturns the digest in the indicated form.\n",
            "subsections": []
        },
        "OO INTERFACE": {
            "content": "The following methods are available for all \"Digest::\" modules:\n\n$ctx = Digest->XXX($arg,...)\n$ctx = Digest->new(XXX => $arg,...)\n$ctx = Digest::XXX->new($arg,...)\nThe constructor returns some object that encapsulate the state of the message-digest\nalgorithm. You can add data to the object and finally ask for the digest. The \"XXX\" should\nof course be replaced by the proper name of the digest algorithm you want to use.\n\nThe two first forms are simply syntactic sugar which automatically load the right module on\nfirst use. The second form allow you to use algorithm names which contains letters which are\nnot legal perl identifiers, e.g. \"SHA-1\". If no implementation for the given algorithm can\nbe found, then an exception is raised.\n\nTo know what arguments (if any) the constructor takes (the \"$args,...\" above) consult the\ndocs for the specific digest implementation.\n\nIf new() is called as an instance method (i.e. $ctx->new) it will just reset the state the\nobject to the state of a newly created object. No new object is created in this case, and\nthe return value is the reference to the object (i.e. $ctx).\n\n$otherctx = $ctx->clone\nThe clone method creates a copy of the digest state object and returns a reference to the\ncopy.\n\n$ctx->reset\nThis is just an alias for $ctx->new.\n\n$ctx->add( $data )\n$ctx->add( $chunk1, $chunk2, ... )\nThe string value of the $data provided as argument is appended to the message we calculate\nthe digest for. The return value is the $ctx object itself.\n\nIf more arguments are provided then they are all appended to the message, thus all these\nlines will have the same effect on the state of the $ctx object:\n\n$ctx->add(\"a\"); $ctx->add(\"b\"); $ctx->add(\"c\");\n$ctx->add(\"a\")->add(\"b\")->add(\"c\");\n$ctx->add(\"a\", \"b\", \"c\");\n$ctx->add(\"abc\");\n\nMost algorithms are only defined for strings of bytes and this method might therefore croak\nif the provided arguments contain chars with ordinal number above 255.\n\n$ctx->addfile( $iohandle )\nThe $iohandle is read until EOF and the content is appended to the message we calculate the\ndigest for. The return value is the $ctx object itself.\n\nThe addfile() method will croak() if it fails reading data for some reason. If it croaks it\nis unpredictable what the state of the $ctx object will be in. The addfile() method might\nhave been able to read the file partially before it failed. It is probably wise to discard\nor reset the $ctx object if this occurs.\n\nIn most cases you want to make sure that the $iohandle is in \"binmode\" before you pass it\nas argument to the addfile() method.\n\n$ctx->addbits( $data, $nbits )\n$ctx->addbits( $bitstring )\nThe addbits() method is an alternative to add() that allow partial bytes to be appended to\nthe message. Most users can just ignore this method since typical applications involve only\nwhole-byte data.\n\nThe two argument form of addbits() will add the first $nbits bits from $data. For the last\npotentially partial byte only the high order \"$nbits % 8\" bits are used. If $nbits is\ngreater than \"length($data) * 8\", then this method would do the same as \"$ctx->add($data)\".\n\nThe one argument form of addbits() takes a $bitstring of \"1\" and \"0\" chars as argument.\nIt's a shorthand for \"$ctx->addbits(pack(\"B*\", $bitstring), length($bitstring))\".\n\nThe return value is the $ctx object itself.\n\nThis example shows two calls that should have the same effect:\n\n$ctx->addbits(\"111100001010\");\n$ctx->addbits(\"\\xF0\\xA0\", 12);\n\nMost digest algorithms are byte based and for these it is not possible to add bits that are\nnot a multiple of 8, and the addbits() method will croak if you try.\n\n$ctx->digest\nReturn the binary digest for the message.\n\nNote that the \"digest\" operation is effectively a destructive, read-once operation. Once it\nhas been performed, the $ctx object is automatically \"reset\" and can be used to calculate\nanother digest value. Call $ctx->clone->digest if you want to calculate the digest without\nresetting the digest state.\n\n$ctx->hexdigest\nSame as $ctx->digest, but will return the digest in hexadecimal form.\n\n$ctx->b64digest\nSame as $ctx->digest, but will return the digest as a base64 encoded string without padding.\n\n$ctx->base64paddeddigest\nSame as $ctx->digest, but will return the digest as a base64 encoded string.\n",
            "subsections": []
        },
        "Digest speed": {
            "content": "This table should give some indication on the relative speed of different algorithms. It is\nsorted by throughput based on a benchmark done with of some implementations of this API:\n\nAlgorithm      Size    Implementation                  MB/s\n\nMD4            128     Digest::MD4 v1.3               165.0\nMD5            128     Digest::MD5 v2.33               98.8\nSHA-256        256     Digest::SHA2 v1.1.0             66.7\nSHA-1          160     Digest::SHA v4.3.1              58.9\nSHA-1          160     Digest::SHA1 v2.10              48.8\nSHA-256        256     Digest::SHA v4.3.1              41.3\nHaval-256      256     Digest::Haval256 v1.0.4         39.8\nSHA-384        384     Digest::SHA2 v1.1.0             19.6\nSHA-512        512     Digest::SHA2 v1.1.0             19.3\nSHA-384        384     Digest::SHA v4.3.1              19.2\nSHA-512        512     Digest::SHA v4.3.1              19.2\nWhirlpool      512     Digest::Whirlpool v1.0.2        13.0\nMD2            128     Digest::MD2 v2.03                9.5\n\nAdler-32        32     Digest::Adler32 v0.03            1.3\nCRC-16          16     Digest::CRC v0.05                1.1\nCRC-32          32     Digest::CRC v0.05                1.1\nMD5            128     Digest::Perl::MD5 v1.5           1.0\nCRC-CCITT       16     Digest::CRC v0.05                0.8\n\nThese numbers was achieved Apr 2004 with ActivePerl-5.8.3 running under Linux on a P4 2.8 GHz\nCPU. The last 5 entries differ by being pure perl implementations of the algorithms, which\nexplains why they are so slow.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Digest::Adler32, Digest::CRC, Digest::Haval256, Digest::HMAC, Digest::MD2, Digest::MD4,\nDigest::MD5, Digest::SHA, Digest::SHA1, Digest::SHA2, Digest::Whirlpool\n\nNew digest implementations should consider subclassing from Digest::base.\n\nMIME::Base64\n\nhttp://en.wikipedia.org/wiki/Cryptographichashfunction\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Gisle Aas <gisle@aas.no>\n\nThe \"Digest::\" interface is based on the interface originally developed by Neil Winton for his\n\"MD5\" module.\n\nThis library is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nCopyright 1998-2006 Gisle Aas.\nCopyright 1995,1996 Neil Winton.\n",
            "subsections": []
        }
    },
    "summary": "Digest - Modules that calculate message digests",
    "flags": [],
    "examples": [],
    "see_also": []
}