export async function main() {
const keys = await window.crypto.subtle.generateKey(
{
name: "RSA-PSS",
modulusLength: 1024,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"],
);
return {
private: await exportKey(keys.privateKey, true),
public: await exportKey(keys.publicKey, false),
};
}
async function exportKey(key: CryptoKey, isPrivateKey: boolean) {
const exported = await window.crypto.subtle.exportKey(
isPrivateKey ? "pkcs8" : "spki",
key,
);
const exportedAsString = ab2str(exported);
const exportedAsBase64 = window.btoa(exportedAsString);
const pemExported = `-----BEGIN ${
isPrivateKey ? "PRIVATE" : "PUBLIC"
} KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`;
return pemExported;
}
function ab2str(buf: ArrayBuffer) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
Submitted by admin 479 days ago
export async function main() {
const keys = await window.crypto.subtle.generateKey(
{
name: "RSA-PSS",
modulusLength: 1024,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"],
);
return {
"private": await exportKey(keys.privateKey, true),
"public": await exportKey(keys.publicKey, false),
};
}
async function exportKey(key: CryptoKey, isPrivateKey: boolean) {
const exported = await window.crypto.subtle.exportKey(
isPrivateKey ? "pkcs8" : "spki",
key,
);
const exportedAsString = ab2str(exported);
const exportedAsBase64 = window.btoa(exportedAsString);
const pemExported = `-----BEGIN ${
isPrivateKey ? "PRIVATE" : "PUBLIC"
} KEY-----\n${exportedAsBase64}\n-----END PRIVATE KEY-----`;
return pemExported;
}
function ab2str(buf: ArrayBuffer) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
}
Submitted by admin 690 days ago