Skip to main content
Version: 4.0

DigestedData

One of the features the Cryptographic Message Syntax (CMS) provides is to act as a wrapper format for digested files.

Reading DigestedData CMS

The method for loading a DigestedData CMS is the same as loading any other CMS type:

use phpseclib4\File\CMS;

$cms = CMS::load(file_get_contents('digested.pem'));

print_r($cms);
(download digested.pem)

$cms, in this case, is a CMS\DigestedData object which, in turn, is basically a thin wrapper around an instance of \phpseclib4\File\ASN1\Constructed. Passing $cms through print_r() or var_dump() triggers the __debugInfo() magic method which, in turn, produces the following output:

contentType
phpseclib4\File\ASN1\Types\OID
id-digestedData
content
version
phpseclib4\File\ASN1\Types\Integer
v0
digestAlgorithm
algorithm
phpseclib4\File\ASN1\Types\OID
id-sha256
encapContentInfo
eContentType
phpseclib4\File\ASN1\Types\OID
id-data
eContent
phpseclib4\File\ASN1\Types\OctetString
68656c6c6f2c20776f726c64210a
digest
phpseclib4\File\ASN1\Types\OctetString
4dca0fd5f424a31b03ab807cbae77eb32bf2d089eed1cee154b3afed458de0dc

Confirming digests

The digested content can be validated by calling $cms->validate().

Creating DigestedData

use phpseclib4\File\CMS;

$cms = new CMS\DigestedData(file_get_contents('...'));
echo $cms;

The hash defaults to sha256 but can be set via a second parameter. eg.

$cms = new CMS\DigestedData(file_get_contents('...'), 'sha512');

Output Format

By default, CMS objects, when cast to a string, are converted to base64-encoded PEMs. To make it so that casting to a string produces a binary encoded DERs one need simply call CMS::enableBinaryOutput(). To go back to PEMs calling CMS::disableBinaryOutput() is sufficient.

You can also convert a CMS object to binary by doing $cms->toString(['binary' => true]).