Skip to content

Commit 29a048e

Browse files
committed
feat: add create hmac signature
1 parent bdfe26f commit 29a048e

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

package-lock.json

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/utilities/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
},
5050
"dependencies": {
5151
"@apify/consts": "^2.37.0",
52-
"@apify/log": "^2.5.13"
52+
"@apify/log": "^2.5.13",
53+
"base62": "^2.0.2"
5354
},
5455
"devDependencies": {
5556
"@types/create-hmac": "^1.1.0"

packages/utilities/src/hmac.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import crypto from 'crypto';
2+
3+
import base62 from 'base62';
4+
5+
/**
6+
* Generates an HMAC signature and encodes it using Base62.
7+
* Base62 encoding reduces the signature length.
8+
*
9+
* @param secretKey {string} Secret key used for signing signatures
10+
* @param message {string} Message to be signed
11+
* @returns string
12+
*/
13+
export function createHmacSignature(secretKey: string, message: string): string {
14+
const signature = crypto.createHmac('sha256', secretKey)
15+
.update(message)
16+
.digest('hex')
17+
.substring(0, 30);
18+
19+
return base62.encode(parseInt(signature, 16));
20+
}

packages/utilities/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './webhook_payload_template';
88
export * from './crypto';
99
export * from './url_params_utils';
1010
export * from './code_hash_manager';
11+
export * from './hmac';

0 commit comments

Comments
 (0)