@aicone/pem is a JavaScript library for handling PEM-encoded strings, providing methods to validate and extract various PEM types, such as certificates, public keys, and private keys. This library supports different PEM types, including RSA, DSA, and EC formats, making it ideal for working with secure data in cryptographic and certificate-based applications.
To add @aicone/pem to your Deno project, use:
deno add jsr:@aicone/pem- Supports Multiple PEM Types: Handles
RSA PRIVATE KEY,CERTIFICATE,RSA PUBLIC KEY,DSA PRIVATE KEY, and more. - Error Handling: Throws a
TypeErrorif the PEM string does not match the expected format. - Regex-based Validation: Validates PEM strings with a regular expression.
import { pem, Pem } from 'jsr:@aicone/pem';Use the certificate() method to extract a certificate PEM string. If the string format is invalid, a TypeError is thrown.
const pemString = '-----BEGIN CERTIFICATE-----\n...base64 data...\n-----END CERTIFICATE-----';
const pemObject = pem(pemString);
try {
const certificate = pemObject.certificate();
console.log(certificate);
} catch (error) {
console.error(error); // Expected PEM format CERTIFICATE
}Use the publicKey() method to retrieve a public key PEM string. Optionally, specify "RSA" for RSA public keys.
try {
const publicKey = pemObject.publicKey("RSA");
console.log(publicKey);
} catch (error) {
console.error(error); // Expected PEM format RSA PUBLIC KEY
}The privateKey() method retrieves a private key PEM string. Optionally, specify "RSA", "DSA", or "EC" for specific types of private keys.
try {
const privateKey = pemObject.privateKey("RSA");
console.log(privateKey);
} catch (error) {
console.error(error); // Expected PEM format RSA PRIVATE KEY
}Takes a PEM string as input and returns an instance of the Pem class.
- Parameters:
string(string): The PEM-encoded string.
- Returns:
Peminstance.
The Pem class provides methods to extract and validate PEM strings for various types.
Returns the certificate PEM string if it exists in the input; otherwise, throws a TypeError.
- Returns:
string | TypeError
Retrieves a public key PEM string based on the specified type, defaulting to any public key if no type is provided.
- Parameters:
type(string, optional): Specify"RSA"for RSA public keys.
- Returns:
string | TypeError
Retrieves a private key PEM string based on the specified type, defaulting to any private key if no type is provided.
- Parameters:
type(string, optional): Specify"RSA","DSA", or"EC"for specific private key types.
- Returns:
string | TypeError
const pemString = '-----BEGIN CERTIFICATE-----\n...base64 data...\n-----END CERTIFICATE-----';
const pemObject = pem(pemString);
try {
console.log(pemObject.certificate());
} catch (error) {
console.error(error.message);
}const pemObject = pem('-----BEGIN RSA PUBLIC KEY-----\n...base64 data...\n-----END RSA PUBLIC KEY-----');
try {
console.log(pemObject.publicKey("RSA"));
} catch (error) {
console.error(error.message);
}const pemObject = pem('-----BEGIN EC PRIVATE KEY-----\n...base64 data...\n-----END EC PRIVATE KEY-----');
try {
console.log(pemObject.privateKey("EC"));
} catch (error) {
console.error(error.message);
}Contributions to improve the library are welcome. Please open an issue or pull request on the GitHub repository.
This project is licensed under the MIT License.