Last active
April 17, 2024 12:54
-
-
Save IanWold/8e337a22ce7eb386b65bb77948eebd66 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Encryption(IJSRuntime jsRuntime) | |
{ | |
public async Task<string> GetEncryptionKeyAsync() => | |
await jsRuntime.InvokeAsync<string>("getEncryptionKey") ?? string.Empty; | |
public async Task<string> EncryptAsync(string value) => | |
await jsRuntime.InvokeAsync<string>("encrypt", value) ?? string.Empty; | |
public async Task<string> DecryptAsync(string value) => | |
await jsRuntime.InvokeAsync<string>("decrypt", value) ?? string.Empty; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded', async () => { | |
const objectKey = window.location.hash.slice("#key=".length); | |
if (objectKey.length > 0) { | |
window.encryptionKey = await window.crypto.subtle.importKey( | |
"jwk", | |
{ | |
k: objectKey, | |
alg: "A128GCM", | |
ext: true, | |
key_ops: ["encrypt", "decrypt"], | |
kty: "oct", | |
}, | |
{ name: "AES-GCM", length: 128 }, | |
true, | |
["encrypt", "decrypt"] | |
); | |
} | |
}); | |
async function getEncryptionKey() { | |
if (!window.encryptionKey) { | |
window.encryptionKey = await window.crypto.subtle.generateKey( | |
{ name: "AES-GCM", length: 128 }, | |
true, | |
["encrypt", "decrypt"], | |
); | |
} | |
return (await window.crypto.subtle.exportKey("jwk", window.encryptionKey)).k | |
} | |
async function encrypt(value) { | |
return window.btoa(String.fromCharCode.apply(null, new Uint8Array( | |
await window.crypto.subtle.encrypt( | |
{ name: "AES-GCM", iv: new Uint8Array(12) }, | |
window.encryptionKey, | |
new TextEncoder().encode(value) | |
) | |
))); | |
} | |
async function decrypt(value) { | |
const bValue = window.atob(value) | |
const buffer = new ArrayBuffer(bValue.length) | |
const bufferView = new Uint8Array(buffer) | |
for (let i = 0; i < bValue.length; i++) { | |
bufferView[i] = bValue.charCodeAt(i) | |
} | |
return new TextDecoder().decode(new Uint8Array( | |
await window.crypto.subtle.decrypt( | |
{ name: "AES-GCM", iv: new Uint8Array(12) }, | |
window.encryptionKey, | |
buffer | |
) | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment