function importPrivateKey
importPrivateKey(
privateKey: string,
): Promise<CryptoKey>

Imports an x25519 private key as a CryptoKey by wrapping it in PKCS8 format.

This function wraps raw 32-byte x25519 private keys in the PKCS8 container format required by the Web Crypto API. The PKCS8 wrapper includes the algorithm OID and proper ASN.1 structure.

Examples

Import private key from base64 string

import { assertEquals } from "@std/assert";
import { importPrivateKey, wgGenKey } from "@hertzg/wg-keys";

const privateKeyBase64 = wgGenKey();
const cryptoKey = await importPrivateKey(privateKeyBase64);

assertEquals(cryptoKey.type, "private");

Parameters

privateKey: string

The private key as base64 string

optional
options: ImportPrivateKeyOptions

Configuration options for key import

Return Type

Promise<CryptoKey>

A promise resolving to the imported CryptoKey

importPrivateKey(
privateKey: Uint8Array,
): Promise<CryptoKey>

Imports an x25519 private key as a CryptoKey by wrapping it in PKCS8 format.

This function wraps raw 32-byte x25519 private keys in the PKCS8 container format required by the Web Crypto API. The PKCS8 wrapper includes the algorithm OID and proper ASN.1 structure.

Examples

Import private key from Uint8Array

import { assertEquals } from "@std/assert";
import { importPrivateKey, randomPrivateKeyBytes } from "@hertzg/wg-keys";

const privateKeyBytes = randomPrivateKeyBytes();
const cryptoKey = await importPrivateKey(privateKeyBytes);

assertEquals(cryptoKey.type, "private");

Import private key with custom key usages

import { assertEquals } from "@std/assert";
import { importPrivateKey, randomPrivateKeyBytes } from "@hertzg/wg-keys";

const privateKeyBytes = randomPrivateKeyBytes();
const cryptoKey = await importPrivateKey(privateKeyBytes, {
  keyUsages: ["deriveKey", "deriveBits"],
});

assertEquals(cryptoKey.type, "private");
assertEquals(cryptoKey.usages.length, 2);

Parameters

privateKey: Uint8Array

The 32-byte private key as Uint8Array

optional
options: ImportPrivateKeyOptions

Configuration options for key import

Return Type

Promise<CryptoKey>

A promise resolving to the imported CryptoKey