class

Vapid::KeyPair

Inherits Reference < Object

Represents an ES256 (ECDSA using P-256 curve and SHA-256) key pair used for VAPID authentication.

Usage

# Generate a new key pair
key_pair = Vapid::KeyPair.generate

# Access keys in various formats
puts key_pair.public_key_bytes    # Raw bytes
puts key_pair.public_key_base64   # URL-safe Base64
puts key_pair.private_key_pem     # PEM format

# Load from existing private key
key_pair = Vapid::KeyPair.from_pem(pem_string)

Constructors

from_der(der : Bytes) : KeyPair

Loads a KeyPair from a DER-encoded private key

Source
from_pem(pem : String) : KeyPair

Loads a KeyPair from a PEM-encoded private key string

pem = File.read("private_key.pem")
key_pair = Vapid::KeyPair.from_pem(pem)
Source
generate

Generates a new ES256 key pair using the P-256 curve

key_pair = Vapid::KeyPair.generate
Source
new(ec_key : OpenSSL::PKey::EC)

Creates a KeyPair from an existing OpenSSL EC key

Source

Instance methods

ec_key
Source
private_key_der

Returns the private key in DER format

Source
private_key_pem

Returns the private key in PEM format

pem = key_pair.private_key_pem
File.write("private_key.pem", pem)
Source
public_key_base64

Returns the public key in URL-safe Base64 format (without padding) This format is used in the 'p256ecdsa' header for VAPID

public_key = key_pair.public_key_base64
# => "BKj3v..."
Source
public_key_bytes

Returns the raw public key bytes in uncompressed format (65 bytes) Format: 0x04 || X || Y where X and Y are 32-byte coordinates

Source
to_openssl

Returns the OpenSSL EC key for advanced usage

Source