-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { sign, signAsync } from './sign.js'; | ||
|
||
describe('sign', () => { | ||
it('should generate correct signature with both async and sync version', async () => { | ||
const signingKey = 'foo'; | ||
const payload = { | ||
bar: 'bar', | ||
foo: 'foo', | ||
}; | ||
|
||
const signature = sign(signingKey, payload); | ||
const signatureByAsync = await signAsync(signingKey, payload); | ||
const expectedResult = | ||
'sha256=436958f1dbfefab37712fb3927760490fbf7757da8c0b2306ee7b485f0360eee'; | ||
|
||
expect(signature).toBe(expectedResult); | ||
expect(signatureByAsync).toBe(expectedResult); | ||
}); | ||
|
||
it('should generate correct signature if payload is empty with both async and sync version', async () => { | ||
const signingKey = 'foo'; | ||
const payload = {}; | ||
const signature = sign(signingKey, payload); | ||
const signatureByAsync = await signAsync(signingKey, payload); | ||
const expectedResult = | ||
'sha256=c76356efa19d219d1d7e08ccb20b1d26db53b143156f406c99dcb8e0876d6c55'; | ||
|
||
expect(signature).toBe(expectedResult); | ||
expect(signatureByAsync).toBe(expectedResult); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createHmac } from 'node:crypto'; | ||
import { promisify } from 'node:util'; | ||
|
||
export const sign = (signingKey: string, payload: Record<string, unknown>): string => { | ||
const hmac = createHmac('sha256', signingKey); | ||
const payloadString = JSON.stringify(payload); | ||
hmac.update(payloadString); | ||
return `sha256=${hmac.digest('hex')}`; | ||
}; | ||
|
||
export const signAsync = async (signingKey: string, payload: Record<string, unknown>) => | ||
promisify<string>((callback) => { | ||
callback(null, sign(signingKey, payload)); | ||
})(); |