Sign with RSA and share the key as PEM
Sign with RSA-PSS in the browser and verify from the public key shared as PEM.
Apache-2.0Runs on this page
Package: @awacloud/fw
Sign a message with an RSA-PSS key whose private half never leaves the browser, publish the public key as PEM, and verify from that PEM text.
Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.
When to use it
When the other side expects RSA: a release signature checked by an
existing tool, or a server that stores public keys as PEM. For a new
design with no such constraint, Ed25519 (fw/crypto-signature) has
shorter keys and signatures.
Copy-paste steps
npm install @awacloud/fw- Serve
node_modules/@awacloud/fw/src/at/vendor/fw/(node serve.jsfrom the examples root does this; any static server works). WebCrypto needs https orlocalhost. - Copy
index.htmlandmain.jsnext to each other and openindex.htmlthrough that server. The../../_style/examples.csslink is optional: copy that file along, or delete the link.
What it shows
webcryptoRsa.generateKey('PSS', 2048, 'SHA-256', false): the browser's native RSA.falsemakes the private key non-extractable: it can sign, and no script can read it out.webcryptoRsa.exportKey('spki', publicKey)gives DER bytes;pem.encode(bytes, 'PUBLIC KEY')wraps them in the-----BEGIN PUBLIC KEY-----text other tools read.- Verify starts from the PEM text, as a receiver would:
pem.decode(text, 'PUBLIC KEY'),webcryptoRsa.importKey('spki', …), thenwebcryptoRsa.verify. Edit the message to check, or paste another key, and the verdict changes. pem.decodewith an expected label refuses any other block (a private key, a certificate) before any cryptography runs.- The fw WebCrypto modules never throw: a failure resolves to
false, so the page tests the result instead of catching.
Code
main.js
// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { webcryptoRsa } from '@awacloud/fw/crypto/webcrypto/rsa.js';
import { pem } from '@awacloud/fw/crypto/utils/pem.js';
fw.runtime.registerAllDeep([webcryptoRsa, pem]);
const RSA = fw.runtime.resolve('webcryptoRsa');
const PEM = fw.runtime.resolve('pem');
const B64 = fw.runtime.resolve('b64'); // registered as a pem dependency
/** @param {string} id */
const el = (id) => /** @type {HTMLTextAreaElement} */ (document.getElementById(id));
const encoder = new TextEncoder();
const status = el('status');
/** @type {Uint8Array | null} */
let signature = null;
// Sign: a fresh RSA-PSS key whose private half cannot be exported; only the public key leaves, as PEM.
el('sign').addEventListener('click', async () => {
status.textContent = 'Generating a 2048-bit key…';
const keys = await RSA.generateKey('PSS', 2048, 'SHA-256', false);
if (!keys) {
status.textContent = 'No WebCrypto here: it needs https or localhost.';
return;
}
signature = await RSA.sign(keys.privateKey, encoder.encode(el('message').value));
el('pem').value = PEM.encode(await RSA.exportKey('spki', keys.publicKey), 'PUBLIC KEY');
el('signature').textContent = B64.fromBytes(/** @type {Uint8Array} */ (signature));
el('check').value = el('message').value;
status.textContent = 'Signed. Publish the PEM key; send the message with its signature.';
});
// Verify from what was published: the PEM text, not the key object, so a pasted key works too.
el('verify').addEventListener('click', async () => {
if (!signature) return;
const block = PEM.decode(el('pem').value, 'PUBLIC KEY');
if (!block) {
el('verdict').textContent = 'Not a PUBLIC KEY PEM block.';
return;
}
const key = await RSA.importKey('spki', block.bytes, 'PSS', 'SHA-256', ['verify']);
const valid = key && await RSA.verify(key, signature, encoder.encode(el('check').value));
el('verdict').textContent = valid
? 'Valid: this key signed this exact message.'
: 'Not valid: the message, the signature or the key changed.';
});index.html
<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>fw · Sign with RSA and share the key as PEM</title>
<link rel="stylesheet" href="../../_style/examples.css">
<script type="importmap">
{ "imports": {
"@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
</script>
<script type="module" src="./main.js"></script>
</head>
<body>
<main>
<h1>Sign with RSA and share the key as PEM</h1>
<label for="message">Message to sign</label>
<textarea id="message" rows="2">Release 1.4.0, sha256 of the archive: 9f86d081884c7d65</textarea>
<p><button id="sign" type="button">Generate a key and sign</button></p>
<p id="status" role="status"></p>
<label for="pem">Public key (PEM, editable)</label>
<textarea id="pem" rows="9" spellcheck="false"></textarea>
<p>Signature (RSA-PSS, base64): <code id="signature"></code></p>
<label for="check">Message to check</label>
<textarea id="check" rows="2"></textarea>
<p><button id="verify" type="button">Verify with the PEM key</button></p>
<p><output id="verdict"></output></p>
</main>
</body>
</html>