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);
$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
content
version
phpseclib4\File\ASN1\Types\Integer
digestAlgorithm
algorithm
phpseclib4\File\ASN1\Types\OID
encapContentInfo
eContentType
phpseclib4\File\ASN1\Types\OID
eContent
phpseclib4\File\ASN1\Types\OctetString
digest
phpseclib4\File\ASN1\Types\OctetString
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]).