-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add] Webcrypto example misc/aes_gcm
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load_module modules/ngx_http_js_module.so; | ||
|
||
events { } | ||
|
||
http { | ||
js_path "/etc/nginx/njs/"; | ||
|
||
js_import main from misc/aes_gcm.js; | ||
|
||
server { | ||
listen 80; | ||
|
||
location /encrypt { | ||
js_content main.encrypt; | ||
} | ||
|
||
location /decrypt { | ||
js_content main.decrypt; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
async function encryptUAM(key_in, iv, text) { | ||
const alg = { name: 'AES-GCM', iv: iv ? Buffer.from(iv, 'hex') | ||
: crypto.getRandomValues(new Uint8Array(12)) }; | ||
|
||
const sha256 = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key_in)); | ||
const key = await crypto.subtle.importKey('raw', sha256, alg, false, ['encrypt']); | ||
|
||
const cipher = await crypto.subtle.encrypt(alg, key, new TextEncoder().encode(text)); | ||
|
||
return JSON.stringify({ | ||
cipher: btoa(String.fromCharCode.apply(null, new Uint8Array(cipher))), | ||
iv: btoa(String.fromCharCode.apply(null, new Uint8Array(alg.iv))), | ||
}); | ||
} | ||
|
||
async function decryptUAM(key_in, value) { | ||
value = JSON.parse(value); | ||
|
||
const alg = { name: 'AES-GCM', iv: Buffer.from(value.iv, 'base64') }; | ||
const sha256 = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key_in)); | ||
const key = await crypto.subtle.importKey('raw', sha256, alg, false, ['decrypt']); | ||
|
||
const decrypt = await crypto.subtle.decrypt(alg, key, Buffer.from(value.cipher, 'base64')); | ||
return new TextDecoder().decode(decrypt); | ||
} | ||
|
||
async function encrypt(r) { | ||
try { | ||
let encrypted = await encryptUAM(r.args.key, r.args.iv, r.requestText); | ||
r.return(200, encrypted); | ||
} catch (e) { | ||
r.return(500, `encryption failed with ${e.message}`); | ||
} | ||
} | ||
|
||
async function decrypt(r) { | ||
try { | ||
let decrypted = await decryptUAM(r.args.key, r.requestText); | ||
r.return(200, decrypted); | ||
} catch (e) { | ||
r.return(500, `decryption failed with ${e.message}`); | ||
} | ||
} | ||
|
||
export default {encrypt, decrypt}; |