-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from grafana/feature/encrypt-decrypt-symmetric
[3/3] Implement the `encrypt` and `decrypt` operations with support for AES algorithms
- Loading branch information
Showing
17 changed files
with
2,005 additions
and
93 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,50 @@ | ||
import { crypto } from "k6/x/webcrypto"; | ||
|
||
export default async function () { | ||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: "AES-CBC", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = stringToArrayBuffer("Hello, World!"); | ||
const iv = crypto.getRandomValues(new Uint8Array(16)); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-CBC", | ||
iv: iv, | ||
}, | ||
key, | ||
encoded | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-CBC", | ||
iv: iv, | ||
}, | ||
key, | ||
ciphertext, | ||
); | ||
|
||
console.log("deciphered text == original text: ", arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)) | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function stringToArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
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,52 @@ | ||
import { crypto } from "k6/x/webcrypto"; | ||
|
||
export default async function () { | ||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: "AES-CTR", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = string2ArrayBuffer("Hello World"); | ||
const counter = crypto.getRandomValues(new Uint8Array(16)); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-CTR", | ||
counter, | ||
length: 64, | ||
}, | ||
key, | ||
encoded | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-CTR", | ||
counter, | ||
length: 64, | ||
}, | ||
key, | ||
ciphertext, | ||
); | ||
|
||
console.log("deciphered text == original text: ", arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)) | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function string2ArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
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,50 @@ | ||
import { crypto } from "k6/x/webcrypto"; | ||
|
||
export default async function () { | ||
const key = await crypto.subtle.generateKey( | ||
{ | ||
name: "AES-GCM", | ||
length: 256, | ||
}, | ||
true, | ||
["encrypt", "decrypt"] | ||
); | ||
|
||
const encoded = string2ArrayBuffer("Hello World"); | ||
const iv = crypto.getRandomValues(new Uint8Array(12)); | ||
|
||
const ciphertext = await crypto.subtle.encrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv: iv, | ||
}, | ||
key, | ||
encoded | ||
); | ||
|
||
const plaintext = await crypto.subtle.decrypt( | ||
{ | ||
name: "AES-GCM", | ||
iv: iv, | ||
}, | ||
key, | ||
ciphertext, | ||
); | ||
|
||
console.log("deciphered text == original text: ", arrayBufferToHex(plaintext) === arrayBufferToHex(encoded)) | ||
} | ||
|
||
function arrayBufferToHex(buffer) { | ||
return [...new Uint8Array(buffer)] | ||
.map((x) => x.toString(16).padStart(2, "0")) | ||
.join(""); | ||
} | ||
|
||
function string2ArrayBuffer(str) { | ||
var buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char | ||
var bufView = new Uint16Array(buf); | ||
for (var i = 0, strLen = str.length; i < strLen; i++) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} |
Oops, something went wrong.