Monday, May 20, 2024
73
rated 0 times [  77] [ 4]  / answers: 1 / hits: 6583  / 3 Years ago, thu, july 22, 2021, 12:00:00

I am using



window.crypto.subtle.importKey



on the localhost, It works fine. But when I put the code on the server, It is giving error Can not read property importKey of undefined. As I know, I am not using this in a secure https connection. Therefore it is showing the error. I checked this issue crypto.subtle for unsecure origins in Chrome
How to enable crypto.subtle for unsecure origins in Chrome?


Is there any alternative for this to fix the issue?


Here is the code


        var contents = e.target.result;//Data from the PKCS#12 file input
var pkcs12Der = arrayBufferToString(contents)
var pkcs12B64 = forge.util.encode64(pkcs12Der);
var pkcs12Der = forge.util.decode64(pkcs12B64);
var pkcs12Asn1 = forge.asn1.fromDer(pkcs12Der);

var pkcs12 = forge.pkcs12.pkcs12FromAsn1(pkcs12Asn1, false, password);
var privateKey
for (var sci = 0; sci < pkcs12.safeContents.length; ++sci) {
var safeContents = pkcs12.safeContents[sci];
for (var sbi = 0; sbi < safeContents.safeBags.length; ++sbi) {
var safeBag = safeContents.safeBags[sbi];
if (safeBag.type === forge.pki.oids.keyBag) {
privateKey = safeBag.key;
} else if (safeBag.type === forge.pki.oids.pkcs8ShroudedKeyBag) {
privateKey = safeBag.key;
} else if (safeBag.type === forge.pki.oids.certBag) { }
}
}
var privateKeyInfoDerBuff = _privateKeyToPkcs8(privateKey);

//Import the webcrypto key
window.crypto.subtle.importKey('pkcs8', privateKeyInfoDerBuff,
{ name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } }, true, ["sign"])
.then(function (cryptoKey) {
var digestToSignBuf = stringToArrayBuffer(message);
crypto.subtle.sign({ name: "RSASSA-PKCS1-v1_5" }, cryptoKey, digestToSignBuf)
.then(function (signature) {
// Other code will come here
});
})

More From » google-chrome

 Answers
1

I tried and got the solution. You can simply use the private key. Here it is:


var sha256 = forge.md.sha256.create();
sha256.update(message, 'utf8');
var signature = privateKey.sign(sha256);

var md5 = forge.md.md5.create();
md5.update((signature));

var required_digest = md5.digest().toHex().toUpperCase()

[#1070] Thursday, July 15, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
whitney

Total Points: 642
Total Questions: 110
Total Answers: 98

Location: Solomon Islands
Member since Mon, Jun 20, 2022
2 Years ago
;