diff --git a/src/constant.ts b/src/constant.ts index 77dff0d..536461e 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -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; @@ -10,7 +10,8 @@ export const BASE64_CODE = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split(""); const naturalNumber = Array.from({ length: 64 }, (_, i) => i); -const fillNegative1 = (length: number) => Array(length).fill(-1); +const fillNegative1 = (length: number): number[] => + Array(length).fill(-1); export const BASE64_INDEX = [ ...fillNegative1(46), diff --git a/src/crypt.ts b/src/crypt.ts index 085d290..d978681 100644 --- a/src/crypt.ts +++ b/src/crypt.ts @@ -119,7 +119,7 @@ const encipher = ( return lr; }; -const streamtoWord = ( +const streamToWord = ( data: number[], offp: number, ): { key: number; offp: number } => { @@ -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 = ( @@ -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]); @@ -159,7 +159,7 @@ const key = ( /** * Expensive key schedule Blowfish. */ -const ekskey = ( +const expensiveKeyScheduleBlowFish = ( data: number[], key: number[], P: Int32Array | number[], @@ -175,15 +175,15 @@ 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)), @@ -191,10 +191,10 @@ const ekskey = ( (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)), @@ -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[] | void => { if (progressCallback) progressCallback(i / rounds); diff --git a/src/hash.ts b/src/hash.ts index 8898daf..f529e6c 100644 --- a/src/hash.ts +++ b/src/hash.ts @@ -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"; @@ -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") diff --git a/src/random/browser.ts b/src/random/browser.ts index 6fe0bf6..e77666b 100644 --- a/src/random/browser.ts +++ b/src/random/browser.ts @@ -1,3 +1,9 @@ +declare global { + interface Window { + msCrypto?: Crypto; + } +} + /** * @private * @@ -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"); } diff --git a/src/salt.ts b/src/salt.ts index 6eed73d..1c6bc17 100644 --- a/src/salt.ts +++ b/src/salt.ts @@ -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"; @@ -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; @@ -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 => { if (typeof rounds !== "number") throw Error("illegal arguments: " + typeof rounds); diff --git a/src/utfx.ts b/src/utfx.ts index 1cb59f4..146d002 100644 --- a/src/utfx.ts +++ b/src/utfx.ts @@ -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; @@ -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);