Skip to content

Commit

Permalink
chore: sanitizedAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Aug 28, 2024
1 parent 1d86685 commit 735a277
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/common/lib/hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const supportedAlgorithms = ['sha256', 'sha384', 'sha512'] as const;
type SupportedAlgorithms = (typeof supportedAlgorithms)[number];

export const defaultHasher: Hasher = (data, algorithm) => {
if (!supportedAlgorithms.includes(algorithm as SupportedAlgorithms)) {
const sanitizedAlgorithm = algorithm.toLowerCase().replace(/[-_]/g, '')
if (!supportedAlgorithms.includes(sanitizedAlgorithm as SupportedAlgorithms)) {
throw new Error(`Unsupported hashing algorithm ${algorithm}`);
}

return new Uint8Array(
sha(algorithm as SupportedAlgorithms)
sha(sanitizedAlgorithm as SupportedAlgorithms)
.update(data)
.digest(),
);
Expand Down

0 comments on commit 735a277

Please sign in to comment.