Web Crypto API with SpawnDev.BlazorJS #37
LostBeard
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Web Crypto API on MDN
From Web Crypto API on w3.org
The Web Crypto API is a very useful set of tools. Using those tools is easy with SpawnDev.BlazorJS. Here are some code examples demonstrating signing, verifying, and public key encryption.
From CryptoService.cs in this repo.
Creating and verifying signatures using ECDSA
Creating and verifying signatures using RSA-PSS
Encrypting and decrypting data using ECDH and AES-GCM public key encryption
Storing keys
The extractable parameter of the SubtleCrypto.GenerateKey method is a simple boolean, but it has a very significant meaning.
For public/private key pairs, setting
extractable
tofalse
will prevent the private key from being exportable to Javascript. (The public key can still be exported.) This makes the key pair significantly more secure because even if malicious code were to run in your web page, it would not be able to exfiltrate your private key. Therefore, non-extractable signing keypair can be trusted to identify a browser without the possibility of the keys being shared, or stolen.So how do you save a key pair for re-use between sessions if it cannot be exported?
The answer is IndexedDB.
From Key concepts and usage
IndexedDB is a special type of web browser storage that supports storing some item types that cannot be stored anywhere else in a web browser, such as CryptoKey, CryptoKeyPair, FileSystemFileHandle, etc.
Here is a very basic example that creates a signing key pair, stores it in an IndexedDB store, and retrieves it.
These Web Crypto API examples are just a small taste of what is possible in Blazor WebAssembly with SpawnDev.BlazorJS.
Hopefully this helps someone. Feel free to request other examples of how to use use SpawnDev.BlazorJS to utilize any browser Web APIs. 🚀
Beta Was this translation helpful? Give feedback.
All reactions