stuff and junk and things
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
export function withSides(sides) {
|
||||
const r = Math.random()
|
||||
const r = Math.random();
|
||||
return Math.floor(r * sides) + 1;
|
||||
}
|
||||
|
||||
@@ -10,4 +10,3 @@ export function d6() {
|
||||
export function d8() {
|
||||
return withSides(8);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@ export function cleanName(s) {
|
||||
if (typeof s !== "string") {
|
||||
throw new Error("String expected, but got a ", typeof s);
|
||||
}
|
||||
return s.toLowerCase().replace(" ", "_").replace(/[^a-zA-Z0-9_]/, "_");
|
||||
return s
|
||||
.toLowerCase()
|
||||
.replace(" ", "_")
|
||||
.replace(/[^a-zA-Z0-9_]/, "_");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* Very bad logic error. Player must quit game, refresh page, and log in again.
|
||||
*
|
||||
* Client-->Server
|
||||
@@ -31,14 +31,14 @@ export const MSG_PASSWORD = "pass";
|
||||
*/
|
||||
export const MSG_PROMPT = "ask";
|
||||
|
||||
/**
|
||||
/**
|
||||
* Client sends the player's username to the server
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
*/
|
||||
export const MSG_USERNAME = "user";
|
||||
|
||||
/**
|
||||
/**
|
||||
* Player has entered a command, and wants to do something.
|
||||
*
|
||||
* Player-->Client-->Server
|
||||
@@ -49,8 +49,8 @@ export const MSG_COMMAND = "c";
|
||||
* Represents a message sent from client to server.
|
||||
*/
|
||||
export class ClientMessage {
|
||||
/**
|
||||
* @protected
|
||||
/**
|
||||
* @protected
|
||||
* @type {any[]} _arr The array that contains the message data
|
||||
*/
|
||||
_arr;
|
||||
@@ -65,28 +65,36 @@ export class ClientMessage {
|
||||
return this._arr[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param {string} msgData the raw text data in the websocket message.
|
||||
*/
|
||||
constructor(msgData) {
|
||||
if (typeof msgData !== "string") {
|
||||
throw new Error("Could not create client message. Attempting to parse json, but data was not even a string, it was a " + typeof msgData);
|
||||
return
|
||||
throw new Error(
|
||||
"Could not create client message. Attempting to parse json, but data was not even a string, it was a " +
|
||||
typeof msgData,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
this._arr = JSON.parse(msgData);
|
||||
} catch (_) {
|
||||
throw new Error(`Could not create client message. Attempting to parse json, but data was invalid json: >>> ${msgData} <<<`);
|
||||
throw new Error(
|
||||
`Could not create client message. Attempting to parse json, but data was invalid json: >>> ${msgData} <<<`,
|
||||
);
|
||||
}
|
||||
|
||||
if (typeof this._arr !== "array") {
|
||||
throw new Error(`Could not create client message. Excpected an array, but got a ${typeof this._arr}`);
|
||||
throw new Error(
|
||||
`Could not create client message. Excpected an array, but got a ${typeof this._arr}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (this._arr.length < 1) {
|
||||
throw new Error("Could not create client message. Excpected an array with at least 1 element, but got an empty one");
|
||||
throw new Error(
|
||||
"Could not create client message. Excpected an array with at least 1 element, but got an empty one",
|
||||
);
|
||||
}
|
||||
|
||||
this._arr = arr;
|
||||
|
||||
@@ -1,30 +1,42 @@
|
||||
import { randomBytes, pbkdf2Sync, randomInt } from 'node:crypto';
|
||||
import { randomBytes, pbkdf2Sync, randomInt } from "node:crypto";
|
||||
|
||||
// Settings (tune as needed)
|
||||
const ITERATIONS = 100_000; // Slow enough to deter brute force
|
||||
const KEYLEN = 64; // 512-bit hash
|
||||
const DIGEST = 'sha512';
|
||||
const DIGEST = "sha512";
|
||||
|
||||
/**
|
||||
* Generate a hash from a plaintext password.
|
||||
* @param {String} password
|
||||
* @param {String} password
|
||||
* @returns String
|
||||
*/
|
||||
export function hash(password) {
|
||||
const salt = randomBytes(16).toString('hex'); // 128-bit salt
|
||||
const hash = pbkdf2Sync(password, salt, ITERATIONS, KEYLEN, DIGEST).toString('hex');
|
||||
return `${ITERATIONS}:${salt}:${hash}`;
|
||||
const salt = randomBytes(16).toString("hex"); // 128-bit salt
|
||||
const hash = pbkdf2Sync(
|
||||
password,
|
||||
salt,
|
||||
ITERATIONS,
|
||||
KEYLEN,
|
||||
DIGEST,
|
||||
).toString("hex");
|
||||
return `${ITERATIONS}:${salt}:${hash}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a password is correct against a given hash.
|
||||
*
|
||||
* @param {String} password
|
||||
* @param {String} hashed_password
|
||||
*
|
||||
* @param {String} password
|
||||
* @param {String} hashed_password
|
||||
* @returns Boolean
|
||||
*/
|
||||
export function verify(password, hashed_password) {
|
||||
const [iterations, salt, hash] = hashed_password.split(':');
|
||||
const derived = pbkdf2Sync(password, salt, Number(iterations), KEYLEN, DIGEST).toString('hex');
|
||||
return hash === derived;
|
||||
const [iterations, salt, hash] = hashed_password.split(":");
|
||||
const derived = pbkdf2Sync(
|
||||
password,
|
||||
salt,
|
||||
Number(iterations),
|
||||
KEYLEN,
|
||||
DIGEST,
|
||||
).toString("hex");
|
||||
return hash === derived;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user