UNPKG

cognito-srp

Version:

Secure Remote Password protocol implementation compatible with Amazon Cognito.

44 lines (43 loc) 1.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const crypto = require("crypto"); const util_1 = require("./util"); const constants_1 = require("./constants"); class Session { constructor(poolname, username, keyOrHkdf, scramblingParameter) { this.poolname = poolname; this.username = username; this.scramblingParameter = scramblingParameter; if (keyOrHkdf instanceof Buffer) { this.key = keyOrHkdf; this.hkdf = this.calculateHkdf(); } else { this.hkdf = Buffer.from(keyOrHkdf, 'hex'); } } calculateSignature(secretBlock, timestamp) { return crypto .createHmac(util_1.HASH_TYPE, this.hkdf) .update(this.poolname) .update(this.username) .update(Buffer.from(secretBlock, 'base64')) .update(timestamp) .digest('base64'); } getHkdf() { return this.hkdf.toString('hex'); } calculateHkdf() { const prk = crypto .createHmac(util_1.HASH_TYPE, Buffer.from(util_1.padHex(this.scramblingParameter), 'hex')) .update(Buffer.from(util_1.padHex(this.key), 'hex')) .digest(); const hmac = crypto .createHmac(util_1.HASH_TYPE, prk) .update(constants_1.infoBits) .digest(); return hmac.slice(0, 16); } } exports.Session = Session;