UNPKG

tssrp6a

Version:

SRP6a client and server lib

58 lines 1.86 kB
import { SRPParameters } from "./parameters.js"; import { SRPRoutines } from "./routines.js"; /** * Crude JSON-based deserialization that has just enough exceptions for tssrp6a * classes. * Needs the serialization string and the target class prototype, e.g. * `SRPServerSessionStep1.prototype`. */ export function deserialize(str, proto) { const obj = JSON.parse(str, (key, value) => { switch (key) { case "routines": value.__proto__ = SRPRoutines.prototype; return value; case "parameters": value.__proto__ = SRPParameters.prototype; return value; case "": case "NBits": case "primeGroup": case "identifier": case "I": return value; case "IH": return new Uint8Array(JSON.parse(value)).buffer; case "H": return SRPParameters.H[value]; default: return BigInt(value); } }); obj.__proto__ = proto; return obj; } /** * Crude JSON-based serialization that has just enough exceptions for tssrp6a * classes. */ export function serialize(step) { return JSON.stringify(step, (_key, value) => { if (typeof value === "bigint") { return value.toString(); } else if (value instanceof ArrayBuffer) { return JSON.stringify(Array.from(new Uint8Array(value))); } else if (typeof value === "function") { for (const [key, fn] of Object.entries(SRPParameters.H)) { if (value === fn) { return key; } } throw new Error("cannot serialize unknown hash function"); } return value; }); } //# sourceMappingURL=serde.js.map