-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify signature compute and verify
- Loading branch information
Showing
7 changed files
with
42 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,31 @@ | ||
import { subtle } from 'node:crypto' | ||
import { Buffer } from 'node:buffer' | ||
|
||
export const encoder = new TextEncoder() | ||
export const hmacAlgorithm = { name: 'HMAC', hash: 'SHA-256' } | ||
export const ed25519Algorithm = { name: 'Ed25519', namedCurve: 'Ed25519' } | ||
|
||
export const computeSignature = async ( | ||
secretKey: string, | ||
algorithm: typeof hmacAlgorithm | typeof ed25519Algorithm, | ||
payload: string, | ||
options?: Partial<{ extractable: boolean, encoding: BufferEncoding }>, | ||
) => { | ||
const key = await subtle.importKey('raw', encoder.encode(secretKey), algorithm, options?.extractable ?? false, ['sign']) | ||
const signature = await subtle.sign(algorithm.name, key, encoder.encode(payload)) | ||
return Buffer.from(signature).toString(options?.encoding ?? 'hex') | ||
} | ||
|
||
export const verifyPublicSignature = async ( | ||
publicKey: string, | ||
algorithm: typeof hmacAlgorithm | typeof ed25519Algorithm, | ||
payload: string, | ||
signature: string, | ||
options?: Partial<{ extractable: boolean, encoding: BufferEncoding }>, | ||
) => { | ||
const publicKeyBuffer = Buffer.from(publicKey, 'hex') | ||
const webhookSignatureBuffer = Buffer.from(signature, 'hex') | ||
const key = await subtle.importKey('raw', publicKeyBuffer, algorithm, options?.extractable ?? false, ['verify']) | ||
const result = await subtle.verify(algorithm.name, key, webhookSignatureBuffer, encoder.encode(payload)) | ||
return result | ||
} |
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
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