我正在创建一个密钥对,然后使用 Web Crypto API 从密钥对导出一个密钥:
var log = console.log.bind(console);
var subtleCrypto = null;
if ( window.crypto ) {
subtleCrypto = window.crypto.subtle || window.crypto.webkitSubtle;
}
if ( window.msCrypto ) {
subtleCrypto = window.msCrypto.subtle
}
subtleCrypto.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]), // 24 bit representation of 65537
hash: {name: "SHA-256"}
},
true, // can extract it later if we want
["sign", "verify"]
).then(function(keyPair){
log('Exporting from keyPair', keyPair)
subtleCrypto.exportKey('pkcs8', keyPair.privateKey).then(function(pkcs8) {
log('Exported keypair!', pkcs8)
}, function(reason) {
log('Couldnt export keypair', reason)
})
}, function(reason){
log('could not generate key', reason)
})
在 Chrome 和 Firefox 上,代码运行良好,打印:
"Exporting from keyPair" Object { privateKey: CryptoKey, publicKey: CryptoKey }
"Exported keypair!" ArrayBuffer { byteLength: 1218 }
但是在 Safari 上它失败了,只打印:
Exporting from keyPair KeyPair
然后什么都不做。如何在 Safari 上导出密钥?