diafygi / webcrypto-examples

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

How to import base64 encoded key? #31

Open mattmazzola opened 8 years ago

mattmazzola commented 8 years ago

Hi,

I was experimenting with your examples to try to recreate a browser version of: https://www.npmjs.com/package/jwt-simple

Here is a gist to explain end goal (although this gist doesn't work): https://gist.github.com/mattmazzola/1eafd7aea79e082982da203ec0405997

When attempting to import the key I am receiving the error: undefined:1 Uncaught (in promise) DOMException: The JWK member "k" could not be base64url decoded or contained padding

I was using this example: https://github.com/diafygi/webcrypto-examples#hmac---importkey

I know the trailing equal signs indicate there is padding on the key ==, but I'm not sure how to remove it and still import using the same method. It seems like I would have to convert it to an ArrayBuffer and then remove the trailing 0's somehow, but then if we have to use raw key import it makes using jwk seems less useful and I thought there must be a better way or perhaps the import jwk should just handle this for us.

mahrud commented 6 years ago

@mattmazzola Just in case this hasn't been resolved yet: JWK accepts Base64URL encoded values for x, y, and d, so probably finding a base64 conversion library that supports both modes would be easiest.

Note that besides the ='s, the URL-mode also switches _ and / to make the string URL-safe. Cf. https://en.wikipedia.org/wiki/Base64#Implementations_and_history