UNPKG

@wault-pw/srp6a-webcrypto

Version:

Pure javascript implementation of SRP-6a (RFC-5054, RFC-2945) using web-crypto

91 lines 3.68 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Hash = exports.SecureRandom = exports.SecureEqual = exports.Uint8ArrayFromHex = exports.Uint8Array2Hex = exports.BigInt2Uint8Array = exports.EuclideanModPow = exports.BigIntFromInt = exports.BigIntFromUint8Array = void 0; const big_integer_1 = __importDefault(require("big-integer")); function BigIntFromUint8Array(input) { return big_integer_1.default.fromArray(Array.from(input), 256); } exports.BigIntFromUint8Array = BigIntFromUint8Array; function BigIntFromInt(input) { return (0, big_integer_1.default)(input); } exports.BigIntFromInt = BigIntFromInt; // @refs https://github.com/peterolson/BigInteger.js/issues/46 function EuclideanModPow(a, b, m) { const res = (0, big_integer_1.default)(a).modPow(b, m); return res.isNegative() ? res.add(m) : res; } exports.EuclideanModPow = EuclideanModPow; // convert to big-endian byte array // Java, GO anf etc. BigInteger math will trim leading zeros so we do likewise // @refs https://github.com/simbo1905/thinbus-srp-npm/blob/master/client.js#L111 // while (array[0] === 0) { // array = array.slice(1, array.length) // } // the code above was used with "jsbn" function BigInt2Uint8Array(input) { let array = input.toArray(256).value; return new Uint8Array(array); } exports.BigInt2Uint8Array = BigInt2Uint8Array; function Uint8Array2Hex(input) { const out = []; input.forEach((n, ix) => out[ix] = n.toString(16).padStart(2, '0')); return out.join(''); } exports.Uint8Array2Hex = Uint8Array2Hex; // Uint8ArrayFromHex parses HEX to binary array. // allows grouping by 4 bytes and new lines function Uint8ArrayFromHex(input) { const matched = input.match(/[A-Fa-f\d]{2}/g); if (matched == null) { return new Uint8Array(0); } return new Uint8Array(matched.map(byte => parseInt(byte, 16))); } exports.Uint8ArrayFromHex = Uint8ArrayFromHex; function SecureEqual(uno, dos) { if (uno.length != dos.length) { return false; } let same = true; for (let i = 0; i < uno.length; i++) { const u = uno[i]; const d = dos[i]; if (u !== d) same = false; } return same; } exports.SecureEqual = SecureEqual; function SecureRandom(length) { return __awaiter(this, void 0, void 0, function* () { const out = new Uint8Array(length); yield crypto.getRandomValues(out); return out; }); } exports.SecureRandom = SecureRandom; function Hash(name, ...inputs) { return __awaiter(this, void 0, void 0, function* () { let data = new Uint8Array(); for (let input of inputs) { data = new Uint8Array([...data, ...input]); } return new Uint8Array(yield crypto.subtle.digest(name, data)); }); } exports.Hash = Hash; //# sourceMappingURL=util.js.map