Jwk unstable
JSON Web Keys and the algorithm-bound keys the other effect/jose modules
use.
A key is imported for one algorithm and keeps it: a JWS or JWE operation
takes its algorithm from the key, so a token cannot pick another one. The
imported key wraps an effect/crypto key object, which redacts itself when
printed and caches its native key.
Importing honours the JWK's alg, use, and key_ops members, and
rejects RSA keys shorter than 2048 bits.
Constructors
generateKeyPair
unstableGenerates a key pair for an asymmetric algorithm and returns it as JWKs.
See
- importPrivate for preparing the private JWK for signing
- importPublic for preparing the public JWK for verification
Signature
declare const generateKeyPair: (...args: [options: GenerateKeyPairOptions]) => Effect<KeyPair, JoseError, Signature>generateSecret
unstableGenerates a redacted secret JWK carrying its alg, use, and optional kid.
Details
HMAC keys contain 32, 48, or 64 random bytes for HS256, HS384, or HS512.
Direct A256GCM keys contain 32 bytes and use enc instead of sig.
See
- importSecret for preparing the JWK for use
- generateKeyPair for asymmetric keys
Signature
declare function generateSecret(algorithm: SecretAlgorithm, options?: { readonly kid?: string;}): Effect<Redacted<{ readonly alg?: string; readonly k: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "oct"; readonly use?: string;}>, never, SecureRandom>importPkcs8
unstableImports a redacted PKCS#8 private key from DER bytes or PEM text for an asymmetric algorithm.
See
- importPrivate for importing JWK-encoded keys
- exportPkcs8
Signature
declare const importPkcs8: (...args: [input: Redacted<string | Uint8Array<ArrayBufferLike>>, algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA", options?: { readonly kid?: string;}]) => Effect<PrivateKey, JoseError, never>importPrivate
unstableImports a private JWK for an asymmetric algorithm.
See
- importPkcs8 for importing DER or PEM keys
- exportPrivate
Signature
declare const importPrivate: (...args: [input: Redacted<{ readonly alg?: string; readonly d: string; readonly dp: string; readonly dq: string; readonly e: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p: string; readonly q: string; readonly qi: string; readonly use?: string;} | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly use?: string; readonly x: string; readonly y: string;} | { readonly alg?: string; readonly crv: "Ed25519"; readonly d: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly use?: string; readonly x: string;}>, algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA"]) => Effect<PrivateKey, JoseError, never>importPublic
unstableImports a public JWK for an asymmetric algorithm.
Details
The key's family and curve must fit the algorithm, and any alg, use, or
key_ops member must allow verification with it. Private members are
rejected. To import untrusted JSON, decode it with PublicJwk first.
See
Signature
declare const importPublic: (...args: [jwk: { readonly alg?: string; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly e: string; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string;} | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string; readonly y: string;} | { readonly alg?: string; readonly crv: "Ed25519"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string;}, algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA"]) => Effect<PublicKey, JoseError, never>importSecret
unstableImports an octet JWK for HMAC signatures or for direct A256GCM encryption.
Details
HS256, HS384, and HS512 keys need at least 32, 48, and 64 bytes respectively;
dir keys need exactly 32. Any alg,
use, or key_ops member must allow the algorithm; key_ops must allow
both of its operations.
See
Signature
declare const importSecret: <A extends SecretAlgorithm>(...args: [input: Redacted<{ readonly alg?: string; readonly k: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "oct"; readonly use?: string;}>, algorithm: A]) => Effect<SecretKey<A>, JoseError, never>Example Importing an HS256 key
import { Effect, Redacted } from "effect"import { Jwk } from "effect/jose"
// Fixed public example material; generate application secrets securely.const secret = Redacted.make({ kty: "oct", k: "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc" } as const)const key = await Effect.runPromise(Jwk.importSecret(secret, "HS256"))key.algorithm // => "HS256"importSpki
unstableImports an SPKI public key from DER bytes or PEM text for an asymmetric algorithm.
See
- importPublic for importing JWK-encoded keys
- exportSpki
Signature
declare const importSpki: (...args: [input: string | Uint8Array<ArrayBufferLike>, algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA", options?: { readonly kid?: string;}]) => Effect<PublicKey, JoseError, never>Cryptography
thumbprint
unstableComputes an RFC 7638 JWK thumbprint, encoded as unpadded base64url.
Details
Uses SHA-256 by default; SHA-384 and SHA-512 are also supported. Only required key members are hashed, so metadata does not affect the result. Asymmetric private keys have the same thumbprint as their public keys. A secret key's thumbprint is a hash of the secret, so treat it as sensitive.
See
Signature
declare const thumbprint: (...args: [input: ThumbprintInput, hash: Algorithm]) => Effect<string, JoseError, Digest>thumbprintUri
unstableComputes an RFC 9278 JWK thumbprint URI using SHA-256 by default.
See
- thumbprint for supported inputs and hashes
Signature
declare function thumbprintUri(input: ThumbprintInput, hash: Algorithm): Effect<string, JoseError, Digest>Encoding
exportPkcs8
unstableExports a private key as redacted PKCS#8 DER bytes, or redacted PEM text with format: "pem".
See
- importPkcs8
- exportPrivate for JWK output
Signature
declare function exportPkcs8(key: PrivateKey, options: { readonly format: "pem";}): Effect<Redacted<string>, JoseError>declare function exportPkcs8(key: PrivateKey, options?: { readonly format?: "der";}): Effect<Redacted<Uint8Array<ArrayBufferLike>>, JoseError>declare function exportPkcs8(key: PrivateKey, options?: { readonly format?: "pem" | "der";}): Effect<Redacted<string | Uint8Array<ArrayBufferLike>>, JoseError>exportPrivate
unstableExports a private key as a JWK carrying its alg, kid, and use: "sig".
See
Signature
declare function exportPrivate(key: PrivateKey): Effect<Redacted<{ readonly alg?: string; readonly d: string; readonly dp: string; readonly dq: string; readonly e: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p: string; readonly q: string; readonly qi: string; readonly use?: string;} | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly use?: string; readonly x: string; readonly y: string;} | { readonly alg?: string; readonly crv: "Ed25519"; readonly d: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly use?: string; readonly x: string;}>, JoseError>exportPublic
unstableExports a public key as a JWK carrying its alg, kid, and use: "sig".
Signature
declare function exportPublic(key: PublicKey): Effect<{ readonly alg?: string; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly e: string; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string;} | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string; readonly y: string;} | { readonly alg?: string; readonly crv: "Ed25519"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string;}, JoseError>exportSpki
unstableExports a public key as SPKI DER bytes, or as PEM text with format: "pem".
See
- importSpki
- exportPublic for JWK output
Signature
declare function exportSpki(key: PublicKey, options: { readonly format: "pem";}): Effect<string, JoseError>declare function exportSpki(key: PublicKey, options?: { readonly format?: "der";}): Effect<Uint8Array<ArrayBufferLike>, JoseError>declare function exportSpki(key: PublicKey, options?: { readonly format?: "pem" | "der";}): Effect<string | Uint8Array<ArrayBufferLike>, JoseError>Models
A JWS algorithm.
Signature
type Algorithm = typeof Algorithm.TypeAn asymmetric signature algorithm.
Signature
type AsymmetricAlgorithm = typeof AsymmetricAlgorithm.TypeOptions for generateKeyPair.
Signature
interface GenerateKeyPairOptions { readonly algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA"; readonly kid?: string; readonly modulusLength?: number;}An HMAC signature algorithm using SHA-256, SHA-384, or SHA-512.
Signature
type HmacAlgorithm = "HS256" | "HS384" | "HS512"A generated public JWK and its private JWK, both carrying alg, use: "sig",
and the requested kid.
Signature
interface KeyPair { readonly privateKey: Redacted<{ readonly alg?: string; readonly d: string; readonly dp: string; readonly dq: string; readonly e: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p: string; readonly q: string; readonly qi: string; readonly use?: string; } | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly use?: string; readonly x: string; readonly y: string; } | { readonly alg?: string; readonly crv: "Ed25519"; readonly d: string; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly use?: string; readonly x: string; }>; readonly publicKey: { readonly alg?: string; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly e: string; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; } | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string; readonly y: string; } | { readonly alg?: string; readonly crv: "Ed25519"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string; };}An RSA, P-256, P-384, P-521, or Ed25519 private JWK.
Signature
type PrivateJwk = typeof PrivateJwk.TypeA private key bound to one asymmetric algorithm.
Signature
interface PrivateKey { readonly _tag: "PrivateKey"; readonly algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA"; readonly key: PrivateKey; readonly kid: string | undefined;}An RSA, P-256, P-384, P-521, or Ed25519 public JWK.
Signature
type PublicJwk = typeof PublicJwk.TypeA public key bound to one asymmetric algorithm, with the JWK it was imported from.
Signature
interface PublicKey { readonly _tag: "PublicKey"; readonly algorithm: "RS256" | "RS384" | "RS512" | "PS256" | "PS384" | "PS512" | "ES256" | "ES384" | "ES512" | "EdDSA"; readonly jwk: { readonly alg?: string; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly e: string; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "RSA"; readonly n: string; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; } | { readonly alg?: string; readonly crv: "P-256" | "P-384" | "P-521"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "EC"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string; readonly y: string; } | { readonly alg?: string; readonly crv: "Ed25519"; readonly d?: undefined; readonly dp?: undefined; readonly dq?: undefined; readonly k?: undefined; readonly key_ops?: readonly Array<string>; readonly kid?: string; readonly kty: "OKP"; readonly oth?: undefined; readonly p?: undefined; readonly q?: undefined; readonly qi?: undefined; readonly use?: string; readonly x: string; }; readonly key: PublicKey; readonly kid: string | undefined;}The algorithms of a symmetric key: HMAC signatures, or direct A256GCM encryption.
Signature
type SecretAlgorithm = HmacAlgorithm | "dir"An octet-sequence JWK.
Signature
type SecretJwk = typeof SecretJwk.TypeA symmetric key bound to an HMAC signature algorithm or to direct A256GCM encryption.
Signature
interface SecretKey<A extends SecretAlgorithm = SecretAlgorithm> { readonly _tag: "SecretKey"; readonly algorithm: A; readonly key: A extends HmacAlgorithm ? Key<Algorithm> : Key<Algorithm>; readonly kid: string | undefined;}A key that signs JWS and JWT tokens.
Signature
type SigningKey = PrivateKey | SecretKey<HmacAlgorithm>A JWK or imported key whose thumbprint can be computed. Private and secret JWKs must be redacted.
Signature
type ThumbprintInput = PublicJwk | Redacted.Redacted<PrivateJwk | SecretJwk> | PublicKey | PrivateKey | SecretKeyA key that verifies JWS and JWT tokens.
Signature
type VerificationKey = PublicKey | SecretKey<HmacAlgorithm>Schemas
Schema for the JWS algorithms: HS256, HS384, HS512, and the asymmetric algorithms.
Signature
declare const Algorithm: Literals<readonly ["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384", "ES512", "EdDSA"]>AsymmetricAlgorithm
unstableSchema for RSA, RSA-PSS, and ECDSA signatures with SHA-256, SHA-384, or SHA-512, and EdDSA signatures with Ed25519.
Signature
declare const AsymmetricAlgorithm: Literals<readonly ["RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384", "ES512", "EdDSA"]>PrivateJwk
unstableSchema for an RSA, P-256, P-384, P-521, or Ed25519 private JWK. RSA keys need their CRT members and cannot have more than two primes.
Signature
declare const PrivateJwk: Union<readonly [Struct<{ readonly alg: optionalKey<String>; readonly d: String; readonly dp: String; readonly dq: String; readonly e: String; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"RSA">; readonly n: String; readonly oth: optionalKey<Never>; readonly p: String; readonly q: String; readonly qi: String; readonly use: optionalKey<String>;}>, Struct<{ readonly alg: optionalKey<String>; readonly crv: Literals<readonly ["P-256", "P-384", "P-521"]>; readonly d: String; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"EC">; readonly use: optionalKey<String>; readonly x: String; readonly y: String;}>, Struct<{ readonly alg: optionalKey<String>; readonly crv: Literal<"Ed25519">; readonly d: String; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"OKP">; readonly use: optionalKey<String>; readonly x: String;}>]>Schema for an RSA, P-256, P-384, P-521, or Ed25519 public JWK. Private and symmetric members, including RSA factors, are rejected.
Signature
declare const PublicJwk: Union<readonly [Struct<{ readonly alg: optionalKey<String>; readonly d: optionalKey<Never>; readonly dp: optionalKey<Never>; readonly dq: optionalKey<Never>; readonly e: String; readonly k: optionalKey<Never>; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"RSA">; readonly n: String; readonly oth: optionalKey<Never>; readonly p: optionalKey<Never>; readonly q: optionalKey<Never>; readonly qi: optionalKey<Never>; readonly use: optionalKey<String>;}>, Struct<{ readonly alg: optionalKey<String>; readonly crv: Literals<readonly ["P-256", "P-384", "P-521"]>; readonly d: optionalKey<Never>; readonly dp: optionalKey<Never>; readonly dq: optionalKey<Never>; readonly k: optionalKey<Never>; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"EC">; readonly oth: optionalKey<Never>; readonly p: optionalKey<Never>; readonly q: optionalKey<Never>; readonly qi: optionalKey<Never>; readonly use: optionalKey<String>; readonly x: String; readonly y: String;}>, Struct<{ readonly alg: optionalKey<String>; readonly crv: Literal<"Ed25519">; readonly d: optionalKey<Never>; readonly dp: optionalKey<Never>; readonly dq: optionalKey<Never>; readonly k: optionalKey<Never>; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"OKP">; readonly oth: optionalKey<Never>; readonly p: optionalKey<Never>; readonly q: optionalKey<Never>; readonly qi: optionalKey<Never>; readonly use: optionalKey<String>; readonly x: String;}>]>Schema for an octet-sequence JWK.
Signature
declare const SecretJwk: Struct<{ readonly alg: optionalKey<String>; readonly k: String; readonly key_ops: optionalKey<$Array<String>>; readonly kid: optionalKey<String>; readonly kty: Literal<"oct">; readonly use: optionalKey<String>;}>