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

sasl-ht-sha-256-none: Improve implementation #1047

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
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
42 changes: 21 additions & 21 deletions packages/sasl-ht-sha-256-none/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
/* eslint-disable n/no-unsupported-features/node-builtins */

// https://datatracker.ietf.org/doc/draft-schmaus-kitten-sasl-ht/
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API

export function Mechanism() {}

Mechanism.prototype.Mechanism = Mechanism;
Mechanism.prototype.name = "HT-SHA-256-NONE";
Mechanism.prototype.clientFirst = true;

Mechanism.prototype.response = async function response(cred) {
this.password = cred.password;
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const hmac = await crypto.subtle.importKey(
Mechanism.prototype.response = async function response({ username, password }) {
this.key = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(this.password),
new TextEncoder().encode(password),
// https://developer.mozilla.org/en-US/docs/Web/API/HmacImportParams
{ name: "HMAC", hash: "SHA-256" },
false,
["sign", "verify"]
false, //extractable
["sign", "verify"],
);
const digest = await crypto.subtle.sign(
"HMAC",
this.key,
new TextEncoder().encode("Initiator"),
);
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const digest = await crypto.subtle.sign("HMAC", hmac, new TextEncoder().encode("Initiator"));
const digestS = String.fromCharCode.apply(null, new Uint8Array(digest));
return cred.username + "\0" + digestS;
const digestS = String.fromCodePoint(...new Uint8Array(digest));
return username + "\0" + digestS;
};

Mechanism.prototype.final = async function final(data) {
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const hmac = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(this.password),
{ name: "HMAC", hash: "SHA-256" },
false,
["sign", "verify"]
const digest = await crypto.subtle.sign(
"HMAC",
this.key,
new TextEncoder().encode("Responder"),
);
// eslint-disable-next-line n/no-unsupported-features/node-builtins
const digest = await crypto.subtle.sign("HMAC", hmac, new TextEncoder().encode("Responder"));
const digestS = String.fromCharCode.apply(null, new Uint8Array(digest));
const digestS = String.fromCodePoint(...new Uint8Array(digest));
sonnyp marked this conversation as resolved.
Show resolved Hide resolved
if (digestS !== data) {
throw new Error("Responder message from server was wrong");
}
Expand Down
Loading