@@ -1545,6 +1545,97 @@ example.js:
1545
1545
curl http://localhost/read
1546
1546
200 < empty reply>
1547
1547
1548
+ Webcrypto (AES-GSM) [misc/aes_gsm]
1549
+ ----------------------------------
1550
+
1551
+ nginx.conf:
1552
+
1553
+ .. code-block :: nginx
1554
+
1555
+ http {
1556
+ js_path "/etc/nginx/njs/";
1557
+
1558
+ js_import main from misc/aes_gsm.js;
1559
+
1560
+ server {
1561
+ listen 80;
1562
+
1563
+ location /encrypt {
1564
+ js_content main.encrypt;
1565
+ }
1566
+
1567
+ location /decrypt {
1568
+ js_content main.decrypt;
1569
+ }
1570
+ }
1571
+ }
1572
+
1573
+ example.js:
1574
+
1575
+ .. code-block :: js
1576
+
1577
+ async function encryptUAM (key_in , iv , text ) {
1578
+ const alg = { name: ' AES-GCM' , iv: iv ? Buffer .from (iv, ' hex' )
1579
+ : crypto .getRandomValues (new Uint8Array (12 )) };
1580
+
1581
+ const sha256 = await crypto .subtle .digest (' SHA-256' , new TextEncoder ().encode (key_in));
1582
+ const key = await crypto .subtle .importKey (' raw' , sha256, alg, false , [' encrypt' ]);
1583
+
1584
+ const cipher = await crypto .subtle .encrypt (alg, key, new TextEncoder ().encode (text));
1585
+
1586
+ return JSON .stringify ({
1587
+ cipher: btoa (String .fromCharCode .apply (null , new Uint8Array (cipher))),
1588
+ iv: btoa (String .fromCharCode .apply (null , new Uint8Array (alg .iv ))),
1589
+ });
1590
+ }
1591
+
1592
+ async function decryptUAM (key_in , value ) {
1593
+ value = JSON .parse (value);
1594
+
1595
+ ngx .log (ngx .ERR , njs .dump (value))
1596
+ const alg = { name: ' AES-GCM' , iv: Buffer .from (value .iv , ' base64' ) };
1597
+ const sha256 = await crypto .subtle .digest (' SHA-256' , new TextEncoder ().encode (key_in));
1598
+ const key = await crypto .subtle .importKey (' raw' , sha256, alg, false , [' decrypt' ]);
1599
+
1600
+ const decrypt = await crypto .subtle .decrypt (alg, key, Buffer .from (value .cipher , ' base64' ));
1601
+ ngx .log (ngx .ERR , njs .dump (new Uint8Array (decrypt)))
1602
+ return new TextDecoder ().decode (decrypt);
1603
+ }
1604
+
1605
+ async function encrypt (r ) {
1606
+ try {
1607
+ let encrypted = await encryptUAM (r .args .key , r .args .iv , r .requestText );
1608
+ r .return (200 , encrypted);
1609
+ } catch (e) {
1610
+ r .return (500 , ` encryption failed with ${ e .message } ` );
1611
+ }
1612
+ }
1613
+
1614
+ async function decrypt (r ) {
1615
+ try {
1616
+ let decrypted = await decryptUAM (r .args .key , r .requestText );
1617
+ r .return (200 , decrypted);
1618
+ } catch (e) {
1619
+ r .return (500 , ` decryption failed with ${ e .message } ` );
1620
+ }
1621
+ }
1622
+
1623
+ export default {encrypt, decrypt};
1624
+
1625
+ .. code-block :: shell
1626
+
1627
+ curl ' http://localhost/encrypt?key=mySecret&iv=000000000000000000000001' -d TEXT-TO-BE-ENCODED
1628
+ {" cipher" :" kLKXeb/h1inwXYlP7M504xCD+/1sF4yesCSUc7/OJiyPyw==" ," iv" :" AAAAAAAAAAAAAAAB" }
1629
+
1630
+ curl ' http://localhost/decrypt?key=mySecret' -d ' {"cipher":"kLKXeb/h1inwXYlP7M504xCD+/1sF4yesCSUc7/OJiyPyw==","iv":"AAAAAAAAAAAAAAAA"}'
1631
+ decryption failed with EVP_DecryptFinal_ex () failed
1632
+
1633
+ curl ' http://localhost/decrypt?key=mySecre' -d ' {"cipher":"kLKXeb/h1inwXYlP7M504xCD+/1sF4yesCSUc7/OJiyPyw==","iv":"AAAAAAAAAAAAAAAB"}'
1634
+ decryption failed with EVP_DecryptFinal_ex () failed
1635
+
1636
+ curl ' http://localhost/decrypt?key=mySecret' -d ' {"cipher":"kLKXeb/h1inwXYlP7M504xCD+/1sF4yesCSUc7/OJiyPyw==","iv":"AAAAAAAAAAAAAAAB"}'
1637
+ TEXT-TO-BE-ENCODED
1638
+
1548
1639
Command line interface
1549
1640
======================
1550
1641
0 commit comments