Skip to content

Commit

Permalink
exportPEMKey options
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinta365 committed Apr 29, 2024
1 parent ed82711 commit a19234c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ const { privateKey, publicKey } = await generateKeyPair("RS512");
const key = await generateKeyPair({ algorithm: "HS512" });
```

- **`exportPEMKey(key: CryptoKey, filePath?: string): Promise<string>`** (Experimental)
- **`exportPEMKey(key: CryptoKey, filePathOrOptions?: string | ExportPEMKeyOptions): Promise<string>`** (Experimental)
- **`importPEMKey(keyDataOrPath: string, algorithm: SupportedKeyPairAlgorithms): Promise<CryptoKey>`** (Experimental)

```javascript
// Experimental.

// Generate and export RS512 keys in PEM-format.
// Generate and export RS512 keys in PEM-format. (filePath and write mode can be supplied as optional second parameter at export)
const { privateKey, publicKey } = await generateKeyPair("RS512");
await exportPEMKey(privateKey, "./private_key_RS512.pem");
await exportPEMKey(publicKey, "./public_key_RS512.pem");
Expand Down
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/jwt",
"version": "0.4.6",
"version": "0.4.7",
"exports": "./mod.ts",

"tasks": {
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { signJWT } from "./src/sign.ts";
export { unsafeParseJWT, validateJWT } from "./src/validate.ts";
export { exportPEMKey, generateKey, generateKeyPair, importPEMKey } from "./src/cryptokeys.ts";
export type {
ExportPEMKeyOptions,
GenerateKeyOptions,
GenerateKeyPairOptions,
SupportedKeyAlgorithms,
Expand Down
33 changes: 30 additions & 3 deletions src/cryptokeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,41 @@ export async function generateKeyPair(
}
}

// Options for PEM formatted file write.
export interface ExportPEMKeyOptions {
// Optional path to write the PEM-formatted key to
filePath?: string;
// Optional for file write mode
mode?: number;
}

// Overload 1: Export without writing to a file
export function exportPEMKey(key: CryptoKey): Promise<string>;

// Overload 2: Export with a file path (default mode)
export function exportPEMKey(key: CryptoKey, filePath: string): Promise<string>;

// Overload 3: Export with custom options
export function exportPEMKey(key: CryptoKey, options: ExportPEMKeyOptions): Promise<string>;

/**
* Exports a CryptoKey to PEM format and optionally writes it to a file.
*
* @param key - The CryptoKey to export.
* @param filePath - (Optional) Path to write the PEM-formatted key to.
* @param filePathOrOptions - (Optional) Path to write the PEM-formatted key to, or an ExportPEMKeyOptions object.
* @returns {Promise<string>} A Promise resolving to the PEM-formatted key string.
*/
export async function exportPEMKey(key: CryptoKey, filePath?: string): Promise<string> {
export async function exportPEMKey(key: CryptoKey, filePathOrOptions?: string | ExportPEMKeyOptions): Promise<string> {
let filePath: string | undefined;
let options: ExportPEMKeyOptions | undefined;

if (typeof filePathOrOptions === "string") {
filePath = filePathOrOptions;
} else {
options = filePathOrOptions;
filePath = options?.filePath;
}

const keyType = key.type;

const exportedKey = await window.crypto.subtle.exportKey(
Expand All @@ -186,7 +213,7 @@ export async function exportPEMKey(key: CryptoKey, filePath?: string): Promise<s
const pemKey = formatAsPem(keyType === "private" ? "PRIVATE KEY" : "PUBLIC KEY", exportedKey);

if (filePath) {
await writeFile(filePath, pemKey);
await writeFile(filePath, pemKey, { mode: options?.mode ?? 0o600 });
}

return pemKey;
Expand Down

0 comments on commit a19234c

Please sign in to comment.