@wault-pw/srp6a-webcrypto
Version:
Pure javascript implementation of SRP-6a (RFC-5054, RFC-2945) using web-crypto
63 lines • 2.82 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Engine = void 0;
const util_1 = require("./util");
class Engine {
constructor(params) {
this.N = (0, util_1.BigIntFromUint8Array)(params.N);
this.g = (0, util_1.BigIntFromInt)(params.g);
this.hash = params.hash;
}
get N_SIZE() {
return this.N.bitLength().toJSNumber() >> 3;
}
k() {
return __awaiter(this, void 0, void 0, function* () {
return (0, util_1.BigIntFromUint8Array)(yield this.HASH((0, util_1.BigInt2Uint8Array)(this.N), this.PAD((0, util_1.BigInt2Uint8Array)(this.g))));
});
}
isModZero(a, b) {
return a.mod(b).isZero();
}
HASH(...inputs) {
return __awaiter(this, void 0, void 0, function* () {
return yield (0, util_1.Hash)(this.hash, ...inputs);
});
}
// Conversion between integers and byte-strings assumes the most
// significant bytes are stored first, as per [TLS] and [SRP-RFC]. In
// the following text, if a conversion from integer to byte-string is
// implicit, the most significant byte in the resultant byte-string MUST
// be non-zero. If a conversion is explicitly specified with the
// operator PAD(), the integer will first be implicitly converted, then
// the resultant byte-string will be left-padded with zeros (if
// necessary) until its length equals the implicitly-converted length of
// N.
//
// In other words RFC5054 specifies that number should be
// left-padded with zeros to be the same length as N.
PAD(input) {
if (input.length >= this.N_SIZE) {
return input;
}
const diff = this.N_SIZE - input.length;
return new Uint8Array([...new Uint8Array(diff), ...input]);
}
HASHED_CRED(username, password) {
return __awaiter(this, void 0, void 0, function* () {
const credentials = new TextEncoder().encode(username + ":" + password);
return yield this.HASH(credentials);
});
}
}
exports.Engine = Engine;
//# sourceMappingURL=engine.js.map