@expressive-analytics/deep-thought-authentication
Version:
Typescript conversion of Deep Thought Authentication
35 lines (34 loc) • 1.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DTUserModel = void 0;
const deep_thought_js_1 = require("@expressive-analytics/deep-thought-js");
const crypto = require('crypto');
const dt_password_charset = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789";
const dt_salt_charset = "abcdef0123456789";
class DTUserModel extends deep_thought_js_1.DTModel {
encryptPassword(pass, salt) {
if (salt === undefined)
salt = this.generateString(5, dt_password_charset);
salt = salt.substring(0, 10);
const sha1 = crypto.createHash("sha1");
sha1.update(pass + salt);
return sha1.digest('hex').substring(salt.length) + salt; // encrypted = tail of sha1 + salt
}
generateString(len = 8, charset = dt_password_charset) {
let str = "";
for (let i = 0; i < len; i++) {
let n = Math.random() * charset.length;
str += charset.substring(n, 1);
}
return str;
}
verifyPassword(given, salt_len = 5) {
const encrypted = this.$get('password'); // accessor returns null
const salt = salt_len > 0 ? encrypted.substring(encrypted.length - salt_len, salt_len) : "";
const password = this.encryptPassword(given, salt);
return encrypted === password;
}
}
exports.DTUserModel = DTUserModel;
DTUserModel.$T = "user";
DTUserModel._strict_properties = true;