Skip to content

Commit

Permalink
Conditional validation for parameter algorithm in isHash function
Browse files Browse the repository at this point in the history
Signed-off-by: kushagra-raj.tiwari <[email protected]>
  • Loading branch information
kushagra-raj.tiwari authored and kushagra-raj.tiwari committed Sep 30, 2023
1 parent c49c8f6 commit c2186bd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/lib/isHash.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import validateKey from './util/validateKey';

const lengths = {
md5: 32,
Expand All @@ -16,8 +17,17 @@ const lengths = {
crc32b: 8,
};

export default function isHash(str, algorithm) {

export default function isHash(
str,
algorithm,
_validateAlgorithm = true
) {
assertString(str);
if (_validateAlgorithm) {
/* Validate hash algorithm before defining Regular expression for hash check. */
validateKey(lengths, algorithm, `Unknown hash algorithm: ${algorithm}`);
}
const hash = new RegExp(`^[a-fA-F0-9]{${lengths[algorithm]}}$`);
return hash.test(str);
}

0 comments on commit c2186bd

Please sign in to comment.