PHPackages                             jasny/http-digest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. jasny/http-digest

ActiveLibrary

jasny/http-digest
=================

PSR-7 client and server middleware for HTTP Digest header creation and validation as described in RFC 3230

v1.2.1(6y ago)02512[2 issues](https://github.com/jasny/http-digest/issues)MITPHPPHP &gt;=7.2.0

Since Mar 2Pushed 5y agoCompare

[ Source](https://github.com/jasny/http-digest)[ Packagist](https://packagist.org/packages/jasny/http-digest)[ RSS](/packages/jasny-http-digest/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (9)Versions (5)Used By (0)

Jasny HTTP Digest
=================

[](#jasny-http-digest)

[![Build Status](https://camo.githubusercontent.com/a1a736bdf1679e9cd5b3b135bc3ff6f75c39bbfa104eac235b06f3589503c191/68747470733a2f2f7472617669732d63692e6f72672f6a61736e792f687474702d6469676573742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jasny/http-digest)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/43b01ee34d3e4805d881d3d8db9ddf0f4385e4d20b768edaa4c783440ba491c3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f687474702d6469676573742f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/http-digest/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d00dfd58adff7f79d22442d01910de37efb6e9484f363b0cedcbc0840b1084ca/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f687474702d6469676573742f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/http-digest/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/91fb8355ce424fc05f2a1bb829f6bba12578ce61032b58c3734db3747be3f629/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f687474702d6469676573742e737667)](https://packagist.org/packages/jasny/http-digest)[![Packagist License](https://camo.githubusercontent.com/42668f8a628ce494d179de278b5e6c12ffafc78bda3822d8123c5914e8f1f90a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f687474702d6469676573742e737667)](https://packagist.org/packages/jasny/http-digest)

PSR-7 client and server middleware for HTTP Digest header creation and validation as described in [RFC 3230](https://tools.ietf.org/html/rfc3230). Supports MD5, SHA, SHA-256 and SHA-512 ([RFC 5843](https://tools.ietf.org/html/rfc5843)).

The `Digest` header contains a hash of the body.

```
Digest: SHA=thvDyvhfIqlvFe+A9MYgxAfm1q5=

```

The Want-Digest message header field indicates the sender's desire to receive an instance digest on messages associated with the Request-URI.

```
Want-Digest: MD5;q=0.3, SHA;q=1

```

Installation
------------

[](#installation)

```
composer require jasny/http-digest

```

Usage
-----

[](#usage)

Create the `HttpDigest` service to create and verify digests. Give the server priorities for supported algorithms. This value should be similar to those in the `Want-Digest` header.

```
use Jasny\HttpDigest\HttpDigest;

$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
```

The priorities may also be specified as string.

```
$service = new HttpDigest("MD5;q=0.3, SHA;q=1");
```

The service for content negotiating may be created and passes in the constructor for proper DI.

```
use Jasny\HttpDigest\HttpDigest;

$negotiator = new DigestNegotiator();
$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"], $negotiator);
```

### Creating a digest

[](#creating-a-digest)

You can use the service to create a digest for content.

```
$digest = $service->create($body);
```

### Verifying a digest

[](#verifying-a-digest)

You can use the service to verify the digest.

```
$service->verify($body, $digest);
```

If the digest doesn't match or if the algorithm is unsupported, a `HttpDigestException` is thrown.

### Priorities and the `Want-Digest` header

[](#priorities-and-the-want-digest-header)

You can change the priorities using `withPriorities()`. This will create a new copy of the service.

```
$newService = $service->withPriorities(["MD5;q=0.3", "SHA;q=0.5", "SHA-256;q=1"]);
```

To get the configured priorities use `getPriorities()`. The `getWantDigest()` function returns the priorities in as a string in the format expected for `Wanted-Digest`.

```
$priorities = $service->getPriorities();
$header = $service->getWantDigest();
```

### Server middleware

[](#server-middleware)

Server middleware can be used to verify the digest of PSR-7 requests.

When the middleware is used, requests with a body (like `POST` or `GET` requests) must contain a `Digest` header. If the `Digest` header is missing, invalid or doesn't meet the requirements, the middleware will return a `400 Bad Request` response with a `With-Digest` header and the handler will not be called.

#### Single pass middleware (PSR-15)

[](#single-pass-middleware-psr-15)

The middleware implements the PSR-15 `MiddlewareInterface`. As PSR standard many new libraries support this type of middleware, for example [Zend Stratigility](https://docs.zendframework.com/zend-stratigility/).

You're required to supply a [PSR-17 response factory](https://www.php-fig.org/psr/psr-17/#22-responsefactoryinterface), to create a `400 Bad Request` response for requests with invalid signatures.

```
use Jasny\HttpDigest\HttpDigest;
use Jasny\HttpDigest\ServerMiddleware;
use Zend\Stratigility\MiddlewarePipe;
use Zend\Diactoros\ResponseFactory;

$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
$responseFactory = new ResponseFactory();
$middleware = new ServerMiddleware($service, $responseFactory);

$app = new MiddlewarePipe();
$app->pipe($middleware);
```

#### Double pass middleware

[](#double-pass-middleware)

Many PHP libraries support double pass middleware. These are callables with the following signature;

```
fn(ServerRequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
```

To get a callback to be used by libraries as [Jasny Router](https://github.com/jasny/router) and [Relay](http://relayphp.com/), use the `asDoublePass()` method.

```
use Jasny\HttpDigest\HttpDigest;
use Jasny\HttpDigest\ServerMiddleware;
use Relay\RelayBuilder;

$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
$middleware = new ServerMiddleware($service);

$relayBuilder = new RelayBuilder($resolver);
$relay = $relayBuilder->newInstance([
    $middleware->asDoublePass(),
]);

$response = $relay($request, $baseResponse);
```

### Client middleware

[](#client-middleware)

Client middleware can be used to sign requests send by PSR-7 compatible HTTP clients like [Guzzle](http://docs.guzzlephp.org) and [HTTPlug](http://docs.php-http.org).

```
use Jasny\HttpDigest\HttpDigest;
use Jasny\HttpDigest\ClientMiddleware;

$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
$middleware = new ClientMiddleware($service);
```

#### Double pass middleware

[](#double-pass-middleware-1)

The client middleware can be used by any client that does support double pass middleware. Such middleware are callables with the following signature;

```
fn(RequestInterface $request, ResponseInterface $response, callable $next): ResponseInterface
```

Most HTTP clients do not support double pass middleware, but a type of single pass instead. However more general purpose PSR-7 middleware libraries, like [Relay](http://relayphp.com/), do support double pass.

```
use Relay\RelayBuilder;

$relayBuilder = new RelayBuilder($resolver);
$relay = $relayBuilder->newInstance([
    $middleware->asDoublePass(),
]);

$response = $relay($request, $baseResponse);
```

*The client middleware does not conform to PSR-15 (single pass) as that is intended for server requests only.*

#### Guzzle

[](#guzzle)

[Guzzle](http://docs.guzzlephp.org) is the most popular HTTP Client for PHP. The middleware has a `forGuzzle()` method that creates a callback which can be used as Guzzle middleware.

```
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Client;
use Jasny\HttpDigest\HttpDigest;
use Jasny\HttpDigest\ClientMiddleware;

$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
$middleware = new ClientMiddleware($service);

$stack = new HandlerStack();
$stack->push($middleware->forGuzzle());

$client = new Client(['handler' => $stack]);
```

#### HTTPlug

[](#httplug)

[HTTPlug](http://docs.php-http.org/en/latest/httplug/introduction.html) is the HTTP client of PHP-HTTP. It allows you to write reusable libraries and applications that need an HTTP client without binding to a specific implementation.

The `forHttplug()` method for the middleware creates an object that can be used as HTTPlug plugin.

```
use Http\Discovery\HttpClientDiscovery;
use Http\Client\Common\PluginClient;
use Jasny\HttpDigest\HttpDigest;
use Jasny\HttpDigest\ClientMiddleware;

$service = new HttpDigest(["MD5;q=0.3", "SHA;q=1"]);
$middleware = new ClientMiddleware($service);

$pluginClient = new PluginClient(
    HttpClientDiscovery::find(),
    [
        $middleware->forHttplug(),
    ]
);
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~31 days

Total

4

Last Release

2533d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (12 commits)")

---

Tags

digestmiddlewarephp7psr-15psr-7rfc-3230

### Embed Badge

![Health badge](/badges/jasny-http-digest/health.svg)

```
[![Health](https://phpackages.com/badges/jasny-http-digest/health.svg)](https://phpackages.com/packages/jasny-http-digest)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[thecodingmachine/graphqlite

Write your GraphQL queries in simple to write controllers (using webonyx/graphql-php).

5723.1M30](/packages/thecodingmachine-graphqlite)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[windwalker/framework

The next generation PHP framework.

25639.1k1](/packages/windwalker-framework)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
