Skip to content

Commit

Permalink
sasl-ht-sha-256-none: Improve implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Jan 7, 2025
1 parent 1a8a2c7 commit 59722e9
Showing 1 changed file with 21 additions and 21 deletions.
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));
if (digestS !== data) {
throw new Error("Responder message from server was wrong");
}
Expand Down

0 comments on commit 59722e9

Please sign in to comment.