function Crypt::hmacBase64

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Utility/Crypt.php \Drupal\Component\Utility\Crypt::hmacBase64()
  2. 8.9.x core/lib/Drupal/Component/Utility/Crypt.php \Drupal\Component\Utility\Crypt::hmacBase64()
  3. 10 core/lib/Drupal/Component/Utility/Crypt.php \Drupal\Component\Utility\Crypt::hmacBase64()

Calculates a base-64 encoded, URL-safe sha-256 hmac.

Parameters

mixed $data: Scalar value to be validated with the hmac.

mixed $key: A secret key, this can be any scalar value.

Return value

string A base-64 encoded sha-256 hmac, with + replaced with -, / with _ and any = padding characters removed.

28 calls to Crypt::hmacBase64()
AssetGroupSetHashTrait::generateHash in core/lib/Drupal/Core/Asset/AssetGroupSetHashTrait.php
Generates a hash for an array of asset groups.
ContextualController::render in core/modules/contextual/src/ContextualController.php
Returns the requested rendered contextual links.
ContextualDynamicContextTest::createContextualIdToken in core/modules/contextual/tests/src/Functional/ContextualDynamicContextTest.php
Creates a contextual ID token.
ContextualLinksPlaceholder::preRenderPlaceholder in core/modules/contextual/src/Element/ContextualLinksPlaceholder.php
Pre-render callback: Renders a contextual links placeholder into #markup.
CryptTest::testHmacBase64 in core/tests/Drupal/Tests/Component/Utility/CryptTest.php
Tests HMAC generation.

... See full list

File

core/lib/Drupal/Component/Utility/Crypt.php, line 24

Class

Crypt
Utility class for cryptographically-secure string handling routines.

Namespace

Drupal\Component\Utility

Code

public static function hmacBase64($data, $key) {
    // $data and $key being strings here is necessary to avoid empty string
    // results of the hash function if they are not scalar values. As this
    // function is used in security-critical contexts like token validation it
    // is important that it never returns an empty string.
    if (!is_scalar($data) || !is_scalar($key)) {
        throw new \InvalidArgumentException('Both parameters passed to \\Drupal\\Component\\Utility\\Crypt::hmacBase64 must be scalar values.');
    }
    $hmac = base64_encode(hash_hmac('sha256', $data, $key, TRUE));
    // Modify the hmac so it's safe to use in URLs.
    return str_replace([
        '+',
        '/',
        '=',
    ], [
        '-',
        '_',
        '',
    ], $hmac);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.