Skip to content

Commit acfd086

Browse files
save file
1 parent a6574ac commit acfd086

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

blog/26-04-26/x509-certificates-in-js---encrypt-decrypt-data/ex/aes-encrypt-decrypt-browser.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,12 @@ aes encrypt / decrypt browser
7070
}//importAesKey
7171

7272

73-
async function aesEncrypt(key,blob){
73+
async function aesEncrypt(key,blob,iv_bits=96){
7474

7575
var buf = await blob.arrayBuffer();
76-
// 96-bit IV recommended
77-
var iv = crypto.getRandomValues(new Uint8Array(12));
76+
// 96-bit IV recommended ( 12 bytes )
77+
var bytes = iv_bits/8;
78+
var iv = crypto.getRandomValues(new Uint8Array(bytes));
7879

7980
var algorithm = {name:'AES-GCM',iv}
8081
key = key;
@@ -109,7 +110,7 @@ aes encrypt / decrypt browser
109110
//:
110111

111112

112-
function iv_buf_blob(iv,buf){debugger;
113+
function iv_buf_blob(iv,buf){
113114

114115
var n1 = iv.length;
115116
var n = n1+buf.length;
@@ -122,13 +123,14 @@ aes encrypt / decrypt browser
122123
}//iv_buf
123124

124125

125-
async function blob_iv_buf(blob,iv_bits=96){debugger;
126+
async function blob_iv_buf(blob,iv_bits=96){
126127

128+
var bytes = iv_bits/8;
127129
var n = blob.size;
128130
var buf = await blob.arrayBuffer();
129131
var uint8 = new Uint8Array(buf);
130-
var iv = uint8.slice(0,iv_bits);
131-
var buf = uint8.slice(iv_bits);
132+
var iv = uint8.slice(0,bytes);
133+
var buf = uint8.slice(bytes);
132134
return {iv,buf};
133135

134136
}//blob_iv_buf

0 commit comments

Comments
 (0)