@synet/keys
Version:
Zero-dependency, secure key generation library. Supports ed25519, x25519, secp256k1, RSA, and WireGuard keys.
98 lines (97 loc) • 3.32 kB
TypeScript
/**
* Supported cryptographic key types
*/
export type KeyType = "rsa" | "ed25519" | "x25519" | "secp256k1" | "wireguard";
/**
* Key pair interface for all cryptographic key types
*/
export interface KeyPair {
privateKey: string;
publicKey: string;
type: KeyType;
}
/**
* Key encoding formats
*/
export type KeyFormat = "pem" | "hex" | "base64";
/**
* Generate a cryptographic key pair
* @param type The type of key to generate
* @param options Optional configuration for key generation
* @returns A key pair object containing the private and public keys
* @throws Error if the key type is unsupported
*/
export declare function generateKeyPair(type: KeyType, options?: {
format?: KeyFormat;
}): KeyPair;
/**
* Extract the public key from a private key
* @param privateKey The private key in PEM or hex format
* @returns The corresponding public key in the same format, or null if extraction fails
*/
export declare function derivePublicKey(privateKey: string): string | null;
/**
* Compute a short identifier from a public key
* @param publicKey The public key in PEM format
* @returns A 16-character hexadecimal identifier
*/
export declare function getShortId(publicKey: string): string;
/**
* Compute a fingerprint from a public key
* @param publicKey The public key in PEM format
* @returns A 64-character hexadecimal fingerprint
*/
export declare function getFingerprint(publicKey: string): string;
/**
* Key format conversion utilities
*/
/**
* Convert PEM key to hex format
* @param pemKey PEM formatted key
* @returns Hex string or null if conversion fails
*/
export declare function pemToHex(pemKey: string): string | null;
/**
* Convert PEM private key to hex format
* @param pemKey The PEM-encoded private key
* @returns Hex string or null if conversion fails
*/
export declare function pemPrivateKeyToHex(pemKey: string): string | null;
/**
* Convert hex key to PEM format
* @param hexKey Hex string key
* @param keyType Key type for proper PEM formatting
* @returns PEM formatted key or null if conversion fails
*/
export declare function hexToPem(hexKey: string, keyType: KeyType): string | null;
/**
* Convert base64 key to hex format
* @param base64Key Base64 encoded key
* @returns Hex string or null if conversion fails
*/
export declare function base64ToHex(base64Key: string): string | null;
/**
* Convert hex key to base64 format
* @param hexKey Hex string key
* @returns Base64 encoded key or null if conversion fails
*/
export declare function hexToBase64(hexKey: string): string | null;
/**
* Detect key format
* @param key Key in any format
* @returns Detected format or null if unknown
*/
export declare function detectKeyFormat(key: string): KeyFormat | null;
/**
* Convert key to hex format regardless of input format
* @param key Key in any format
* @param keyType Key type (needed for PEM conversion)
* @returns Hex string or null if conversion fails
*/
export declare function toHex(key: string, keyType?: KeyType): string | null;
/**
* Convert Ed25519 private key from hex to PEM format
* @param hexKey The hex-encoded private key (64 bytes: 32 private + 32 public)
* @returns PEM formatted private key or null if conversion fails
*/
export declare function hexPrivateKeyToPem(hexKey: string): string | null;