diafygi / webcrypto-examples

Web Cryptography API Examples Demo: https://diafygi.github.io/webcrypto-examples/
GNU General Public License v2.0
1.64k stars 193 forks source link

How pass PEM key (pem=b64(der)) to importKey? #6

Closed BaurzhanSakhariev closed 9 years ago

BaurzhanSakhariev commented 9 years ago

I exctracted pkcs#8 private key with the help of other JS library (forge). After that I converted pkcs8 to PEM (stripped "BEGIN, END" headers and footers) and tried to give that key to standart web crypto API function:

 window.crypto.subtle.importKey(
  "pkcs8", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
  //der,
  //der.getBytes().buffer,
  b64ToArrayBuffer(PEM_headers_stripped);   
     //b64ToArrayBuffer(PEM_headers_NOT_stripped);              
  {   //these are the algorithm options
   name: "RSASSA-PKCS1-v1_5",
   hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
  },
  false, //whether the key is extractable (i.e. can be used in exportKey)
  ["sign"] //"verify" for public key import, "sign" for private key imports
)
.then ...

and in console recieving message "DataError", code: 0.

You can see above, I tried to pass PEM without stripping headers, passed key in der format but all my efforts failed.

May be you know, how pass a pkcs8 in PEM format to web crypto?

Thanks.

diafygi commented 9 years ago

Howdy, what is the result of b64ToArrayBuffer()? In the live table, it's a Uint8Array object. I don't know if an ArrayBuffer object works.