简体   繁体   中英

How to get the modulus of a certificate from javascript?

I'm trying to get the "modulus" from public key and certificate to validate that these pair key and cert match before sign but I can't

I have a web app building in Angular and I use https://www.npmjs.com/package/node-forge.This is a snnipet but I don't know how to achive getting mudulus.

    var buffer = forge.util.createBuffer(FirmaUtil.Key, 'raw');
    var asn1 = forge.asn1.fromDer(buffer);
    var info = forge.pki.decryptPrivateKeyInfo(asn1, 'password');
    var privateK = forge.pki.privateKeyFromAsn1(info);

At the end i could achieve the goal compare the modulus from cer and key

  doesCertMatchKey: function privateKeyMatchesCertificate(model) {

            certificate = {};
            certificate.Cer = model.Cert; //AsArrayBuffer .cer file
            certificate.Key = model.Key;  //AsArrayBuffer .key file
            certificate.Pass = model.Pass

            let bufferCer = forge.util.createBuffer(certificate.Cer, 'raw');
            let asn1Cert = forge.asn1.fromDer(bufferCer);
            let cer = forge.pki.certificateFromAsn1(asn1Cert);
                        
            let nHexPublicK = cer.publicKey.n.toString(16);
            let eHexPublicK = cer.publicKey.e.toString(16);
            
            var bufferKey = forge.util.createBuffer(certificate.Key, 'raw');
            var asn1Key = forge.asn1.fromDer(bufferKey);
            //Before the next step check password to void exception.
            var info = forge.pki.decryptPrivateKeyInfo(asn1Key, certificate.Pass);
            var privateKey = forge.pki.privateKeyFromAsn1(info);

            var nHexPrivateK = privateKey.n.toString(16);
            var eHexPrivaetK = privateKey.e.toString(16);

            //If they are identical then the private key matches the certificate.
            return nHexPublicK === nHexPrivateK;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM