-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathrsa_signature.ts
43 lines (37 loc) · 1.11 KB
/
rsa_signature.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @title RSASSA-PKCS1-v1_5 Signature and Verification
* @difficulty intermediate
* @tags cli, web
* @run <url>
* @group Cryptography
*
* This example demonstrates RSA signature and verification using Deno's built-in SubtleCrypto API.
*/
// Convert the text to a Uint8Array using TextEncoder (required for signing)
const data = new TextEncoder().encode("Hello, Deno 2.0!");
const { publicKey, privateKey } = await crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
modulusLength: 2048, // 2048-bit key for strong security
publicExponent: new Uint8Array([1, 0, 1]), // Public exponent: 65537
hash: { name: "SHA-256" },
},
true,
["verify", "sign"],
);
// Sign the data using the private key
const signature = await crypto.subtle.sign(
{ name: "RSASSA-PKCS1-v1_5" },
privateKey,
data,
);
// Log the signature as a byte array
console.log("Signature:", new Uint8Array(signature));
// Verify the signature using the public key
const verification = await crypto.subtle.verify(
{ name: "RSASSA-PKCS1-v1_5" },
publicKey,
signature,
data,
);
console.log("Verification:", verification);