---
source_url: https://www.pubnub.com/docs/serverless/functions/functions-apis/crypto-module
title: Crypto Module
updated_at: 2026-06-18T11:29:22.216Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Crypto Module

The Functions' `crypto` module provides hashing, HMAC, signing, and verification helpers for PubNub Functions. Use it to protect data and validate messages in real time.

Import the module:

```javascript
const crypto = require('crypto');
```

## Available algorithms

List supported algorithms with `crypto.ALGORITHM`.

```javascript
console.log(crypto.ALGORITHM);
// -> ED25519, ECDSA_P256_SHA1, ECDSA_P256_SHA256, ECDSA_P256_SHA512, HMAC_SHA1, HMAC_SHA256, HMAC_SHA512
```

### HMAC

Generate an HMAC signature with `hmac()`. Output is Base64‑encoded.

Usage: `hmac(key, msg, algorithm)`

* `key`: signature key
* `msg`: message to sign
* `algorithm`: one of `crypto.ALGORITHM.HMAC_*`

```javascript
crypto.hmac(base64.btoa('sharedSecretKey'), 'secretPayload', crypto.ALGORITHM.HMAC_SHA1).then((result) => {
    console.log(result.replace('-', '+').replace('_', '/'));
}).catch((error) => {
    console.log(error)
});
```

### SHA‑1

Create a SHA‑1 hash with `sha1()`.

Usage: `sha1(msg)`

* `msg`: message to hash

```javascript
crypto.sha1('secretPayload').then((result2) => {
    console.log("secretPayload:" + result2);
}).catch((error) => {
    console.log(error)
});
```

### SHA‑256

Create a SHA‑256 hash with `sha256()`.

Usage: `sha256(msg)`

* `msg`: message to hash

```javascript
crypto.sha256('secretPayload').then((result2) => {
    console.log("secretPayload:" + result2);
}).catch((error) => {
    console.log(error)
});
```

### SHA‑512

Create a SHA‑512 hash with `sha512()`.

Usage: `sha512(msg)`

* `msg`: message to hash

```javascript
crypto.sha512('secretPayload').then((result2) => {
    console.log("secretPayload:" + result2);
}).catch((error) => {
    console.log(error)
});
```

### Private and public key examples

```javascript
const secretKey_ed25519 = {
    'kty'   : 'EdDSA',
    'crv'   : 'Ed25519',
    'sk'    : 'bfk0DBOMwYi1_kRk66o_f8IGotVcNDRwfnTJ_ATiDrs',
    'use'   : 'sig',
};
const publicKey_ed25519 = {
    'kty'   : 'EdDSA',
    'crv'   : 'Ed25519',
    'pk'    : 'wNrBAsRTMYbiXcQxKEcjU-qr24eLFSrrjgAfktkCM6c',
    'use'   : 'sig',
};
```

## Sign

Sign a message with a key and algorithm using `sign()`.

Usage: `sign(key, msg, algorithm)`

* `key`: private key for signing
* `msg`: message to sign
* `algorithm`: one of `crypto.ALGORITHM.*`

```javascript
crypto.sign(privateKey, 'secretPayload', crypto.ALGORITHM.ECDSA_P256_SHA1).then((result4) => {
    console.log(result4);
}).catch((error) => {
    console.log(error)
});
```

## Verify

Verify a signature with `verify()`.

Usage: `verify(sig, key, msg, algorithm)`

* `sig`: existing signature
* `key`: public key for verification
* `msg`: original message
* `algorithm`: one of `crypto.ALGORITHM.*`

```javascript
crypto.verify(<existing_signature>, publicKey, 'secretPayload', crypto.ALGORITHM.ECDSA_P256_SHA1).then((results) => {
    console.log(results)
}).catch((error) => {
    console.log(error)
});
```

:::note Functions support
If you need help with a scenario not covered here, contact [PubNub Support](https://www.pubnub.com/docs/mailto:support@pubnub.com).
:::