Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DkimSigner.php

function DkimSigner::hashBody

1 call to DkimSigner::hashBody()
DkimSigner::sign in vendor/symfony/mime/Crypto/DkimSigner.php

File

vendor/symfony/mime/Crypto/DkimSigner.php, line 144

Class

DkimSigner
@author Fabien Potencier <fabien@symfony.com>

Namespace

Symfony\Component\Mime\Crypto

Code

private function hashBody(AbstractPart $body, string $bodyCanon, int $maxLength) : array {
    $hash = hash_init('sha256');
    $relaxed = self::CANON_RELAXED === $bodyCanon;
    $currentLine = '';
    $emptyCounter = 0;
    $isSpaceSequence = false;
    $length = 0;
    foreach ($body->bodyToIterable() as $chunk) {
        $canon = '';
        for ($i = 0, $len = \strlen($chunk); $i < $len; ++$i) {
            switch ($chunk[$i]) {
                case "\r":
                    break;
                case "\n":
                    // previous char is always \r
                    if ($relaxed) {
                        $isSpaceSequence = false;
                    }
                    if ('' === $currentLine) {
                        ++$emptyCounter;
                    }
                    else {
                        $currentLine = '';
                        $canon .= "\r\n";
                    }
                    break;
                case ' ':
                case "\t":
                    if ($relaxed) {
                        $isSpaceSequence = true;
                        break;
                    }
                // no break
                default:
                    if ($emptyCounter > 0) {
                        $canon .= str_repeat("\r\n", $emptyCounter);
                        $emptyCounter = 0;
                    }
                    if ($isSpaceSequence) {
                        $currentLine .= ' ';
                        $canon .= ' ';
                        $isSpaceSequence = false;
                    }
                    $currentLine .= $chunk[$i];
                    $canon .= $chunk[$i];
            }
        }
        if ($length + \strlen($canon) >= $maxLength) {
            $canon = substr($canon, 0, $maxLength - $length);
            $length += \strlen($canon);
            hash_update($hash, $canon);
            break;
        }
        $length += \strlen($canon);
        hash_update($hash, $canon);
    }
    // Add trailing Line return if last line is non empty
    if ('' !== $currentLine) {
        hash_update($hash, "\r\n");
        $length += \strlen("\r\n");
    }
    if (!$relaxed && 0 === $length) {
        hash_update($hash, "\r\n");
        $length = 2;
    }
    return [
        hash_final($hash, true),
        $length,
    ];
}
RSS feed
Powered by Drupal