"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var SSHKeyGeneratorService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SSHKeyGeneratorService = void 0;
const common_1 = require("@nestjs/common");
const ssh2_1 = require("ssh2");
const node_crypto_1 = require("node:crypto");
let SSHKeyGeneratorService = SSHKeyGeneratorService_1 = class SSHKeyGeneratorService {
    constructor() {
        this.logger = new common_1.Logger(SSHKeyGeneratorService_1.name);
    }
    async generateKeyPair(type = 'ed25519') {
        return new Promise((resolve, reject) => {
            const options = type === 'rsa' ? { bits: 4096 } : undefined;
            ssh2_1.utils.generateKeyPair(type, options, (err, keys) => {
                if (err) {
                    reject(new Error(`Failed to generate SSH key pair: ${err.message}`));
                    return;
                }
                const fingerprint = this.generateFingerprint(keys.public);
                this.logger.debug(`Generated SSH key | fingerprint: ${fingerprint}`);
                resolve({
                    publicKey: keys.public,
                    privateKey: keys.private,
                    fingerprint: fingerprint,
                });
            });
        });
    }
    generateFingerprint(publicKey) {
        const parts = publicKey.split(' ');
        if (parts.length < 2) {
            throw new Error('Invalid public key format');
        }
        const keyData = parts[1];
        const hash = (0, node_crypto_1.createHash)('sha256')
            .update(Buffer.from(keyData, 'base64'))
            .digest('base64');
        return `SHA256:${hash}`;
    }
};
exports.SSHKeyGeneratorService = SSHKeyGeneratorService;
exports.SSHKeyGeneratorService = SSHKeyGeneratorService = SSHKeyGeneratorService_1 = __decorate([
    (0, common_1.Injectable)()
], SSHKeyGeneratorService);
