Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert passing signatureValue and fix formatting #78

Merged
merged 1 commit into from
Jan 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 62 additions & 70 deletions web-crypto/sign-verify/rsassa-pkcs1.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,88 @@
(() => {
/*

/*
Store the calculated signature here, so we can verify it later.
*/
let signature;
let signature;

/*
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for sign operation.
*/
function getMessageEncoding() {
const messageBox = document.querySelector("#rsassa-pkcs1-message");
let message = messageBox.value;
let enc = new TextEncoder();
return enc.encode(message);
}
function getMessageEncoding() {
const messageBox = document.querySelector("#rsassa-pkcs1-message");
let message = messageBox.value;
let enc = new TextEncoder();
return enc.encode(message);
}

/*
/*
Get the encoded message-to-sign, sign it and display a representation
of the first part of it in the "signature" element.
*/
async function signMessage(privateKey) {
const signatureValue = document.querySelector(
".rsassa-pkcs1 .signature-value"
);
signatureValue.classList.remove("valid", "invalid");
async function signMessage(privateKey) {
const signatureValue = document.querySelector(".rsassa-pkcs1 .signature-value");
signatureValue.classList.remove("valid", "invalid");

let encoded = getMessageEncoding();
signature = await window.crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
privateKey,
encoded
);
let encoded = getMessageEncoding();
signature = await window.crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
privateKey,
encoded
);

signatureValue.classList.add("fade-in");
signatureValue.addEventListener("animationend", () => {
signatureValue.classList.remove("fade-in");
});
let buffer = new Uint8Array(signature, 0, 5);
signatureValue.textContent = `${buffer}...[${signature.byteLength} bytes total]`;
}
signatureValue.classList.add('fade-in');
signatureValue.addEventListener('animationend', () => {
signatureValue.classList.remove('fade-in');
});
let buffer = new Uint8Array(signature, 0, 5);
signatureValue.textContent = `${buffer}...[${signature.byteLength} bytes total]`;
}

/*
/*
Fetch the encoded message-to-sign and verify it against the stored signature.
* If it checks out, set the "valid" class on the signature.
* Otherwise set the "invalid" class.
*/
async function verifyMessage(publicKey) {
const signatureValue = document.querySelector(
".rsassa-pkcs1 .signature-value"
);
signatureValue.classList.remove("valid", "invalid");
async function verifyMessage(publicKey) {
const signatureValue = document.querySelector(".rsassa-pkcs1 .signature-value");
signatureValue.classList.remove("valid", "invalid");

let encoded = getMessageEncoding();
let result = await window.crypto.subtle.verify(
"RSASSA-PKCS1-v1_5",
publicKey,
signatureValue,
encoded
);
let encoded = getMessageEncoding();
let result = await window.crypto.subtle.verify(
"RSASSA-PKCS1-v1_5",
publicKey,
signature,
encoded
);

signatureValue.classList.add(result ? "valid" : "invalid");
}
signatureValue.classList.add(result ? "valid" : "invalid");
}

/*
/*
Generate a sign/verify key, then set up event listeners
on the "Sign" and "Verify" buttons.
*/
window.crypto.subtle
.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
// Consider using a 4096-bit key for systems that require long-term security
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"]
)
.then((keyPair) => {
const signButton = document.querySelector(
".rsassa-pkcs1 .sign-button"
);
signButton.addEventListener("click", () => {
signMessage(keyPair.privateKey);
});
window.crypto.subtle.generateKey(
{
name: "RSASSA-PKCS1-v1_5",
// Consider using a 4096-bit key for systems that require long-term security
modulusLength: 2048,
publicExponent: new Uint8Array([1, 0, 1]),
hash: "SHA-256",
},
true,
["sign", "verify"]
).then((keyPair) => {
const signButton = document.querySelector(".rsassa-pkcs1 .sign-button");
signButton.addEventListener("click", () => {
signMessage(keyPair.privateKey);
});

const verifyButton = document.querySelector(".rsassa-pkcs1 .verify-button");
verifyButton.addEventListener("click", () => {
verifyMessage(keyPair.publicKey);
});
});

const verifyButton = document.querySelector(
".rsassa-pkcs1 .verify-button"
);
verifyButton.addEventListener("click", () => {
verifyMessage(keyPair.publicKey);
});
});
})();