canvas-lti
Version:
A Canvas LTI 1.3 integration tool.
38 lines (31 loc) • 1.39 kB
JavaScript
const crypto = require('crypto');
const fs = require('fs');
// Function to load keys from files
function loadKey(file) {
return fs.readFileSync(file, 'utf8');
}
// Function to extract public key from private key
function extractPublicKeyFromPrivateKey(privateKey) {
try {
const privateKeyObject = crypto.createPrivateKey(privateKey);
const publicKeyObject = crypto.createPublicKey(privateKeyObject);
return publicKeyObject.export({ type: 'spki', format: 'pem' });
} catch (error) {
console.error('Failed to extract public key from private key:', error.message);
return null; // Or handle the error as appropriate for your application
}
}
// Function to normalize keys for comparison
const normalizeKey = (key) => key.replace(/\r\n/g, '\n').trim();
// Main function to compare keys
function validateKeys(privateKeyName, publicKeyName) {
// Load the private and public keys
const privateKey = loadKey(privateKeyName);
const publicKey = loadKey(publicKeyName);
// Extract the public key from the private key
const extractedPublicKey = extractPublicKeyFromPrivateKey(privateKey);
// Compare the extracted public key with the provided public key
return normalizeKey(extractedPublicKey) === normalizeKey(publicKey);
}
// Export the compareKeys function
module.exports = { validateKeys };