const crypto = require("crypto");

const TOKEN_TTL_MS = 10 * 60 * 1000;

function generateOneTimeToken(now = Date.now()) {
  const token = crypto.randomBytes(32).toString("hex");
  return {
    token,
    digest: digestOneTimeToken(token),
    expiresAt: new Date(now + TOKEN_TTL_MS),
  };
}

function digestOneTimeToken(token) {
  return crypto.createHash("sha256").update(String(token)).digest("hex");
}

module.exports = { TOKEN_TTL_MS, generateOneTimeToken, digestOneTimeToken };
