Skip to content

Commit

Permalink
chore: tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Oct 10, 2023
1 parent 2b51b93 commit b78d4cd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
5 changes: 3 additions & 2 deletions src/constant.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const BCRYPT_SALT_LEN = 16;

export const GENSALT_DEFAULT_LOG2_ROUNDS = 10;
export const GENERATE_SALT_DEFAULT_LOG2_ROUNDS = 10;

export const BLOWFISH_NUM_ROUNDS = 16;

Expand All @@ -10,7 +10,8 @@ export const BASE64_CODE =
"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");

const naturalNumber = Array.from({ length: 64 }, (_, i) => i);
const fillNegative1 = (length: number) => Array<number>(length).fill(-1);
const fillNegative1 = (length: number): number[] =>
Array<number>(length).fill(-1);

export const BASE64_INDEX = [
...fillNegative1(46),
Expand Down
22 changes: 11 additions & 11 deletions src/crypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const encipher = (
return lr;
};

const streamtoWord = (
const streamToWord = (
data: number[],
offp: number,
): { key: number; offp: number } => {
Expand All @@ -129,7 +129,7 @@ const streamtoWord = (
(word = (word << 8) | (data[offp] & 0xff)),
(offp = (offp + 1) % data.length);

return { key: word, offp: offp };
return { key: word, offp };
};

const key = (
Expand All @@ -147,7 +147,7 @@ const key = (
};

for (let i = 0; i < pLength; i++)
(sw = streamtoWord(key, offp)), (offp = sw.offp), (P[i] = P[i] ^ sw.key);
(sw = streamToWord(key, offp)), (offp = sw.offp), (P[i] = P[i] ^ sw.key);

for (let i = 0; i < pLength; i += 2)
(lr = encipher(lr, 0, P, S)), (P[i] = lr[0]), (P[i + 1] = lr[1]);
Expand All @@ -159,7 +159,7 @@ const key = (
/**
* Expensive key schedule Blowfish.
*/
const ekskey = (
const expensiveKeyScheduleBlowFish = (
data: number[],
key: number[],
P: Int32Array | number[],
Expand All @@ -175,26 +175,26 @@ const ekskey = (
};

for (let i = 0; i < pLength; i++)
(sw = streamtoWord(key, offp)), (offp = sw.offp), (P[i] = P[i] ^ sw.key);
(sw = streamToWord(key, offp)), (offp = sw.offp), (P[i] = P[i] ^ sw.key);

offp = 0;

for (let i = 0; i < pLength; i += 2)
(sw = streamtoWord(data, offp)),
(sw = streamToWord(data, offp)),
(offp = sw.offp),
(lr[0] ^= sw.key),
(sw = streamtoWord(data, offp)),
(sw = streamToWord(data, offp)),
(offp = sw.offp),
(lr[1] ^= sw.key),
(lr = encipher(lr, 0, P, S)),
(P[i] = lr[0]),
(P[i + 1] = lr[1]);

for (let i = 0; i < sLength; i += 2)
(sw = streamtoWord(data, offp)),
(sw = streamToWord(data, offp)),
(offp = sw.offp),
(lr[0] ^= sw.key),
(sw = streamtoWord(data, offp)),
(sw = streamToWord(data, offp)),
(offp = sw.offp),
(lr[1] ^= sw.key),
(lr = encipher(lr, 0, P, S)),
Expand Down Expand Up @@ -255,10 +255,10 @@ export const crypt = (
S = S_ORIG.slice();
}

ekskey(salt, bytes, P, S);
expensiveKeyScheduleBlowFish(salt, bytes, P, S);

/**
* Calcualtes the next round.
* Calculates the next round.
*/
const next = (): Promise<number[] | undefined> | number[] | void => {
if (progressCallback) progressCallback(i / rounds);
Expand Down
4 changes: 2 additions & 2 deletions src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { encodeBase64, decodeBase64 } from "./base64.js";
import {
BCRYPT_SALT_LEN,
C_ORIG,
GENSALT_DEFAULT_LOG2_ROUNDS,
GENERATE_SALT_DEFAULT_LOG2_ROUNDS,
} from "./constant.js";
import { crypt } from "./crypt.js";
import { genSalt, genSaltSync } from "./salt.js";
Expand Down Expand Up @@ -122,7 +122,7 @@ function _hash(
*/
export const hashSync = (
contentString: string,
salt: string | number = GENSALT_DEFAULT_LOG2_ROUNDS,
salt: string | number = GENERATE_SALT_DEFAULT_LOG2_ROUNDS,
): string => {
if (typeof salt === "number") salt = genSaltSync(salt);
if (typeof contentString !== "string" || typeof salt !== "string")
Expand Down
18 changes: 10 additions & 8 deletions src/random/browser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
declare global {
interface Window {
msCrypto?: Crypto;
}
}

/**
* @private
*
Expand All @@ -9,16 +15,12 @@
*/
export const random = (length: number): number[] => {
try {
let array: Uint32Array;
const { crypto, msCrypto } = window;
const array = new Uint32Array(length);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(self["crypto"] || self["msCrypto"])["getRandomValues"](
(array = new Uint32Array(length)),
);
(crypto || msCrypto)?.getRandomValues(array);

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Array.prototype.slice.call(array);
return Array.from(array);
} catch (err) {
throw Error("WebCryptoAPI is not available");
}
Expand Down
11 changes: 8 additions & 3 deletions src/salt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { encodeBase64 } from "./base64.js";
import { BCRYPT_SALT_LEN, GENSALT_DEFAULT_LOG2_ROUNDS } from "./constant.js";
import {
BCRYPT_SALT_LEN,
GENERATE_SALT_DEFAULT_LOG2_ROUNDS,
} from "./constant.js";
import { nextTick } from "./utils.js";
import { random } from "@random";

Expand All @@ -10,7 +13,9 @@ import { random } from "@random";
* @returns Resulting salt
* @throws {Error} If a random fallback is required but not set
*/
export const genSaltSync = (rounds = GENSALT_DEFAULT_LOG2_ROUNDS): string => {
export const genSaltSync = (
rounds = GENERATE_SALT_DEFAULT_LOG2_ROUNDS,
): string => {
if (typeof rounds !== "number")
throw Error("Illegal arguments: " + typeof rounds);
if (rounds < 4) rounds = 4;
Expand All @@ -33,7 +38,7 @@ export const genSaltSync = (rounds = GENSALT_DEFAULT_LOG2_ROUNDS): string => {
* @param rounds Number of rounds to use, defaults to 10 if omitted
*/
export const genSalt = (
rounds = GENSALT_DEFAULT_LOG2_ROUNDS,
rounds = GENERATE_SALT_DEFAULT_LOG2_ROUNDS,
): Promise<string> => {
if (typeof rounds !== "number")
throw Error("illegal arguments: " + typeof rounds);
Expand Down
4 changes: 2 additions & 2 deletions src/utfx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const UTF16toUTF8 = (
*/
export const UTF8toUTF16 = (
src: (() => number | null) | number,
dst: (byte: number) => void,
dst: (byte: number | null) => void,
): void => {
let cp = null;

Expand Down Expand Up @@ -182,7 +182,7 @@ export const encodeUTF16toUTF8 = (
*/
export const decodeUTF8toUTF16 = (
src: () => number | null,
dst: (byte: number) => void,
dst: (byte: number | null) => void,
): void => {
decodeUTF8(src, (cp) => {
UTF8toUTF16(cp, dst);
Expand Down

0 comments on commit b78d4cd

Please sign in to comment.