UNPKG

@iobroker/js-controller-common-db

Version:

The Library contains the common utils for the ioBroker controller which can be used by db classes too, as they do not rely on the db (circular dependencies).

77 lines (76 loc) 2.82 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var password_exports = {}; __export(password_exports, { password: () => password }); module.exports = __toCommonJS(password_exports); var import_node_crypto = __toESM(require("node:crypto"), 1); function password(pw) { return { hash: (salt, iterations, callback) => { salt = salt || import_node_crypto.default.randomBytes(16).toString("hex"); iterations = iterations || 1e4; import_node_crypto.default.pbkdf2(pw, salt, iterations, 256, "sha256", (err, key) => { if (err) { return callback(err); } callback(null, `pbkdf2$${iterations}$${key.toString("hex")}$${salt}`); }); }, check: function(hashedPassword, callback) { if (!hashedPassword) { return callback(null, false); } const key = hashedPassword.split("$"); if (key.length !== 4 || !key[2] || !key[3]) { return callback(new Error("Hash not formatted correctly")); } if (key[0] !== "pbkdf2") { return callback(new Error("Unknown")); } this.hash(key[3], parseInt(key[1], 10), (error, newHash) => { if (error) { callback(error); } else { callback(null, newHash === hashedPassword); } }); }, complexity: (password2, callback) => { let result = false; if (typeof password2 === "string") { result = password2.length >= 8 && /\d/.test(password2) && /[a-z]/.test(password2) && /[A-Z]/.test(password2); } typeof callback === "function" && callback(result); return result; } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { password }); //# sourceMappingURL=password.js.map