无法获取未定义或空引用的属性“消息”

时间:2018-05-07 21:58:20

标签: typescript internet-explorer-11 webcryptoapi

我正在尝试使用适用于IE 11和Chrome的webcrypto API。使用IE实现时,我收到错误Unable to get property 'message' of undefined or null reference

我正在使用此处显示的实施https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/dev-guides/dn265046(v=vs.85)

export function verifySocketMessage(signature: string, convertedData: any, publicKey: string) {
    publicKey = publicKey.replace(/(\r\n|\n|\r)/gm, "").split("-----")[2];

    let crypto = window.crypto || (<any>window).msCrypto;

    // IE 11 implementation
    let importOp = crypto.subtle.importKey("spki",
        base64ToArrayBufferEx(publicKey),
        {
            name: "RSASSA-PKCS1-v1_5",
            hash: { name: "SHA-256" }
        },
        true, ["verify"]);

    return Promise.resolve(importOp).then((key) => {
        let keyRes;
        key.oncomplete = (e) => {
            keyRes = e.target.result;
            console.log("onComplete", keyRes);

            let verifyOp = crypto.subtle.verify("RSASSA-PKCS1-v1_5",
                keyRes, base64ToArrayBufferEx("signature"), strToAB("convertedData"));

            console.log("verifyOp", verifyOp); // This contains the error

            return Promise.resolve(verifyOp).then(verified => {
                console.log("in verified", verified);
                verified.onerror = (err) => {
                    console.log("in verified err", err);
                };

                verified.oncomplete = (e) => {
                    console.log("verified complete", e);
                };
            });
        };
    });


    // Chrome implementation
    // return crypto.subtle.importKey("spki",
    //     base64ToArrayBufferEx(publicKey),
    //     {
    //         name: "RSASSA-PKCS1-v1_5",
    //         hash: { name: "SHA-256" }
    //     },
    //     true, ["verify"]).then(key => {
    //         console.log("key", key)
    //         return crypto.subtle.verify({
    //             name: "RSASSA-PKCS1-v1_5"
    //         } as any, key, base64ToArrayBufferEx(signature),
    //             strToAB(convertedData)).then(verified => {
    //                 console.log("verified", verified)
    //                 return Promise.resolve(verified);
    //             }, (err) => {
    //                 console.log("up err", err);
    //             });
    //     });

}

参数工作得很完美,并在Chrome中验证消息但在IE 11中失败。当我尝试执行确切的实现,包括IE11文档中显示的键的生成和签名时,它工作正常。

生成IE11密钥和生成chrome密钥之间有什么不同吗?我使用的数据和密钥是在Chrome浏览器上生成的。

0 个答案:

没有答案