Javascript 将 ECDH 密钥转换为 ECDSA 密钥
Javascript convert ECDH keys into ECDSA keys
我是这样生成ECDH密钥的
let _this = this;
window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256", // the curve name
},
true, // <== Here if you want it to be exportable !!
["deriveKey", "deriveBits"] // usage
)
.then(key => {
_this.keys = key;
// export
return window.crypto.subtle.exportKey(
"raw", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
_this.keys.publicKey
)
.then(rawPublicKey => {
_this.publicKey = rawPublicKey;
return rawPublicKey;
})
})
通过这种方式,我得到了密钥和原始(x,y 坐标)public 密钥。
我会使用密钥将其用于 ECDSA
我该怎么做?
window.crypto.subtle.generateKey({
name: "ECDSA",
namedCurve: curve, //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key) {
publicKey = key.publicKey;
privateKey = key.privateKey;
// For Demo Purpos Only Exported in JWK format
window.crypto.subtle.exportKey("jwk", key.publicKey).then(
function(keydata) {
publicKeyhold = keydata;
publicKeyJson = JSON.stringify(publicKeyhold);
document.getElementById("ecdsapublic").value = publicKeyJson;
}
);
window.crypto.subtle.exportKey("jwk", key.privateKey).then(
function(keydata) {
privateKeyhold = keydata;
privateKeyJson = JSON.stringify(privateKeyhold);
document.getElementById("ecdsaprivate").value = privateKeyJson;
如您所见,您可以使用全局方法生成 ECDSA 密钥
它们会有所不同,您不能使用 ECDH 密钥
}
);
我是这样生成ECDH密钥的
let _this = this;
window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-256", // the curve name
},
true, // <== Here if you want it to be exportable !!
["deriveKey", "deriveBits"] // usage
)
.then(key => {
_this.keys = key;
// export
return window.crypto.subtle.exportKey(
"raw", //can be "jwk" (public or private), "raw" (public only), "spki" (public only), or "pkcs8" (private only)
_this.keys.publicKey
)
.then(rawPublicKey => {
_this.publicKey = rawPublicKey;
return rawPublicKey;
})
})
通过这种方式,我得到了密钥和原始(x,y 坐标)public 密钥。
我会使用密钥将其用于 ECDSA
我该怎么做?
window.crypto.subtle.generateKey({
name: "ECDSA",
namedCurve: curve, //can be "P-256", "P-384", or "P-521"
},
true, //whether the key is extractable (i.e. can be used in exportKey)
["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function(key) {
publicKey = key.publicKey;
privateKey = key.privateKey;
// For Demo Purpos Only Exported in JWK format
window.crypto.subtle.exportKey("jwk", key.publicKey).then(
function(keydata) {
publicKeyhold = keydata;
publicKeyJson = JSON.stringify(publicKeyhold);
document.getElementById("ecdsapublic").value = publicKeyJson;
}
);
window.crypto.subtle.exportKey("jwk", key.privateKey).then(
function(keydata) {
privateKeyhold = keydata;
privateKeyJson = JSON.stringify(privateKeyhold);
document.getElementById("ecdsaprivate").value = privateKeyJson;
如您所见,您可以使用全局方法生成 ECDSA 密钥 它们会有所不同,您不能使用 ECDH 密钥 } );