From 6aa790c0bbb547608d88a6ae3809ffa6cae3cf73 Mon Sep 17 00:00:00 2001 From: "Lukas.J.Han" Date: Wed, 31 Jan 2024 18:09:58 +0900 Subject: [PATCH] feat: Support lower version of nodejs in crypto (#48) Signed-off-by: Lukas --- src/crypto.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/crypto.ts b/src/crypto.ts index adf4143..b537aff 100644 --- a/src/crypto.ts +++ b/src/crypto.ts @@ -15,7 +15,8 @@ export const digest = async ( algorithm: string = 'SHA-256', ): Promise => { const Crypto = require('node:crypto'); - const hash = Crypto.createHash(algorithm); + const nodeAlg = toNodeCryptoAlg(algorithm); + const hash = Crypto.createHash(nodeAlg); hash.update(data); return hash.digest('hex'); }; @@ -23,3 +24,6 @@ export const digest = async ( export const getHasher = (algorithm: string = 'SHA-256') => { return (data: string) => digest(data, algorithm); }; + +const toNodeCryptoAlg = (hashAlg: string): string => + hashAlg.replace('-', '').toLowerCase();