forked from mystiq/hydrogen-web
support jwk keys to encrypt
This commit is contained in:
parent
e2dd9b9f77
commit
e9324ad678
2 changed files with 6 additions and 4 deletions
|
@ -62,7 +62,7 @@ export async function encryptAttachment(platform, blob) {
|
||||||
const iv = await crypto.aes.generateIV();
|
const iv = await crypto.aes.generateIV();
|
||||||
const key = await crypto.aes.generateKey("jwk", 256);
|
const key = await crypto.aes.generateKey("jwk", 256);
|
||||||
const buffer = await blob.readAsBuffer();
|
const buffer = await blob.readAsBuffer();
|
||||||
const ciphertext = await crypto.aes.encryptCTR({key, iv, data: buffer});
|
const ciphertext = await crypto.aes.encryptCTR({jwkKey: key, iv, data: buffer});
|
||||||
const digest = await crypto.digest("SHA-256", ciphertext);
|
const digest = await crypto.digest("SHA-256", ciphertext);
|
||||||
return {
|
return {
|
||||||
blob: platform.createBlob(ciphertext, blob.mimeType),
|
blob: platform.createBlob(ciphertext, blob.mimeType),
|
||||||
|
|
|
@ -199,17 +199,19 @@ class AESCrypto {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async encryptCTR({key, iv, data}) {
|
async encryptCTR({key, jwkKey, iv, data}) {
|
||||||
const opts = {
|
const opts = {
|
||||||
name: "AES-CTR",
|
name: "AES-CTR",
|
||||||
counter: iv,
|
counter: iv,
|
||||||
length: 64,
|
length: 64,
|
||||||
};
|
};
|
||||||
let aesKey;
|
let aesKey;
|
||||||
|
const selectedKey = key || jwkKey;
|
||||||
|
const format = jwkKey ? "jwk" : "raw";
|
||||||
try {
|
try {
|
||||||
aesKey = await subtleCryptoResult(this._subtleCrypto.importKey(
|
aesKey = await subtleCryptoResult(this._subtleCrypto.importKey(
|
||||||
"raw",
|
format,
|
||||||
key,
|
selectedKey,
|
||||||
opts,
|
opts,
|
||||||
false,
|
false,
|
||||||
['encrypt'],
|
['encrypt'],
|
||||||
|
|
Loading…
Reference in a new issue