# Crypto Hash (/experiments/crypto-hash)



Compute SHA-256, SHA-384, or SHA-512 digests for any text input using the Web Crypto API in Workers.

## API Endpoint [#api-endpoint]

### GET /hash [#get-hash]

Returns a hex-encoded hash for the provided text.

**`text`** `string` (required)

Input string to hash (max 10,000 characters)

**`algorithm`** `string` (optional)

Hash algorithm: `SHA-256` (default), `SHA-384`, or `SHA-512`

#### Example Request [#example-request]

```bash
curl "https://your-worker.workers.dev/hash?text=hello&algorithm=SHA-256"
```

#### Success Response [#success-response]

**`algorithm`** `string`

The algorithm used for the digest

**`hash`** `string`

Lowercase hex-encoded digest

**`inputLength`** `number`

Length of the input text in characters

```json
{
  "algorithm": "SHA-256",
  "hash": "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824",
  "inputLength": 5
}
```

#### Error Response [#error-response]

```json
{
  "error": "Missing or invalid query parameter: text",
  "code": "INVALID_TEXT"
}
```

#### Error Codes [#error-codes]

* `400` - Missing or invalid `text` (`INVALID_TEXT`)
* `400` - Unsupported `algorithm` (`INVALID_ALGORITHM`)

## Use Cases [#use-cases]

* Hash strings at the edge without shipping crypto libraries
* Verify content fingerprints in API workflows
* Learn Web Crypto (`crypto.subtle.digest`) in Workers
* Compare digest sizes across SHA-256, SHA-384, and SHA-512

## Deployment [#deployment]

<Steps>
  <Step>
    ### Click the deploy button [#click-the-deploy-button]

    [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/shrinathsnayak/cloudflare-experiments/tree/main/apps/experiments/crypto-hash)
  </Step>

  <Step>
    ### Deploy [#deploy]

    No additional configuration required.
  </Step>

  <Step>
    ### Test your deployment [#test-your-deployment]

    ```bash
    curl "https://your-worker.workers.dev/hash?text=hello"
    ```
  </Step>
</Steps>

## Local Development [#local-development]

```bash
cd apps/experiments/crypto-hash
npm install
npm run dev
```

Test locally:

```bash
curl "http://localhost:8787/hash?text=hello"
```

## Cloudflare Features Used [#cloudflare-features-used]

* **[Workers](https://developers.cloudflare.com/workers/)** - Edge compute runtime
* **[Web Crypto API](https://developers.cloudflare.com/workers/runtime-apis/web-crypto/)** - `crypto.subtle.digest` for SHA hashing
