-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sjcl, password hashing, math functions
- Loading branch information
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import * as alt from 'alt-client'; | ||
|
||
/** | ||
* It's a function that takes two vectors and returns a vector. | ||
* | ||
* | ||
* @name getCrossProduct | ||
* @param {alt.Vector3} v1 | ||
* @param {alt.Vector3} v2 | ||
* @returns {alt.Vector3} | ||
* s | ||
*/ | ||
export function getCrossProduct(v1: alt.Vector3, v2: alt.Vector3): alt.Vector3 { | ||
return new alt.Vector3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x); | ||
} | ||
|
||
/** | ||
* It's a function that takes a vector and returns a normalized vector. | ||
* | ||
* | ||
* @name getNormalizedVector | ||
* @param {alt.Vector3} vector | ||
* @returns {alt.Vector3} | ||
* s | ||
*/ | ||
export function getNormalizedVector(vector: alt.Vector3): alt.Vector3 { | ||
const mag = Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z); | ||
return new alt.Vector3(vector.x / mag, vector.y / mag, vector.z / mag); | ||
} | ||
|
||
/** | ||
* Converts degrees to radians | ||
* | ||
* | ||
* @name degToRad | ||
* @param {number} degrees | ||
* @returns {number} | ||
* s | ||
*/ | ||
export function degToRad(degrees: number): number { | ||
return (degrees * Math.PI) / 180; | ||
} | ||
|
||
/** | ||
* Could also be seen as rotAnglesToVector | ||
* | ||
* | ||
* @param {alt.IVector3} rotation | ||
* @return {alt.Vector3} | ||
*/ | ||
export function rotationToDirection(rotation: alt.IVector3): alt.Vector3 { | ||
const z = degToRad(rotation.z); | ||
const x = degToRad(rotation.x); | ||
const num = Math.abs(Math.cos(x)); | ||
|
||
return new alt.Vector3(-Math.sin(z) * num, Math.cos(z) * num, Math.sin(x)); | ||
} | ||
|
||
/** | ||
* It's converting a rotation to a direction. | ||
* | ||
* | ||
* @name getDirectionFromRotation | ||
* @param {alt.IVector3} rotation | ||
* @returns {alt.IVector3} | ||
* s | ||
*/ | ||
export function getDirectionFromRotation(rotation: alt.IVector3): alt.IVector3 { | ||
const z = rotation.z * (Math.PI / 180.0); | ||
const x = rotation.x * (Math.PI / 180.0); | ||
const num = Math.abs(Math.cos(x)); | ||
|
||
return new alt.Vector3(-Math.sin(z) * num, Math.cos(z) * num, Math.sin(x)); | ||
} | ||
|
||
/** | ||
* Returns an array of alt.IVector2 in a circle | ||
* | ||
* | ||
* @name getPointsInCircle | ||
* @param {number} points | ||
* @param {number} radius | ||
* @param {alt.IVector2} center | ||
* @returns {Array<alt.IVector2>} | ||
* s | ||
*/ | ||
export function getPointsInCircle(points: number, radius: number, center: alt.IVector2): Array<alt.IVector2> { | ||
const slice = (2 * Math.PI) / points; | ||
const pointDefs = []; | ||
|
||
for (let i = 0; i < points; i++) { | ||
const sliceAngle = slice * i; | ||
const x = center.x + (radius / 2) * Math.cos(sliceAngle); | ||
const y = center.y + radius * Math.sin(sliceAngle); | ||
pointDefs.push({ x, y }); | ||
} | ||
|
||
return pointDefs; | ||
} | ||
|
||
/** | ||
* Returns the average value among all numbers. | ||
* | ||
* | ||
* @param {Array<number>} data | ||
* @return {number} | ||
*/ | ||
export function getAverage(data: Array<number>): number { | ||
let sum = 0; | ||
for (let i = 0; i < data.length; i++) { | ||
sum += data[i]; | ||
} | ||
|
||
return sum / data.length; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import sjcl from 'sjcl'; | ||
|
||
/** | ||
* Hash a string of data into a persistent SHA256 hash. | ||
* | ||
* @param {string} data | ||
* @returns {string} | ||
*/ | ||
export function sha256(data: string): string { | ||
const hashBits = sjcl.hash.sha256.hash(data); | ||
return sjcl.codec.hex.fromBits(hashBits); | ||
} | ||
|
||
/** | ||
* Hash a string of data into a random SHA256 hash. | ||
* | ||
* @param {string} data | ||
* @returns {string} | ||
*/ | ||
export function sha256Random(data: string): string { | ||
const randomValue = Math.random(); | ||
return sha256(`${data} + ${randomValue}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './hash.js'; | ||
export * from './password.js'; | ||
export * from './reconnect.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import sjcl from 'sjcl'; | ||
|
||
/** | ||
* Hash a plain text password with pbkdf2 hash and salt. | ||
* | ||
* Returns a pbkdf2 key, and salt. Which can be seperated by the `$` sign. | ||
* | ||
* #### Example | ||
* ```ts | ||
* const result = Athena.utility.hash.hashPassword('somePassword'); | ||
* ``` | ||
* | ||
* @param {string} plainTextPassword | ||
* @returns {string} | ||
*/ | ||
export function hash(plainTextPassword: string): string { | ||
const saltBits = sjcl.random.randomWords(128, 0); | ||
const salt = sjcl.codec.base64.fromBits(saltBits); | ||
const key = sjcl.codec.base64.fromBits(sjcl.misc.pbkdf2(plainTextPassword, saltBits, 2000, 256)); | ||
return `${key}$${salt}`; | ||
} | ||
|
||
/** | ||
* Test a plain text password against a stored pbkdf2 string. | ||
* | ||
* #### Example | ||
* ```ts | ||
* // Actual pbkdf2Hash is just mock string | ||
* const doesMatch = Athena.utility.hash.testPassword('test', 'kjfdskljfsdkl$90jj0f10f21f1') | ||
* ``` | ||
* | ||
* @param {string} plainTextPassword | ||
* @param {string} pbkdf2Hash | ||
* @returns {boolean} | ||
*/ | ||
export function check(plainTextPassword: string, pbkdf2Hash: string): boolean { | ||
const [_key, _salt] = pbkdf2Hash.split('$'); | ||
const saltBits = sjcl.codec.base64.toBits(_salt); | ||
const derivedKey = sjcl.misc.pbkdf2(plainTextPassword, saltBits, 2000, 256); | ||
const derivedBaseKey = sjcl.codec.base64.fromBits(derivedKey); | ||
return _key === derivedBaseKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * as clone from './clone.js'; | ||
export * as color from './color.js'; | ||
export * as flag from './flags.js'; | ||
export * as math from './math.js'; | ||
export * as random from './random.js'; | ||
export * as vector from './vector.js'; | ||
export * as uid from './uid.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Takes an array of numbers and attempts to determine which number is missing given a range. | ||
* Returns the first missing number. | ||
* | ||
* | ||
* @param {Array<number>} arr | ||
* @param {number} [startIndex=0] | ||
* @return {number} | ||
*/ | ||
export function getMissingNumber(arr: Array<number>, startIndex = 0): number { | ||
if (arr.length <= 0) { | ||
return startIndex; | ||
} | ||
|
||
const count = arr.length; | ||
for (var i = startIndex; i <= count; i++) { | ||
if (arr.indexOf(i) == -1) { | ||
return i; | ||
} | ||
} | ||
|
||
return arr.length + 1; | ||
} |