@cocalc/backend
Version:
CoCalc backend functionality: functionality used by either the hub, the next.js server or the project.
40 lines • 1.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyPassword = void 0;
const password_hash_1 = require("password-hash");
Object.defineProperty(exports, "verifyPassword", { enumerable: true, get: function () { return password_hash_1.verify; } });
const lru_cache_1 = __importDefault(require("lru-cache"));
// We cache computation of the hash, since e.g., api keys have the
// hash computed for every single api call, and it's always the same key,
// so that's expensive.
const cache = new lru_cache_1.default({
max: 1000,
ttl: 1000 * 60 * 5, // 5 minutes
});
// You can change the parameters at any time and no existing passwords
// or cookies should break. This will only impact newly created
// passwords and cookies. Old ones can be read just fine (with the old
// parameters).
const HASH_ALGORITHM = "sha512";
const HASH_ITERATIONS = 1000;
const HASH_SALT_LENGTH = 32;
function passwordHash(password) {
// This blocks the server for around 5ms.
// There are newer async libraries as explained at https://www.npmjs.com/package/password-hash
// that do NOT block, which maybe we should be using instead....
if (cache.has(password)) {
return cache.get(password);
}
const hash = (0, password_hash_1.generate)(password, {
algorithm: HASH_ALGORITHM,
saltLength: HASH_SALT_LENGTH,
iterations: HASH_ITERATIONS,
});
cache.set(password, hash);
return hash;
}
exports.default = passwordHash;
//# sourceMappingURL=password-hash.js.map