Signing Helpers
Signing helpers provide functions to cryptographically sign and verify data using HMAC-SHA256.
Basic Usage
import { function getSignedValue(signedValue: string | undefined | null): string | undefinedExtracts the value part from a signed string without verification.
This function simply extracts the original value from a signed string
without performing any signature verification. It's useful when you need
to access the value quickly without the overhead of cryptographic verification.getSignedValue, function sign(value: string, secret: string): Promise<string>Signs a string value using HMAC-SHA256 with a secret key.
This function creates a cryptographic signature that can be used to verify
the integrity and authenticity of the data. The signature is appended to
the original value, separated by a dot, using base64url encoding (no padding).sign, function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>Verifies and extracts the original value from a signed string.
This function validates the signature of a previously signed value using the same
secret key. If the signature is valid, it returns the original value. If the
signature is invalid or the format is incorrect, it returns undefined.unsign } from '@orpc/server/helpers'
const const secret: "your-secret-key"secret = 'your-secret-key'
const const userData: "user123"userData = 'user123'
const const signedValue: stringsignedValue = await function sign(value: string, secret: string): Promise<string>Signs a string value using HMAC-SHA256 with a secret key.
This function creates a cryptographic signature that can be used to verify
the integrity and authenticity of the data. The signature is appended to
the original value, separated by a dot, using base64url encoding (no padding).sign(const userData: "user123"userData, const secret: "your-secret-key"secret)
// 'user123.oneQsU0r5dvwQFHFEjjV1uOI_IR3gZfkYHij3TRauVA'
// ↑ Original data is visible to users
const const verifiedValue: string | undefinedverifiedValue = await function unsign(signedValue: string | undefined | null, secret: string): Promise<string | undefined>Verifies and extracts the original value from a signed string.
This function validates the signature of a previously signed value using the same
secret key. If the signature is valid, it returns the original value. If the
signature is invalid or the format is incorrect, it returns undefined.unsign(const signedValue: stringsignedValue, const secret: "your-secret-key"secret) // 'user123'
// Extract value without verification
const const extractedValue: string | undefinedextractedValue = function getSignedValue(signedValue: string | undefined | null): string | undefinedExtracts the value part from a signed string without verification.
This function simply extracts the original value from a signed string
without performing any signature verification. It's useful when you need
to access the value quickly without the overhead of cryptographic verification.getSignedValue(const signedValue: stringsignedValue) // 'user123'