javascript - Cannot extract public key from ASN Object - Stack Overflow

admin2025-04-25  3

So I am trying to parse a der format file to extract the public key. I have three algos, RSA, EDDSA and ECDSA. The below code works for RSA but not for EDDSA and ECDSA. I am getting the below error

Error: Cannot read public key. Unknown OID. at push../node_modules/node-forge/lib/rsa.js.pki.publicKeyFromAsn1

Below is the function which I wrote which parses the der to extract the public key. I think this is generic but not sure why it is not working for ecdsa and eddsa.

  const uploadDer = (file: any): boolean => {
    const fileReader = new FileReader();

    fileReader.onload = () => {
      const arrayBuffer = fileReader.result as ArrayBuffer;

      try {
        const uint8Array = new Uint8Array(arrayBuffer);
        const byteBuffer = forge.util.createBuffer(uint8Array);
        const asnObject = forge.asn1.fromDer(byteBuffer);
        const publicKeyObject = forge.pki.publicKeyFromAsn1(asnObject);
        const fileContent = forge.pki.publicKeyToPem(publicKeyObject);
        console.log('fileContent--> ', fileContent);

        const pemSections = forge.pem.decode(fileContent);
        const lastPemSection = pemSections[pemSections.length - 1];
        const publicKey = forge.util.encode64(lastPemSection.body);
        props.change('publicKey', publicKey);
      } catch (error) {
        props.change('publicKey', '');

        return dispatch(
          showNotification({
            id: uuidv1(),
            title: 'statusTitle',
            message: 'uploadKeypairFailedMessage',
            type: NotificationTypes.error,
          })
        );
      }
    };

    fileReader.onerror = (error) => {
      console.error('onerror', error);
    };

    fileReader.readAsArrayBuffer(file);

    return false;
  };
转载请注明原文地址:http://anycun.com/QandA/1745596797a90963.html