Skip to content

Commit

Permalink
docs: unifying of sign/verify examples
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Apr 18, 2024
1 parent e4be2dd commit c5c4fd9
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 146 deletions.
54 changes: 30 additions & 24 deletions examples/sign_verify/sign-verify-ecdsa.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
try {
const keyPair = await crypto.subtle.generateKey(
crypto.subtle
.generateKey(
{
name: "ECDSA",
namedCurve: "P-256",
},
true,
["sign", "verify"]
)
.then(
(keyPair) => {
const data = string2ArrayBuffer("Hello World");
const alg = { name: "ECDSA", hash: { name: "SHA-256" } };

// makes a signature of the encoded data with the provided key
crypto.subtle.sign(alg, keyPair.privateKey, data).then(
(signature) => {
console.log("signature: ", printArrayBuffer(signature));
// verifies the signature of the encoded data with the provided key
crypto.subtle.verify(alg, keyPair.publicKey, signature, data).then(
(verified) => {
console.log("operation successful, verified: ", verified);
},
(err) => {
console.error("verifying, got unexpected error: " + err);
}
);
},
(err) => {
console.error("signing, got unexpected error: " + err);
}
);
},
(err) => {
console.error("key generation, got unexpected error: " + err);
}
);

const data = string2ArrayBuffer("Hello World");

const alg = { name: "ECDSA", hash: { name: "SHA-256" } };

// makes a signature of the encoded data with the provided key
const signature = await crypto.subtle.sign(alg, keyPair.privateKey, data);

console.log("signature: ", printArrayBuffer(signature));

//Verifies the signature of the encoded data with the provided key
const verified = await crypto.subtle.verify(
alg,
keyPair.publicKey,
signature,
data
);

console.log("verified: ", verified);
} catch (err) {
console.log("err: " + JSON.stringify(err));
}
}

const string2ArrayBuffer = (str) => {
Expand Down
48 changes: 28 additions & 20 deletions examples/sign_verify/sign-verify-hmac.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
try {
const generatedKey = await crypto.subtle.generateKey(
crypto.subtle
.generateKey(
{
name: "HMAC",
hash: { name: "SHA-1" },
},
true,
["sign", "verify"]
);

const encoded = string2ArrayBuffer("Hello World");

// Signs the encoded data with the provided key using the HMAC algorithm
// the returned signature can be verified using the verify method.
const signature = await crypto.subtle.sign("HMAC", generatedKey, encoded);
)
.then(
(generatedKey) => {
const encoded = string2ArrayBuffer("Hello World");

// Verifies the signature of the encoded data with the provided key using the HMAC algorithm
const verified = await crypto.subtle.verify(
"HMAC",
generatedKey,
signature,
encoded
// Signs the encoded data with the provided key using the HMAC algorithm
// the returned signature can be verified using the verify method.
crypto.subtle.sign("HMAC", generatedKey, encoded).then(
(signature) => {
// Verifies the signature of the encoded data with the provided key using the HMAC algorithm
crypto.subtle.verify("HMAC", generatedKey, signature, encoded).then(
(verified) => {
console.log("operation successful, verified: ", verified);
},
(err) => {
console.error("verifying, got unexpected error: " + err);
}
);
},
(err) => {
console.error("signing, got unexpected error: " + err);
}
);
},
(err) => {
console.error("key generation, got unexpected error: " + err);
}
);

console.log("verified: ", verified);
} catch (err) {
console.log(JSON.stringify(err));
}
}

function string2ArrayBuffer(str) {
Expand Down
124 changes: 71 additions & 53 deletions examples/sign_verify/verify-spki-ecdsa-invalid.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,77 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const publicKey = await crypto.subtle.importKey(
"spki",
new Uint8Array([
48, 118, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0,
34, 3, 98, 0, 4, 29, 49, 157, 105, 45, 202, 95, 87, 84, 186, 123, 50, 193,
22, 66, 198, 216, 210, 180, 251, 130, 73, 195, 242, 20, 215, 30, 144, 181,
37, 41, 102, 217, 127, 123, 235, 31, 170, 177, 228, 243, 226, 96, 85, 73,
194, 238, 219, 82, 3, 41, 179, 190, 166, 181, 229, 86, 36, 161, 81, 80,
161, 105, 102, 99, 95, 25, 22, 239, 4, 221, 117, 142, 105, 64, 157, 6, 51,
203, 75, 37, 153, 65, 121, 178, 42, 118, 156, 116, 52, 54, 145, 14, 121,
153, 81,
]),
{ name: "ECDSA", namedCurve: "P-384" },
true,
["verify"]
);
crypto.subtle
.importKey(
"spki",
new Uint8Array([
48, 118, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0,
34, 3, 98, 0, 4, 29, 49, 157, 105, 45, 202, 95, 87, 84, 186, 123, 50,
193, 22, 66, 198, 216, 210, 180, 251, 130, 73, 195, 242, 20, 215, 30,
144, 181, 37, 41, 102, 217, 127, 123, 235, 31, 170, 177, 228, 243, 226,
96, 85, 73, 194, 238, 219, 82, 3, 41, 179, 190, 166, 181, 229, 86, 36,
161, 81, 80, 161, 105, 102, 99, 95, 25, 22, 239, 4, 221, 117, 142, 105,
64, 157, 6, 51, 203, 75, 37, 153, 65, 121, 178, 42, 118, 156, 116, 52,
54, 145, 14, 121, 153, 81,
]),
{ name: "ECDSA", namedCurve: "P-384" },
true,
["verify"]
)
.then(
(publicKey) => {
let plaintText = new Uint8Array([
95, 77, 186, 79, 50, 12, 12, 232, 118, 114, 90, 252, 229, 251, 210,
91, 248, 62, 90, 113, 37, 160, 140, 175, 231, 60, 62, 186, 196, 33,
119, 157, 249, 213, 93, 24, 12, 58, 233, 148, 38, 69, 225, 216, 47,
238, 140, 157, 41, 75, 60, 177, 160, 138, 153, 49, 32, 27, 60, 14,
129, 252, 71, 202, 207, 131, 21, 162, 175, 102, 50, 65, 19, 195, 182,
98, 48, 195, 70, 8, 196, 244, 89, 54, 52, 206, 2, 178, 103, 54, 34,
119, 240, 168, 64, 202, 116, 188, 61, 26, 98, 54, 149, 44, 94, 215,
170, 248, 168, 254, 203, 221, 250, 117, 132, 230, 151, 140, 234, 93,
42, 91, 159, 183, 241, 180, 140, 139, 11, 229, 138, 48, 82, 2, 117,
77, 131, 118, 16, 115, 116, 121, 60, 240, 38, 170, 238, 83, 0, 114,
125, 131, 108, 215, 30, 113, 179, 69, 221, 178, 228, 68, 70, 255, 197,
185, 1, 99, 84, 19, 137, 13, 145, 14, 163, 128, 152, 74, 144, 25, 16,
49, 50, 63, 22, 219, 204, 157, 107, 225, 104, 184, 72, 133, 56, 76,
160, 62, 18, 96, 10, 193, 194, 72, 2, 138, 243, 114, 108, 201, 52, 99,
136, 46, 168, 192, 42, 171,
]);

let plaintText = new Uint8Array([
95, 77, 186, 79, 50, 12, 12, 232, 118, 114, 90, 252, 229, 251, 210, 91, 248,
62, 90, 113, 37, 160, 140, 175, 231, 60, 62, 186, 196, 33, 119, 157, 249,
213, 93, 24, 12, 58, 233, 148, 38, 69, 225, 216, 47, 238, 140, 157, 41, 75,
60, 177, 160, 138, 153, 49, 32, 27, 60, 14, 129, 252, 71, 202, 207, 131, 21,
162, 175, 102, 50, 65, 19, 195, 182, 98, 48, 195, 70, 8, 196, 244, 89, 54,
52, 206, 2, 178, 103, 54, 34, 119, 240, 168, 64, 202, 116, 188, 61, 26, 98,
54, 149, 44, 94, 215, 170, 248, 168, 254, 203, 221, 250, 117, 132, 230, 151,
140, 234, 93, 42, 91, 159, 183, 241, 180, 140, 139, 11, 229, 138, 48, 82, 2,
117, 77, 131, 118, 16, 115, 116, 121, 60, 240, 38, 170, 238, 83, 0, 114,
125, 131, 108, 215, 30, 113, 179, 69, 221, 178, 228, 68, 70, 255, 197, 185,
1, 99, 84, 19, 137, 13, 145, 14, 163, 128, 152, 74, 144, 25, 16, 49, 50, 63,
22, 219, 204, 157, 107, 225, 104, 184, 72, 133, 56, 76, 160, 62, 18, 96, 10,
193, 194, 72, 2, 138, 243, 114, 108, 201, 52, 99, 136, 46, 168, 192, 42,
171,
]);
let signature = new Uint8Array([
13, 217, 194, 199, 240, 182, 244, 217, 50, 130, 84, 169, 2, 232, 115,
116, 179, 192, 146, 25, 94, 107, 226, 26, 161, 166, 220, 216, 235,
166, 15, 123, 11, 56, 196, 0, 109, 250, 33, 70, 212, 233, 253, 35,
220, 51, 97, 121, 151, 64, 23, 73, 58, 31, 79, 116, 238, 207, 228, 85,
190, 61, 169, 237, 153, 100, 29, 129, 97, 13, 254, 180, 104, 182, 7,
218, 148, 29, 87, 20, 231, 181, 26, 238, 44, 69, 170, 14, 156, 77,
160, 33, 178, 55, 0,
]);

let signature1 = new Uint8Array([
13, 217, 194, 199, 240, 182, 244, 217, 50, 130, 84, 169, 2, 232, 115, 116,
179, 192, 146, 25, 94, 107, 226, 26, 161, 166, 220, 216, 235, 166, 15, 123,
11, 56, 196, 0, 109, 250, 33, 70, 212, 233, 253, 35, 220, 51, 97, 121, 151,
64, 23, 73, 58, 31, 79, 116, 238, 207, 228, 85, 190, 61, 169, 237, 153, 100,
29, 129, 97, 13, 254, 180, 104, 182, 7, 218, 148, 29, 87, 20, 231, 181, 26,
238, 44, 69, 170, 14, 156, 77, 160, 33, 178, 55, 0,
]);

//Verifies the signature of the encoded data with the provided key
const verified = await crypto.subtle.verify(
{
name: "ECDSA",
hash: "SHA-384",
},
publicKey,
signature1,
plaintText
);

console.log("verified: ", verified);
// Verifies the signature of the encoded data with the provided key
crypto.subtle
.verify(
{
name: "ECDSA",
hash: "SHA-384",
},
publicKey,
signature,
plaintText
)
.then(
(verified) => {
// This should print "operation successful, verified: false"
// because the signature is invalid
console.log("operation successful, verified: ", verified);
},
(err) => {
console.error("verifying, got unexpected error: " + err);
}
);
},
(err) => {
console.error("import key, got unexpected error: " + err);
}
);
}
114 changes: 65 additions & 49 deletions examples/sign_verify/verify-spki-ecdsa-valid.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,71 @@
import { crypto } from "k6/x/webcrypto";

export default async function () {
const publicKey = await crypto.subtle.importKey(
"spki",
new Uint8Array([
48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72, 206,
61, 3, 1, 7, 3, 66, 0, 4, 10, 5, 30, 56, 111, 103, 196, 166, 225, 229,
203, 238, 125, 55, 116, 91, 88, 142, 190, 114, 15, 117, 89, 22, 40, 111,
150, 41, 105, 122, 57, 23, 17, 216, 106, 234, 201, 103, 8, 210, 58, 38,
35, 216, 198, 237, 187, 84, 217, 164, 63, 100, 6, 105, 49, 128, 15, 53,
29, 158, 117, 235, 238, 30,
]),
{ name: "ECDSA", namedCurve: "P-256" },
true,
["verify"]
);
crypto.subtle
.importKey(
"spki",
new Uint8Array([
48, 89, 48, 19, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 8, 42, 134, 72,
206, 61, 3, 1, 7, 3, 66, 0, 4, 10, 5, 30, 56, 111, 103, 196, 166, 225,
229, 203, 238, 125, 55, 116, 91, 88, 142, 190, 114, 15, 117, 89, 22, 40,
111, 150, 41, 105, 122, 57, 23, 17, 216, 106, 234, 201, 103, 8, 210, 58,
38, 35, 216, 198, 237, 187, 84, 217, 164, 63, 100, 6, 105, 49, 128, 15,
53, 29, 158, 117, 235, 238, 30,
]),
{ name: "ECDSA", namedCurve: "P-256" },
true,
["verify"]
)
.then(
(publicKey) => {
let plaintText = new Uint8Array([
95, 77, 186, 79, 50, 12, 12, 232, 118, 114, 90, 252, 229, 251, 210,
91, 248, 62, 90, 113, 37, 160, 140, 175, 231, 60, 62, 186, 196, 33,
119, 157, 249, 213, 93, 24, 12, 58, 233, 148, 38, 69, 225, 216, 47,
238, 140, 157, 41, 75, 60, 177, 160, 138, 153, 49, 32, 27, 60, 14,
129, 252, 71, 202, 207, 131, 21, 162, 175, 102, 50, 65, 19, 195, 182,
98, 48, 195, 70, 8, 196, 244, 89, 54, 52, 206, 2, 178, 103, 54, 34,
119, 240, 168, 64, 202, 116, 188, 61, 26, 98, 54, 149, 44, 94, 215,
170, 248, 168, 254, 203, 221, 250, 117, 132, 230, 151, 140, 234, 93,
42, 91, 159, 183, 241, 180, 140, 139, 11, 229, 138, 48, 82, 2, 117,
77, 131, 118, 16, 115, 116, 121, 60, 240, 38, 170, 238, 83, 0, 114,
125, 131, 108, 215, 30, 113, 179, 69, 221, 178, 228, 68, 70, 255, 197,
185, 1, 99, 84, 19, 137, 13, 145, 14, 163, 128, 152, 74, 144, 25, 16,
49, 50, 63, 22, 219, 204, 157, 107, 225, 104, 184, 72, 133, 56, 76,
160, 62, 18, 96, 10, 193, 194, 72, 2, 138, 243, 114, 108, 201, 52, 99,
136, 46, 168, 192, 42, 171,
]);

let plaintText = new Uint8Array([
95, 77, 186, 79, 50, 12, 12, 232, 118, 114, 90, 252, 229, 251, 210, 91, 248,
62, 90, 113, 37, 160, 140, 175, 231, 60, 62, 186, 196, 33, 119, 157, 249,
213, 93, 24, 12, 58, 233, 148, 38, 69, 225, 216, 47, 238, 140, 157, 41, 75,
60, 177, 160, 138, 153, 49, 32, 27, 60, 14, 129, 252, 71, 202, 207, 131, 21,
162, 175, 102, 50, 65, 19, 195, 182, 98, 48, 195, 70, 8, 196, 244, 89, 54,
52, 206, 2, 178, 103, 54, 34, 119, 240, 168, 64, 202, 116, 188, 61, 26, 98,
54, 149, 44, 94, 215, 170, 248, 168, 254, 203, 221, 250, 117, 132, 230, 151,
140, 234, 93, 42, 91, 159, 183, 241, 180, 140, 139, 11, 229, 138, 48, 82, 2,
117, 77, 131, 118, 16, 115, 116, 121, 60, 240, 38, 170, 238, 83, 0, 114,
125, 131, 108, 215, 30, 113, 179, 69, 221, 178, 228, 68, 70, 255, 197, 185,
1, 99, 84, 19, 137, 13, 145, 14, 163, 128, 152, 74, 144, 25, 16, 49, 50, 63,
22, 219, 204, 157, 107, 225, 104, 184, 72, 133, 56, 76, 160, 62, 18, 96, 10,
193, 194, 72, 2, 138, 243, 114, 108, 201, 52, 99, 136, 46, 168, 192, 42,
171,
]);
let signature1 = new Uint8Array([
83, 223, 63, 226, 42, 29, 106, 105, 225, 145, 197, 180, 118, 154, 109,
110, 66, 67, 47, 251, 53, 190, 203, 65, 207, 36, 19, 57, 49, 122, 124,
118, 59, 74, 222, 134, 42, 235, 180, 229, 134, 24, 205, 81, 171, 156,
100, 218, 127, 242, 126, 53, 27, 77, 249, 101, 157, 132, 244, 30, 67,
30, 64, 12,
]);

let signature1 = new Uint8Array([
83, 223, 63, 226, 42, 29, 106, 105, 225, 145, 197, 180, 118, 154, 109, 110,
66, 67, 47, 251, 53, 190, 203, 65, 207, 36, 19, 57, 49, 122, 124, 118, 59,
74, 222, 134, 42, 235, 180, 229, 134, 24, 205, 81, 171, 156, 100, 218, 127,
242, 126, 53, 27, 77, 249, 101, 157, 132, 244, 30, 67, 30, 64, 12,
]);

//Verifies the signature of the encoded data with the provided key
const verified = await crypto.subtle.verify(
{
name: "ECDSA",
hash: "SHA-256",
},
publicKey,
signature1,
plaintText
);

console.log("verified: ", verified);
// Verifies the signature of the encoded data with the provided key
crypto.subtle
.verify(
{
name: "ECDSA",
hash: "SHA-256",
},
publicKey,
signature1,
plaintText
)
.then(
(verified) => {
console.log("operation successful, verified: ", verified);
},
(err) => {
console.error("verifying, got unexpected error: " + err);
}
);
},
(err) => {
console.error("import key, got unexpected error: " + err);
}
);
}

0 comments on commit c5c4fd9

Please sign in to comment.